Hey, I decided to make a tutorial like Denvish proposed a while ago (see As: Main : http://www.newgrounds.com/bbs/topic.php?id=229808).
What I'll teach is how to make your character move on terrain like in the following example :
http://img42.echo.cx/my.php?image=gravengine0gg.swf
Controls : Right and left arrow keys to move, up arrow key to jump.
Now this will be useful only for the floor in your games. If you wish to make platforms, you'll have to use another script.
So, let's get started :
The hero movie clip. Create a new movie clip and draw your hero. Now, align it so the hero's feet are in the center of the movie clip (see pic. 1).
Now, make an invisible movie clip a bit above the center of the movie clip (so a bit above the feet). Name this movie clip "GoUP" (withouth the quotes). Then, make another invisible MC under "GoUP" and name it "Feet". See pic. 2 for result (note : I made the movie clips visible for you to see how it looks).
Ok, go back to your main timeline. We'll now script the character movement. Click your hero and give it this script :
onClipEvent(load){
xSpeed=0
ySpeed=0
//the speed for the x and y axises
maxSpeed=10
accel=1
jumpHeight=15
//character's stats
jump=1
//a variable to tell wether the char. is jumping or not
}
onClipEvent(enterFrame){
Right=Key.isDown(Key.RIGHT)
Left=Key.isDown(Key.LEFT)
Down=Key.isDown(Key.DOWN)
Up=Key.isDown(Key.UP)
//just so we don't have to type if(Key.isDown(Key.UP)) each time
_x+=xSpeed
_y+=ySpeed
//add speed variables to current position
if(Right&&xSpeed<maxSpeed){
if(jump==0){
xSpeed+=accel
//if the right arrow key is down and the hero is not jumping, add "accel" (1) to the speed
}else{
xSpeed+=accel/5
//if he is jumping, add 1/5 of accel so air control is not as good as ground control
}
}else if(Left&&xSpeed>-maxSpeed){
if(jump==0){
xSpeed-=accel
}else{
xSpeed-=accel/5
}
//same for the left key, but the right key has to be up
}else if(jump==0){
xSpeed/=1.5
if(xSpeed<0.1&&xSpeed>-0.1){
xSpeed=0
}
//if both right and left keys are up, slow down
}else if(jump==1){
xSpeed/=1.05
//same but when jumping
}
ySpeed++
//make it so ySpeed always increases (gravity)
if(jump==0&&Up){
ySpeed=-jumpHeight
jump=1
//if the char. is not jumping and you hit the up key, make ySpeed the jump height
}
if(ySpeed>1){
jump=1
//if ySpeed is bigger than one, the char. is jumping
}
}
Now your character should move correctly, but free fall and go off stage. We'll now add the ground. Draw some wavy terrain (but not too wavy or it might not work) and convert it to a movie clip. See pic. 3 for example. Finally, give your hero the instance name "Hero".
Let’s script this, click your ground and add in the actions panel :
onClipEvent(enterFrame){
if(_root.Hero.ySpeed>=0){
//run this script only if the hero is falling
while(this.hitTest(_root.Hero._x+_root.Hero.GoUP._x, _root.Hero._y+_root.Hero.GoUP._y, true)){
//while actions will run until they can't anymore
//this one will tell if the movie clip is touching the GoUP
_root.Hero._y--
//while the ground touches GoUP, make the hero go up
_root.Hero.ySpeed=0
_root.Hero.jump=0
//set the hero's xspeed to 0 and make it so he's not jumping
}
if(this.hitTest(_root.Hero._x+_root.Hero.Feet._x, _root.Hero._y+_root.Hero.Feet._y, true)){
//this if will check if the hero's feet are touching this MC
_root.Hero.ySpeed=0
_root.Hero.jump=0
//set the hero's xspeed to 0 and make it so he's not jumping
//we already typed that but it's only to be sure it'll work
}
}
}
Now you have your hero and ground working. Unfortunately, this script won’t work with floating platforms, so we’ll have to use another script for those. Draw one and convert it to a movie clip. The top of the platform must be in the center of the movie clip (just like for the hero’s feet, but now the platform has to be below it’s movie clip’s center). Return to the main timeline and give this script to the platform :
onClipEvent(enterFrame){
if(this.hitTest(_root.Hero.GoUP) or this.hitTest(_root.Hero.Feet)){
//run the script if this touches one of the invisible MC's in the hero
if(_root.Hero.ySpeed>0){
//will work only if hero is falling
_root.Hero.ySpeed=0
_root.Hero.jump=0
//stop the hero from falling
_root.Hero._y=_y
//set the hero's _y to this _y
}
}
}
And voilà ! Here’s your basic platformer engine. I did my best to explain everything but there still might be stuff hard to understand. If so, just tell me and I’ll help you.
Have fun