Be a Supporter!

Animation not playing on key (as3)

  • 562 Views
  • 7 Replies
New Topic Respond to this Topic
gameObject
gameObject
  • Member since: Jan. 4, 2012
  • Offline.
Forum Stats
Member
Level 03
Game Developer
Animation not playing on key (as3) Jan. 20th, 2013 @ 04:29 AM Reply

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");
			}
		}
SantoNinoDeCebu
SantoNinoDeCebu
  • Member since: Jul. 20, 2002
  • Offline.
Forum Stats
Supporter
Level 32
Programmer
Response to Animation not playing on key (as3) Jan. 20th, 2013 @ 06:40 PM Reply

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.

gameObject
gameObject
  • Member since: Jan. 4, 2012
  • Offline.
Forum Stats
Member
Level 03
Game Developer
Response to Animation not playing on key (as3) Jan. 23rd, 2013 @ 04:25 AM Reply

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

egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Member
Level 05
Game Developer
Response to Animation not playing on key (as3) Jan. 23rd, 2013 @ 10:41 AM Reply

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
SantoNinoDeCebu
SantoNinoDeCebu
  • Member since: Jul. 20, 2002
  • Offline.
Forum Stats
Supporter
Level 32
Programmer
Response to Animation not playing on key (as3) Jan. 23rd, 2013 @ 02:10 PM Reply

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?

gameObject
gameObject
  • Member since: Jan. 4, 2012
  • Offline.
Forum Stats
Member
Level 03
Game Developer
Response to Animation not playing on key (as3) Jan. 24th, 2013 @ 03:14 AM Reply

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!

SantoNinoDeCebu
SantoNinoDeCebu
  • Member since: Jul. 20, 2002
  • Offline.
Forum Stats
Supporter
Level 32
Programmer
Response to Animation not playing on key (as3) Jan. 25th, 2013 @ 12:14 PM Reply

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!

gameObject
gameObject
  • Member since: Jan. 4, 2012
  • Offline.
Forum Stats
Member
Level 03
Game Developer
Response to Animation not playing on key (as3) Jan. 29th, 2013 @ 04:35 AM Reply

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.