Hey guys,
Like the title says, I'm new to AS3/Flash and trying to learn how to make games.
I've been following the tutorials from Gary Rosenzweig's "Flash Game University" (both the book and the iTunes podcast).
As a disclaimer, all of the code I"m using comes from the above mentioned tutorials, I did not create them.
Before I post up the code, let me briefly explain my problem and what I've tried to do to solve it. Basically, I got the game to work fine when I used his code EXACTLY, but I wanted to try to make the player ("catcher") move with the left and right arrow keys instead of the mouse. However, when I put that in, the movement becomes erratic, and instead of smoothly moving left and right, the player jumps left and right, gaining speed constantly until it simply jumps off the screen. Needless to say, I have no idea why it does this.
At first, I thought the code for my arrow based movement was wrong, so I copied and pasted it into a separate file to test and it worked fine, so I figure that for some reason the code that makes the rest of the game work was somehow breaking it.
I tried moving the code around to different places, but that didn't fix the problem (in fact, it often made it worse).
I figure that I'm missing something in terms of formatting (unfortunately, none of the tutorials I've found cover proper style for AS3, so my code is probably disgustingly messy/unorganized).
Anyways, I'm not sure how to "zip" my files to a post (as per the instructions in my registration confirmation email, so I'm going to just copy and paste the code I have into this post).
As a final note, I apologize in advance for the messiness of my code/possible forum issues. I'm rather new to this and just trying to figure things out.
Any help is greatly appreciated, and thanks for taking the time to read this!
ActionScript Code: package { import flash.display.*; import flash.events.*; import flash.utils.Timer; import flash.utils.getDefinitionByName; public class CatchingGame1 extends MovieClip { var catcher:Catcher; var nextObject:Timer; var objects:Array = new Array; const speed:Number = 7.0; var score:int = 0; var leftArrow:Boolean = false; var rightArrow:Boolean = false; public function CatchingGame1() { catcher = new Catcher(); catcher.y = 350; addChild(catcher); setNextObject(); addEventListener(Event.ENTER_FRAME,moveObjects); } public function setNextObject() { nextObject = new Timer(Math.random()*1000,1); nextObject.addEventListener(TimerEvent.TIMER_COMPLETE,newObject); nextObject.start(); } public function newObject(e:Event) { var goodObjects:Array = ["Circle1","Circle2"]; var badObjects:Array = ["Square1","Square2"]; if(Math.random() < .5) { var r:int = Math.floor(Math.random()*goodObjects.length); var classRef:Class = getDefinitionByName(goodObjects[r]) as Class; var newObject:MovieClip = new classRef(); newObject.typestr = "good"; } else { r = Math.floor(Math.random()*badObjects.length); classRef = getDefinitionByName(badObjects[r]) as Class; newObject = new classRef(); newObject.typestr = "bad"; } newObject.x = Math.random()*500; addChild(newObject); objects.push(newObject); setNextObject(); } public function moveObjects(e:Event) { for(var i:int=objects.length-1; i>=0;i--) { objects[i].y += speed; if(objects[i].y > 400) { removeChild(objects[i]); objects.splice(i,1); } if(objects[i].hitTestObject(catcher)) { if(objects[i].typestr == "good") { score += 5; } else { score -= 1; } if(score < 0) score = 0; scoreDisplay.text = "Score: " + score; removeChild(objects[i]); objects.splice(i,1); } } //this is where it starts to have problems //make arrow booleans true stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown); stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp); stage.addEventListener(Event.ENTER_FRAME, moveCatcher); function keyPressedDown(Event:KeyboardEvent){ if (Event.keyCode == 37){ leftArrow = true; } else if (Event.keyCode == 39){ rightArrow = true; } } function keyPressedUp(event:KeyboardEvent) { if(event.keyCode == 37){ leftArrow = false; } else if(event.keyCode == 39){ rightArrow = false; } } function moveCatcher(event:Event) { var catcherSpeed:Number = 1; if(leftArrow){ catcher.x -= catcherSpeed; } if(rightArrow){ catcher.x += catcherSpeed; } } } }}
[AS3] having problems with movement in a tutorial game
Thursday, May 10, 2012
[Action Script] [AS3] having problems with movement in a tutorial game May,2012
Posted by Bimo Hery Prabowo at 7:13 AM
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment