00:00
00:00
Newgrounds Background Image Theme

Almost-Ichabod 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!

Animation not playing on key (as3)

1,057 Views | 7 Replies
New Topic Respond to this Topic

Hi newgrounds
I am creating a sidescrolling beat 'em up game.
When I press the punch key, my punch animation will not play instead my character just pauses a little.
My player's walking animation plays fine when directional keys are pressed.
Here is where I placed the code for my punch animation

public function playerAttack(object: MovieClip):void
		{
			if((punchKeyDown) && (canPunch))
			{
				this.gotoAndPlay("playerPunch");
			}
		}

Response to Animation not playing on key (as3) 2013-01-20 18:40:50


At 1/20/13 04:29 AM, gameObject wrote: Hi newgrounds
I am creating a sidescrolling beat 'em up game.
When I press the punch key, my punch animation will not play instead my character just pauses a little.
My player's walking animation plays fine when directional keys are pressed.
Here is where I placed the code for my punch animation

public function playerAttack(object: MovieClip):void
{
if((punchKeyDown) && (canPunch))
{
this.gotoAndPlay("playerPunch");
}
}

is this function called within an update/enter frame?

If so then what may be happening is that every frame it is calling 'gotoAndPlay("playerPunch"); ' which means that you keep setting it to the first frame of the animation, hence the freezing.

Try set that canPunch flag to false during the punch animation so you won't repeatedly call gotoAndPlay.

Response to Animation not playing on key (as3) 2013-01-23 04:25:18


Thanks heaps for the reply.
hmmm I see what you mean, there is no call to an event so should I have a function just dedicated to playing the animation some like:

public function playerPunch(event:Event): void
		{
			if(punchKeyDown)
			{
				this.gotoAndStop("playerPunch");
			}
		}

I've tried this and it does play the animation but only the first frame as you said. I'm sorry i don't really understand what you mean by making canPunch false during the animation.

here is more of the code to the playerAttrack function in my player class

public function playerAttack(object: MovieClip):void
		{
			if((punchKeyDown) && (canPunch))
			{
				this.gotoAndPlay("playerPunch");

				if(this.hitTestObject(object))
				{
					trace("Hit");
					object.takeHit(10);
				}
			}
		}

and in my engine class iu've got:

public function playerAttack(event:Event):void
		{
			player.playerAttack(enemy);
		}

if i were to do something like this in my player class:

public function playerAttack(object:MovieClip, event:Event): void

is there a way to squeeze in my punch animation event this way?

Thanks again for the reply

Response to Animation not playing on key (as3) 2013-01-23 10:41:49


in AS3, unless a function's parameter has the same name as a class's method, "this." is assumed. Save yourself some carpal tunnel.
also, hitTestOject and hitTestPoint is ungodly slow. Just use math.

i'm not sure a single engine class is going to cut it, to be honest. It's a good idea, but you may wind up needing to split that up a little. Just a forewarning.

also, declare your private variables with a leading underscore to them so you can separate them out from public ones when you use them later.

the engine class should be handling collision. It should tell the player class to play an animation and where to be, and that's all the player class should be useful for. A visual and some private vars with public getters telling the engine where it is and what it's doing. Think of it like a puppet show, where the engine class is the puppet master.
Again, there should probably be three engines running your game. One for game logic (the "main" engine), one for the UI (interface, HUD, whatever you want to call it), and one for other game visuals such as backgrounds, platforms (assuming there are some), the player and enemies.

also, why are events being dispatched in your game? They're very slow, is it really necessary?


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to Animation not playing on key (as3) 2013-01-23 14:10:36


can you show the code of where the in engine class function 'public function playerAttack(event:Event):void' is called from.

is that event an ENTER_FRAME event?

Response to Animation not playing on key (as3) 2013-01-24 03:14:19


At 1/23/13 10:41 AM, egg82 wrote: in AS3, unless a function's parameter has the same name as a class's method, "this." is assumed. Save yourself some carpal tunnel.
also, hitTestOject and hitTestPoint is ungodly slow. Just use math.

i'm not sure a single engine class is going to cut it, to be honest. It's a good idea, but you may wind up needing to split that up a little. Just a forewarning.

also, declare your private variables with a leading underscore to them so you can separate them out from public ones when you use them later.

the engine class should be handling collision. It should tell the player class to play an animation and where to be, and that's all the player class should be useful for. A visual and some private vars with public getters telling the engine where it is and what it's doing. Think of it like a puppet show, where the engine class is the puppet master.
Again, there should probably be three engines running your game. One for game logic (the "main" engine), one for the UI (interface, HUD, whatever you want to call it), and one for other game visuals such as backgrounds, platforms (assuming there are some), the player and enemies.

Thanks a bunch for the advice man. I come from an artist background and have much to learn about programming. So far I have a main class and engine class. Main looks out for my menu and engine looks out for my in game stuff. I will keep this awesome free info in mind for sure

also, why are events being dispatched in your game? They're very slow, is it really necessary?

I had no idea you can go about game making in flash without using events, perhaps I will keep this in mind for my next project

At 1/23/13 02:10 PM, SantoNinoDeCebu wrote: can you show the code of where the in engine class function 'public function playerAttack(event:Event):void' is called from.

is that event an ENTER_FRAME event?
public function spawnPlayer():void
		{
			player = new Player;
			addChild(player);
			player.x = 90;
			player.y = 180;
			
				addEventListener(Event.ENTER_FRAME, playerAttack);
		}

Yep it is an ENTER_FRAME event. The collision works fine and all, it's just the animation being a whiny bitch.

Thanks guys!

Response to Animation not playing on key (as3) 2013-01-25 12:14:10


At 1/24/13 03:14 AM, gameObject wrote: Yep it is an ENTER_FRAME event. The collision works fine and all, it's just the animation being a whiny bitch.

Thanks guys!

That's you're problem right there because the function is an ENTER_FRAME event it will be called once every single frame which means every frame you are repeatedly setting the animation to go to frame 'playerPunch'. Once the animation has started playing you will want to avoid calling gotoAndPlay until the animation is over.

Why don't you do something like:

public function playerAttack(object: MovieClip):void
{
	if((punchKeyDown) && (canPunch))
	{
		this.gotoAndPlay("playerPunch");
                // stops repeatedly playing the same animation frame
                canPunch=false;
	}
}

And whenever you detect the punchKey has been released you will want to set canPunch back to true, so you can punch again!

Response to Animation not playing on key (as3) 2013-01-29 04:35:27


At 1/25/13 12:14 PM, SantoNinoDeCebu wrote:
At 1/24/13 03:14 AM, gameObject wrote: Yep it is an ENTER_FRAME event. The collision works fine and all, it's just the animation being a whiny bitch.

Thanks guys!
That's you're problem right there because the function is an ENTER_FRAME event it will be called once every single frame which means every frame you are repeatedly setting the animation to go to frame 'playerPunch'. Once the animation has started playing you will want to avoid calling gotoAndPlay until the animation is over.

Why don't you do something like:

public function playerAttack(object: MovieClip):void
{
if((punchKeyDown) && (canPunch))
{
this.gotoAndPlay("playerPunch");
// stops repeatedly playing the same animation frame
canPunch=false;
}
}

And whenever you detect the punchKey has been released you will want to set canPunch back to true, so you can punch again!

Hate to be a shitty programmer but it still doesn't work.

public function playerAttack(object: MovieClip):void
		{
			if((punchKeyDown) && (canPunch))
			{
				
				this.gotoAndStop("playerPunch");
				canPunch = false;

				if(this.hitTestObject(object))
				{
					trace("Hit");
					object.takeHit(10);
				}
			}
                  }

The code I posted up earlier is from my engine class

//Engine
public function spawnPlayer():void
		{
			player = new Player;
			addChild(player);
			player.x = 90;
			player.y = 180;
			
			addEventListener(Event.ENTER_FRAME, playerAttack);
		}

		public function playerAttack(event:Event):void
		{
			player.playerAttack(enemy);
		}

And in my player class I've got the code above

public function playerAttack(object: MovieClip):void
		{
			if((punchKeyDown) && (canPunch))
			{
				
				this.gotoAndStop("playerPunch");
				canPunch = false;

				if(this.hitTestObject(object))
				{
					trace("Hit");
					object.takeHit(10);
				}
			}
                  }

sorry to have confused you.