Be a Supporter!

AS2 Moving animation problem.

  • 368 Views
  • 8 Replies
New Topic Respond to this Topic
WhaleMan
WhaleMan
  • Member since: Aug. 20, 2012
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
AS2 Moving animation problem. 2013-02-06 11:54:23 Reply

I really need help i have tried to fix this problem and tried a lot of different solutions but none of them working.
I have this code for my character:

onClipEvent (enterFrame) {
radius =  _width/2;
if(Key.isDown(Key.UP)){
this._y -= 10;
this.head.gotoAndStop(3);
this.gotoAndPlay(2);
}
if(!Key.isDown(Key.UP)){
	this.gotoAndStop(1);
}

if(Key.isDown(Key.DOWN)){
this._y += 10;
this.head.gotoAndStop(1);
this.gotoAndPlay(2);
} 
if(!Key.isDown(Key.DOWN)){
	this.gotoAndStop(1);
}

if(Key.isDown(Key.LEFT)){
this._x -= 10;
this.head.gotoAndStop(2);
this.gotoAndPlay(2);
} 
if(!Key.isDown(Key.LEFT)){
	this.gotoAndStop(1);
}

if(Key.isDown(Key.RIGHT)){
this._x += 10;
this.head.gotoAndStop(4);
this.gotoAndPlay(2);
}
if(!Key.isDown(Key.RIGHT)){
	this.gotoAndStop(1);
}

while (_root.wall.hitTest (this._x, this._y+radius, true)){
this._y--;
}
while (_root.wall.hitTest (this._x, this._y-radius, true)){
this._y++;
}
while (_root.wall.hitTest (this._x-radius, this._y, true)){
this._x++;
}
while (_root.wall.hitTest (this._x+radius, this._y, true)){
this._x--;
}
}

I can move the char. around and all that but the walking animation won't play.
Any one help with this problem.
Thanks.

egg82
egg82
  • Member since: Jun. 24, 2006
  • Online!
Forum Stats
Supporter
Level 05
Game Developer
Response to AS2 Moving animation problem. 2013-02-06 12:31:07 Reply

looking at this code certainly takes me back.

do yourself a favor and learn AS3.


Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P

BBS Signature
Mattster
Mattster
  • Member since: Nov. 21, 2007
  • Offline.
Forum Stats
Member
Level 20
Game Developer
Response to AS2 Moving animation problem. 2013-02-06 12:36:05 Reply

Your issue is likely that you are telling your clip to gotoAndPlay every frame. A quick fix would be to add labels to your animations, and check if (this.currentLabel != "animationLabelName"). That way it will notice that it's playing the animation, and will not reset it each time.

At 2/6/13 12:31 PM, egg82 wrote: do yourself a favor and learn AS3.

I second this comment.

WhaleMan
WhaleMan
  • Member since: Aug. 20, 2012
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to AS2 Moving animation problem. 2013-02-06 13:59:08 Reply

At 2/6/13 12:36 PM, Mattster wrote: Your issue is likely that you are telling your clip to gotoAndPlay every frame. A quick fix would be to add labels to your animations, and check if (this.currentLabel != "animationLabelName"). That way it will notice that it's playing the animation, and will not reset it each time.

Can't get it to work.

Spysociety
Spysociety
  • Member since: Dec. 30, 2009
  • Offline.
Forum Stats
Member
Level 21
Blank Slate
Response to AS2 Moving animation problem. 2013-02-06 14:57:54 Reply

At 2/6/13 01:59 PM, WhaleMan wrote:
At 2/6/13 12:36 PM, Mattster wrote: Your issue is likely that you are telling your clip to gotoAndPlay every frame. A quick fix would be to add labels to your animations, and check if (this.currentLabel != "animationLabelName"). That way it will notice that it's playing the animation, and will not reset it each time.
Can't get it to work.

Of course, in AS1 there is no currentLabel property, you have to set your own, AS3 already give you that. Another motive which you should change to AS3.

WhaleMan
WhaleMan
  • Member since: Aug. 20, 2012
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to AS2 Moving animation problem. 2013-02-06 15:07:13 Reply

At 2/6/13 02:57 PM, Spysociety wrote:
Of course, in AS1 there is no currentLabel property, you have to set your own, AS3 already give you that. Another motive which you should change to AS3.

I don't want to change to as3 now

egg82
egg82
  • Member since: Jun. 24, 2006
  • Online!
Forum Stats
Supporter
Level 05
Game Developer
Response to AS2 Moving animation problem. 2013-02-06 15:12:34 Reply

At 2/6/13 03:07 PM, WhaleMan wrote: I don't want to change to as3 now

peer pressure says you will.


Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P

BBS Signature
Tree-SkyLark-BCE
Tree-SkyLark-BCE
  • Member since: Aug. 6, 2005
  • Offline.
Forum Stats
Member
Level 35
Programmer
Response to AS2 Moving animation problem. 2013-02-06 18:11:52 Reply

At 2/6/13 11:54 AM, WhaleMan wrote: I really need help i have tried to fix this problem and tried a lot of different solutions but none of them working.
I can move the char. around and all that but the walking animation won't play.
Any one help with this problem.
Thanks.

You need manage the state and fix your key logic. When holding down the an arrow key, it will gotoAndPlay(2) every frame, effectively stopping the animation from ever getting past the first frame. In order to fix this, create a boolean variable to represent whether you are already playing the animation. However, even if you were doing this, the animation would only play when you are holding down the right arrow key because you have a condition that takes the clip back to the first frame if the right key is not down. You could check whether any of the keys are down or just flip another boolean if one is down. Also, instead of checking whether something is false after you just checked if is true, you can use an else statement.

onClipEvent(load)
{
	var standing:Boolean = true;
	var keysDown:Boolean = false;
}
onClipEvent (enterFrame) {
	radius =  _width/2;
	keysDown = false;
	if(Key.isDown(Key.UP)){
		keysDown = true;
		this._y -= 10;
		this.head.gotoAndStop(3);
		if (standing){
			gotoAndPlay(2);
			standing = false;
		}
	}
	if(Key.isDown(Key.DOWN)){
		keysDown = true;
		this._y += 10;
		this.head.gotoAndStop(1);
		if (standing){
			gotoAndPlay(2);
			standing = false;
		}
	}
	if(Key.isDown(Key.LEFT)){
		keysDown = true;
		this._x -= 10;
		this.head.gotoAndStop(2);
		if (standing){
			gotoAndPlay(2);
			standing = false;
		}
	}
	if(Key.isDown(Key.RIGHT)){
		keysDown = true;
		this._x += 10;
		this.head.gotoAndStop(4);
		if (standing){
			gotoAndPlay(2);
			standing = false;
		}
	}
	if (!keysDown)
	{
		this.gotoAndStop(1);
		standing = true;
	}
	
	while (_root.wall.hitTest (this._x, this._y+radius, true)){
		this._y--;
	}
	while (_root.wall.hitTest (this._x, this._y-radius, true)){
		this._y++;
	}
	while (_root.wall.hitTest (this._x-radius, this._y, true)){
		this._x++;
	}
	while (_root.wall.hitTest (this._x+radius, this._y, true)){
		this._x--;
	}
}

BBS Signature
WhaleMan
WhaleMan
  • Member since: Aug. 20, 2012
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to AS2 Moving animation problem. 2013-02-07 12:59:25 Reply

At 2/6/13 06:11 PM, Tree-SkyLark-BCE wrote: You need manage the state and fix your key logic. When holding down the an arrow key, it will gotoAndPlay(2) every frame,

Thank you it really helped and got it to work with some tweaks.