Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 05-08-2012, 03:59 AM   #1
Namrettel
Registered User
 
Join Date: May 2012
Posts: 1
Default [AS3] having problems with movement in a tutorial game

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;                     }                 }                                                 }             } }
Namrettel is offline   Reply With Quote
Old 05-08-2012, 06:39 AM   #2
northmantif
dont Re Member
 
Join Date: May 2009
Location: Poland
Posts: 739
Default

you didn't close your moveObjects() method. Put a '}' before these two pinky comments and clean your code to match further braces (maybe then it's ok. but I didnt try your code as a whole - I just tried your pointed piece and it works well)
northmantif is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 11:38 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.