Platform Game Preview(my upcoming game)
I know...Atomic Sponge made one...my turn :D
Ok,first I'll start with anything before the coding.
Make your character.Make him/her/it into a MovieClip.Make sure the Center of your MovieClip is in between your characters legs.Use Onion Skinning to do the same for the next frames in the MovieClip.On your first frame,add a stop action.Make sure your character is there .Add another frame.Add a MovieClip of your character running,along with a stop action.Now add one last frame.This frame is your character in a jumping position.It doesn't have to be a MovieClip or anything,but do what you want.
Next,you need to create Platforms.Either way,the Platforms need to be a MovieClip.You can animate them if you want.It doesn't matter how many Platforms you have,they all belong in the same MovieClip per lvl of your game.Outside of your Platforms on the selected scene,add the instance name of "Platforms" to your Platforms so the hitTest on your character works.Of course there are no quotes.Also remember that your Platforms eventually should be covered up by a real Platform part,as in a Mario walking thingy or whatever.I suggest always putting your Platforms that your character is on to be on the very bottom layer behind the background,platform pictures,and whatever so they aren't seen.
Now I'll start off with the coding variables and such.
onClipEvent (load) {
gravity = 10;
walkSpeed = 6;
maxJump = 4;
scale = _xscale;
}
The (load) onClipEvent is always needed for setting variables.Now then...
Gravity...this will set how much Gravity is in your character.10 will have to be the Gravity for the code to work right.
walkSpeed...this tells how fast or slow your character will walk.6 is an average walk rate.
maxJump...this is part of the jumping script area in the full code.Set it to whatever.
scale and _xscale...this will make your character change direction depending on which key you use.How does this variable work?It tells that the scale variable you are using is _xscale rather than _yscale.I'm actually not sure whether or not it's needed in the variables...ok then,next part.
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
_xscale = -scale;
_x -= walkSpeed;
this.gotoAndStop(2);
}
if (Key.isDown(Key.RIGHT)) {
_xscale = +scale;
_x += walkSpeed;
this.gotoAndStop(2);
}
The (enterFrame) onClipEvent is always used for hitTest or arrow movment.There are also other uses that we won't need in this tutorial.
First of all,the if(Key.isDown(Key.set)) will be the "if" for pressing a Key down.If your smart enough,you'll have figured out that when there's an "if",there's a "then".So you see all that's past the second { in that code?That's the "then" part._xscale = -scale will make your character turn to the Left while the Key is down._xscale = +scale is the same thing,just for the Right._x -= walkSpeed is setting what direction the character will go with the speed.You notice that "-" on the walk part is for Left.Everything thing above is the same for Right,except opposite.The gotoAndStop(2); code makes your character go to the second frame,which if you remember,is the frame for running/walking.It's the same for both since both codes lead to the 2nd frame.On to the next part of this code...
if (air == true) {
_y += gravity;
}
}
onClipEvent (enterFrame) {
if (_root.platforms.hitTest(_x, _y, true)) {
air = false;
} else {
air = true;
}
This area is for the gravity.The first "if" part is so the gravity will start when your character is in the air._y += gravity; will make your move down automatically,almost like the Key.isDown code,except this time,there is no Key actions.The next part is for landing on your platforms.As you can see,a hitTest is started.You also see the word "Platforms" in there.There really is a use for that instance .The "then" part of this hitTest makes it so air = false;,which means that the character should stop falling.The "else" is kinda like an "if",but shorter.It's a continuing "if" for incase the character is in the air,which makes the possibility of even being in the air possible!(Confusing,eh? )Ok,we'll stop that now.As a conclusion to Platforms,you need to keep this in your head....make sure every Platform is located in 1(just ONE!)single MovieClip per lvl.For example,create your rectangle Platforms with the Rec. Tool,then convert them all into on MC.This goes for every lvl(platformslvl1.1,platformslvl1.2,etc etc)Let's continue...
if (Key.isDown(Key.UP) && jump == true) {
_y -= jumpSpeed;
gotoAndStop(3);
}
if (air == false) {
jump = true;
jumpcount = 0;
jumpSpeed = 22;
}
if (Key.isDown(Key.UP)) {
jumpcount += 1;
this.gotoAndStop(3);
}
if (jumpcount>maxjump && jumpSpeed>-2) {
jumpSpeed -= 2;
}
}
I bet you were wondering about the jump...ok,we have another Key.isDown code.Except this time it's for going "Up".You notice the "&&" part of this code.This is also like a continue of the "if",except for it's for 2 "if"s at once...and you should know that _y - =jumpSpeed will make your character jump into the air at that variable rate you set earlier.Then here is a toughy...this is almost like a new variable setter,but it's with an "if" function.This is for when your character is on the Platform.It makes it so you may jump(jump = true),jumpcount = 0;(no more jumping),and jumpSpeed = 22(also no more jumping).Then the next part.Jumpcount += 1.This raises 0 to 1,which is part of the jumping and falling.Kinda makes it realistic...not just jump and suddenly fall.Jump,slow down,then fall...and finally,a part for actually jumping.this.gotoAndStop(3); will make your character go to jumping position.Sorry about some bugs in the jumping part.The rest is all part of the slowdown process I mentioned a few seconds ago.It will set the jumpSpeed down 2 so falling is in act.On we go...
onClipEvent (keyUp) {
this.gotoAndStop(1);
walkSpeed = 6;
}
The (keyUp) onClipEvent...Simple eh.This makes it so when any key pops back up on your keyboard,the character will go back to frame 1,which is the standing frame.(This is one part of my jumping bug).walkSpeed = 6; is just a part for if you add something to make your character faster during walk...whatever.Now I will add a few extra things for making a platform game.
<<<Read Next Post>>>