PDA

View Full Version : Platformer; Time-based animation states - please advise me!



stillwired
October 21st, 2010, 02:40 AM
I've been working on my first game with the help of a book (AS3 Game Programming University for those familiar), and I've been stuck on this issue for a while now. I've tried searching high and low for the answer but I'm at a loss.

Basically I worked through my book's chapter on platformers and now I'm trying to make my own improvements to it, specifically the character's jump and stand animations. It's a little trickier for me because it's time-based animation and I have one mc for the hero with the frames labeled for each animation state (walk, jump, etc.), but when I tried to figure out how to add the necessary code to play a jump sequence instead of a single frame jump it gets hairy. The character actually does the jump animation sometimes but sometimes he just jumps and stays in the stand position..

I don't have any compiler errors but I get the #1010 error when I run the movie, which I found out has something to do with referencing an array or having an incorrect instance name?

TypeError: Error #1010: A term is undefined and has no properties.
at PlatformGame/moveCharacter()[D:\PlatformGame.as:310]
at PlatformGame/moveEnemies()[D:\PlatformGame.as:179]
at PlatformGame/gameLoop()[D:\PlatformGame.as:168]

I checked and I believe the array I set up to hold the frame numbers that belong to the jump animation is correct, since it's just like the working walkAnimation array, but like I said I'm at a loss.


hero.walkAnimation=new Array(2,3,4,5,6,7,8,9,10,11,12);
hero.jumpAnimation=new Array(13,14,15,16,17,18,19,20,21,22,23);


It must have something to do with me writing the code for the jump animation incorrectly, so here's the relevant code from the moveCharacter function, with my suspicious code in blue:


// set animation state
if (char.inAir) { newAnimState = "jump";}

char.animstate=newAnimState;

// move along walk cycle
if (char.animstate=="walk") {
char.animstep+=timeDiff/60;
if (char.animstep>char.walkAnimation.length) {
char.animstep=1;
}
char.mc.gotoAndStop(char.walkAnimation[Math.floor(char.animstep)]);

// not walking, show stand or jump state
} else {
char.mc.gotoAndStop(char.animstate);

{
if (char.animstate=="jump") {
char.animstep += timeDiff / 60;
if (char.animstep>char.jumpAnimation.length) {
char.animstep=13;
}
char.mc.gotoAndPlay(char.jumpAnimation[Math.floor(char.animstep)]);

}
}

// changed direction
if (newDirection!=char.direction) {
char.direction=newDirection;
char.mc.scaleX=char.direction;
}

}

}


I'd really appreciate it if someone could school me on this, I pretty much understand how time-based animation works, but I don't have a lot of experience yet. Help me out please, I'm beggin' ya!

bluemagica
October 21st, 2010, 02:20 PM
Please use "
[/ code]" tags to post your code....
I think
[code]
char.animstep=13;
will be
char.animstep=1;

cause to me it seems, the animstep thing is used to wrap and move around the array containing the frames.

stillwired
October 21st, 2010, 04:39 PM
Please use "
[/ code]" tags to post your code....
I think
[code]
char.animstep=13;
will be
char.animstep=1;
cause to me it seems, the animstep thing is used to wrap and move around the array containing the frames.

Yeah, because the walkAnimation array seems to work fine and I think you're right it should be 1. I thought I was supposed to put the first frame number in the jumpAnimation array for it to reset to which is 13. Anyways, I tried that but it must not have been the problem. Still getting the 1010 error, and for some reason setting the animstep to 1 broke the ability to change direction the character is facing while walking. It only flips him if I press the opposite direction in mid-air, otherwise he moonwalks. I tried moving the jump code to after the newDirection stuff but it didn't matter. Any thoughts?

bluemagica
October 21st, 2010, 09:48 PM
where are you setting the character state to walking? I don't really see that code in there....

And if you don't mind, post your fla.

stillwired
October 22nd, 2010, 06:41 AM
where are you setting the character state to walking? I don't really see that code in there....

And if you don't mind, post your fla.


Alright, looks like the issue with the frames jumping around resolved now.

So really I just need to figure out why I'm getting the #1010 Error.

Here's a link for the files, tell me what you think:

http://www.megaupload.com/?d=L5RR30G5

bluemagica
October 22nd, 2010, 07:22 AM
Your problem is happening cause you are assuming every character has a jumpAnimation property, but enemies don't have that property(array), do they? there are many ways to solve this, like checking if the character is of enemy type before executing jumping routine. Or you can simply set it's jumpAnimation property to null, and in the moveCharacter function check if(char.jumpAnimation!=null) before proceeding.

however normally, I inherit my players and enemies from a base character class so that they have similar properties and methods, and then I keep an vector(array) of states available to them, so in code I can easily use only those states which are available to that character.

stillwired
October 22nd, 2010, 10:23 PM
You're right! I can't thank you enough, I understand what was going on now. So glad I won't have to pull out the rest of my hair now.