Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > General > Gaming and Game Development

Reply
 
Thread Tools Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
Old 10-27-2008, 12:09 AM   #1
ChiWhiteSox56
ChiWhiteSox56
 
Join Date: Oct 2008
Location: Chicago, IL
Posts: 8
Wink [AS3] movieclip doesn't play when holding arrow keys down

Hello,

I'm having a problem with a little game that I'm making in AS3. I'm pretty new to AS, so this may be something very simple.

I have a character that is supposed to walk back and forth across the stage when the left and right arrow keys are held down. The character moves back and forth as expected, but the walking motion for the character only plays once and does not loop like I want it to. The labels for the walking motion in the character movie clip are "left" and "right". Character is mcPanda.

The bottom line is that I can't get the movie clip to loop when I hold down the arrow key is AS3. Any help would be greatly, greatly appreciated!

- ChiWhiteSox56

ActionScript Code:
var leftKeyDown:Boolean = false; var rightKeyDown:Boolean = false; var pandaSpeed:Number = 10; // Listener - makes panda move based on key strokes that are down mcPanda.addEventListener(Event.ENTER_FRAME, movePanda); function movePanda(event:Event):void {     if(leftKeyDown)     {         mcPanda.x -= pandaSpeed;     }     if(rightKeyDown)     {         mcPanda.x += pandaSpeed;     } } //Key down stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown); function checkKeysDown(event:KeyboardEvent):void {     //Making booleans true bbased on keycode     if(event.keyCode == 37)     {         leftKeyDown = true;         mcPanda.gotoAndPlay("left");     }     if(event.keyCode == 39)     {         rightKeyDown = true;         mcPanda.gotoAndPlay("right");     }         if(mcPanda.x < mcPanda.width / 2)     {         mcPanda.x = 55;     }         else if(mcPanda.x > stage.stageWidth - mcPanda.width / 1.85)     {         mcPanda.x = stage.stageWidth - mcPanda.width / 1.85;     } } //Key up stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp); function checkKeysUp(event:KeyboardEvent):void {     //Making booleans false based on keycode     if(event.keyCode == 37)     {         leftKeyDown = false;         mcPanda.gotoAndStop("leftStop");     }     if(event.keyCode == 39)     {         rightKeyDown = false;         mcPanda.gotoAndStop("rightStop");     } }
Attached Files
File Type: zip pandamonium_TryAgain1.zip (24.8 KB, 475 views)

Last edited by ChiWhiteSox56; 10-27-2008 at 12:10 AM. Reason: Adding another detail for clarity
ChiWhiteSox56 is offline   Reply With Quote
Old 10-27-2008, 12:58 AM   #2
Mazoonist
Site Contributor
 
Mazoonist's Avatar
 
Join Date: Jun 2006
Posts: 3,160
Send a message via AIM to Mazoonist
Default

Wow! I really like your panda! Here's the code that fixes it:
ActionScript Code:
var leftKeyDown:Boolean = false; var rightKeyDown:Boolean = false; var pandaSpeed:Number = 10; // Listener - makes panda move based on key strokes that are down mcPanda.addEventListener(Event.ENTER_FRAME, movePanda); function movePanda(event:Event):void {     if(leftKeyDown)     {         mcPanda.x -= pandaSpeed;     }     if(rightKeyDown)     {         mcPanda.x += pandaSpeed;     } } //Key down stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown); function checkKeysDown(event:KeyboardEvent):void {     //Making booleans true bbased on keycode     if(event.keyCode == 37)     {         leftKeyDown = true;         if(mcPanda.currentLabel == "leftStop" || mcPanda.currentLabel == "rightStop")         {             mcPanda.gotoAndPlay("left");         }     }     if(event.keyCode == 39)     {         rightKeyDown = true;         if(mcPanda.currentLabel == "leftStop" || mcPanda.currentLabel == "rightStop")         {             mcPanda.gotoAndPlay("right");         }     }         if(mcPanda.x < mcPanda.width / 2)     {         mcPanda.x = 55;     }         else if(mcPanda.x > stage.stageWidth - mcPanda.width / 1.85)     {         mcPanda.x = stage.stageWidth - mcPanda.width / 1.85;     } } //Key up stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp); function checkKeysUp(event:KeyboardEvent):void {     //Making booleans false based on keycode     if(event.keyCode == 37)     {         leftKeyDown = false;         mcPanda.gotoAndStop("leftStop");     }     if(event.keyCode == 39)     {         rightKeyDown = false;         mcPanda.gotoAndStop("rightStop");     } }
Now for the explanation: The KEY_DOWN event listens for keys being pressed. That's obvious. But when you press and hold a key, you keep getting the event repeatedly, because of the key repeat rate of the operating system. It's not like a MouseEvent.CLICK event that happens once and it's over. To demonstrate, copy this code into a new fla file and run it:
ActionScript Code:
stage.addEventListener(KeyboardEvent.KEY_DOWN, downHandler); function downHandler(event:KeyboardEvent):void {     if(event.keyCode == 37) {         trace("leftArrow");     } }
Press and hold down the left arrow key. You'll get the trace statement, then a slight lag, then a whole bunch of trace statements. That's like what's happening with your panda: he (she?) walks briefly, then after that same lag kicks in, it keeps getting the same command repeatedly, either gotoAndPlay("left") or gotoAndPlay("right"). So imagine that you're holding down the left arrow key--your code is repeatedly telling it to gotoAndPlay("left") because of the key repeat rate.

In fact, the key repeat rate is why you'd want to use booleans in the first place, because pressing and/or releasing a key shouldn't do much more than set the corresponding boolean true or false.

New in AS3 is the "currentLabel" property, which comes in handy here. The above code checks to see if you're currently stopped on one of those two "stop" frames, and if you are, then and only then does it gotoAndPlay one of those sections. Hope that all makes sense.

Did I mention I really like that panda?
__________________
My Tutorials * My Website

Last edited by Mazoonist; 10-27-2008 at 01:38 AM.
Mazoonist is offline   Reply With Quote
Old 10-27-2008, 01:37 AM   #3
ChiWhiteSox56
ChiWhiteSox56
 
Join Date: Oct 2008
Location: Chicago, IL
Posts: 8
Default

I can't thank you enough, Mazoonist!!!

This totally fixed my problem! I was unaware of the currentLabel property until today, so I definitely learned something. I was about to pull my hair out over this!

(And I'm glad you liked the panda! When this game is done, it will be a brick-breaker game, except the bricks are going to be sticks of bamboo.)

Again, on behalf of the panda and myself, thank you!
ChiWhiteSox56 is offline   Reply With Quote
Old 10-27-2008, 01:50 AM   #4
Mazoonist
Site Contributor
 
Mazoonist's Avatar
 
Join Date: Jun 2006
Posts: 3,160
Send a message via AIM to Mazoonist
Default

Great! Keep me posted on the game, huh?
__________________
My Tutorials * My Website
Mazoonist is offline   Reply With Quote
Old 10-27-2008, 02:13 AM   #5
ChiWhiteSox56
ChiWhiteSox56
 
Join Date: Oct 2008
Location: Chicago, IL
Posts: 8
Default

Will do! : )
ChiWhiteSox56 is offline   Reply With Quote
Old 05-28-2009, 05:25 AM   #6
brazaguy
Registered User
 
Join Date: May 2009
Posts: 5
Default

Hey, this is the only place where I could find that solution!
THANKS!!!

Still, There is some bug with the PANDA cause when it hits the stage limit , and you use the other arrow imediately after the one you were using, the panda move backwards with no animation and not mirrored... (or even when you dont hit the satge limit but press both keys)...

Any suggestions?

And any advices how to make the diagonal jumps (as in the famous game ANYS BATTLE)?

I am developping a game and that would be really helpful!

Thanks guys!
brazaguy 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

Similar Threads
Thread Thread Starter Forum Replies Last Post
AS3 movieclip doesn't play when holding down key scarce ActionScript 3.0 24 11-23-2008 04:49 AM
Disable scrolling by arrow keys and mouse wheel in a scrollpane manojmathew Components 5 10-27-2008 12:25 PM
Play Movieclip in reverse greed83 ActionScript 2.0 3 01-22-2008 02:15 AM
stopping when character controlled by arrow keys hits a wall megashock21 ActionScript 2.0 2 08-25-2004 08:53 PM
how to detect arrow keys? dbonneville ActionScript 1.0 (and below) 4 10-25-2002 01:40 PM


All times are GMT. The time now is 07:02 AM.

///
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.