00:00
00:00
Newgrounds Background Image Theme

Ryor just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS2 help with keyDown

377 Views | 2 Replies
New Topic Respond to this Topic

AS2 help with keyDown 2014-11-05 16:11:48


Hey there. Im wondering if anyone here can help me out with my code. The code works fine other than the fact that i have to hold the SPACE key down in order to play my animation frames. What method would i use to make it so when i PRESS the SPACE key my "strike" animation frames play.

here's my code so far.

onClipEvent (enterFrame) {

if (Key.isDown(Key.SPACE)) {
if (touchingGround == true) {
this.gotoAndStop("strike");

}else if (Key.isDown(Key.RIGHT)) {
_xscale = +scale;
if (touchingGround == true) {
this.gotoAndStop("walk");
}
_x += speed;

} else if (Key.isDown(Key.LEFT)) {
_xscale = -scale;
if (touchingGround == true) {
this.gotoAndStop("walk");
}
_x -= speed;

} else {
if (touchingGround == true) {
this.gotoAndStop("idle");
}
}

thanks in advance. sorry im a noob to actionscript.

Response to AS2 help with keyDown 2014-11-05 22:31:02


This is what's happening:

Frame 1
(SPACE key is pressed)
this.gotoAndStop("strike")

Frame 2
(SPACE key is lifted)
this.gotoAndStop("idle")

See the problem here?

---
What you need is a flag for your character actions.

// Whether this object is performing an action.
var isPerformingAction:Boolean = false;

onClipEvent (enterFrame) {

	// Only update object state if object is not performing any action.
	if (!isPerformingAction) {

		if (Key.isDown(Key.SPACE)) {
			if (touchingGround == true) {
				this.gotoAndStop("strike");
				// Set flag to true.
				isPerformingAction = true;
			} // Btw you missed a closing brace here.
		}else if (Key.isDown(Key.RIGHT)) {
			_xscale = +scale;
			if (touchingGround == true) {
				this.gotoAndStop("walk");
			}
			_x += speed;
		} else if (Key.isDown(Key.LEFT)) {
			_xscale = -scale;
			if (touchingGround == true) {
				this.gotoAndStop("walk");
			}
			_x -= speed;
		} else {
			if (touchingGround == true) {
				this.gotoAndStop("idle");
			}
		}

	}
}

On the last frame of your "strike" animation, set [isPerformingAction] back to false.


If you're a Newgrounds OG who appreciates Flash games with depth, check out the game I made in 2024.

Response to AS2 help with keyDown 2014-11-06 04:29:15


hey thanks for pointing this out its a big help. I tried using a Boolean for it before but i was missing the part on calling back the Boolean to false. Thanks again.