00:00
00:00

As: Movement on slopes

13,984 Views | 53 Replies
New Topic Respond to this Topic

As: Movement on slopes 2005-06-23 12:16:57


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

As: Movement on slopes


ey

BBS Signature

Response to As: Movement on slopes 2005-06-23 12:21:34


Wow!Nice job on that,Joe..(Happy level 13 XD)
The code itself was amazing but it would be better if you explained every line of code..
Learn more about simple physics.


BBS Signature

Response to As: Movement on slopes 2005-06-23 12:23:05


At 6/23/05 12:21 PM, Dark_Toaster wrote: Wow!Nice job on that,Joe..(Happy level 13 XD)
The code itself was amazing but it would be better if you explained every line of code..
Learn more about simple physics.

Yeah well I just explained what the codes did in overall... Anyway I didn't have much characters remaining! Oh and thanks for noticing I'm now level 13


ey

BBS Signature

Response to As: Movement on slopes 2005-06-23 12:23:41


At 6/23/05 12:21 PM, Dark_Toaster wrote: The code itself was amazing but it would be better if you explained every line of code..

oops,sorry,I didn't notice the explanations in the code...(//stuff)


BBS Signature

Response to As: Movement on slopes 2005-06-23 12:25:46


At 6/23/05 12:23 PM, Joelasticot wrote:
At 6/23/05 12:21 PM, Dark_Toaster wrote: Wow!Nice job on that,Joe..(Happy level 13 XD)
The code itself was amazing but it would be better if you explained every line of code..
Learn more about simple physics.
Anyway I didn't have much characters remaining! Oh and thanks for noticing I'm now level 13

Haha,it happened to me to in my physics tutoria,I had to double post in order to finish the tutorial ^_^


BBS Signature

Response to As: Movement on slopes 2005-06-23 13:55:48


Nice work mate...

Brilliant code. Gave me inspiration


- Matt, Rustyarcade.com

Response to As: Movement on slopes 2005-06-30 10:58:57


This was very good! I recommend this for all n00b programmers :P (NO COPYING AND PASTING, read all the explanations first!)


the events are merely fictional, written, directed, and acted out by all who create them

BBS Signature

Response to As: Movement on slopes 2005-07-29 10:22:26


A few comments: dont use a while loop: just use a for loop of lets say 10 max this way it restricts the steepness of slope the player can walk up and stops the player floating up a wall sorta speak: youll also need a safe guard that if it cant reach the top of the ground before the loop ends to not alloow it to move otherwise the player walks through walls:

you can also make a more realistic looking walk by doing the same again but down so that the player actually walks down the slopes not bounces down, again a safe guard but this time it only means if he doesnt reach the ground before the loop ends that he is floating in the air ans is falling/jumping


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature

Response to As: Movement on slopes 2005-08-09 23:37:07


wow, thx a lot. i actually understood it. i would have never thought of that, partly cas i suck.

Response to As: Movement on slopes 2005-08-10 00:26:39


wow you learn something every day, i knew of while() but i never knew it repeated itself until untrue. i thought it was just another if statement. thanks


This blog I made | This game sucks | I, twit

Just trying to get enough Twitter followers to quit Facebook forever

Response to As: Movement on slopes 2005-09-19 21:27:51


how do you make an "invisible" movie clip???

Response to As: Movement on slopes 2005-09-19 21:37:49


nm i fixed it (a box wid an alpha of 0) but my guy when straight through the ground!!!

Response to As: Movement on slopes 2005-09-19 21:41:51


At 9/19/05 09:37 PM, JCesta wrote: nm i fixed it (a box wid an alpha of 0) but my guy when straight through the ground!!!

Are you sure your instance names are correct? They're CaSe SeNsItIvE.


ey

BBS Signature

Response to As: Movement on slopes 2005-09-20 00:25:19


Wow impressive code lines

Response to As: Movement on slopes 2005-09-25 11:42:10


quick question though...say i have movement so when it moves it doesn't just move across screen without doing anything. what coding would i need to continue that? like i'll have a standing still part in the movie clip and than have it so when the button is clicked it goes into the moving part.

Response to As: Movement on slopes 2005-09-25 13:19:04


oie....the ground cli won't work but instead of a line i made it an image from the background will that work?

Response to As: Movement on slopes 2005-09-25 13:47:07


I feel as if no one cares. lol I got that much to work but my first problem is still there...If i can figure it out i guess I can post if or future users.

Response to As: Movement on slopes 2005-12-19 13:31:51


Quick note: If you only make a small line the character will go throught it. You got to have a bigger ground.


BBS Signature

Response to As: Movement on slopes 2006-01-02 01:25:30


Thanks a lot!!!
That actually helped me.
Im a f..ing noob in AS.

Response to As: Movement on slopes 2006-01-02 05:17:54


At 12/19/05 01:31 PM, -Snail- wrote: Quick note: If you only make a small line the character will go throught it. You got to have a bigger ground.

unless that is you do a bit of ray tracing to trace the movement of the character across its displacement unit by unit, that way it can detect collision with terrain that from one frame tot he next its completely passed through


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature

Response to As: Movement on slopes 2006-01-02 18:26:25


i liked the tut. But i want to create a bmx type game where you move around on a bmx basically. I used your tut to create 1 but the bike doesnt moved to the ground it stays horizontal. i was wondering if you know how to make it it move to ground. thnx

Response to As: Movement on slopes 2006-01-12 17:29:09


At 7/29/05 10:22 AM, -dELta- wrote: A few comments: dont use a while loop: just use a for loop of lets say 10 max this way it restricts the steepness of slope the player can walk up and stops the player floating up a wall sorta speak: youll also need a safe guard that if it cant reach the top of the ground before the loop ends to not alloow it to move otherwise the player walks through walls:

you can also make a more realistic looking walk by doing the same again but down so that the player actually walks down the slopes not bounces down, again a safe guard but this time it only means if he doesnt reach the ground before the loop ends that he is floating in the air ans is falling/jumping

I noticed that problem. But I am unsure of where and how to edit the loop :-(.

I tried making a game with this code where a portion of the level is sort of underground, if you walk to close to a wall you appear ontop of the entire level.

can any body help me out on this?

Response to As: Movement on slopes 2006-02-18 23:53:36


Great. Now say i wanted to make a boundary for this char. so it doesnt go off stage... how would i do it for this sloping platform?

Response to As: Movement on slopes 2006-02-19 05:21:55


Argh, I hate it when people bump old threads... Don't look at my old posts! :P


BBS Signature

Response to As: Movement on slopes 2006-02-19 05:22:53


At 2/18/06 11:53 PM, PolishMike wrote: Great. Now say i wanted to make a boundary for this char. so it doesnt go off stage... how would i do it for this sloping platform?

make another "boundry" mc, and set up some more while loops to puch the character's _x away from them.

Response to As: Movement on slopes 2006-02-19 05:40:30


pretty 1337 code you got there, it fwnz mine.

Response to As: Movement on slopes 2006-02-19 05:43:55


At 1/12/06 05:29 PM, Patch_The_Rad wrote:
At 7/29/05 10:22 AM, -dELta- wrote: stuff
I noticed that problem. But I am unsure of where and how to edit the loop :-(.

I tried making a game with this code where a portion of the level is sort of underground, if you walk to close to a wall you appear ontop of the entire level.

can any body help me out on this?

my version of the loop will stop that happening, and for the roof you can add another collision point and treat it seperately from the feet so that it pushes the player down


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature

Response to As: Movement on slopes 2006-02-19 07:16:05


When you have more "wavy" terrains than in the example you might want to test several points on the outline of your character to have a more realistic procession. The more points, the slower procession but also better simulation.

4 points are enough in most cases if you don't need an uber realistic engine. Here is a sample that uses 4 points and most of dELta's earlier mentioned tips.


BBS Signature

Response to As: Movement on slopes 2006-02-19 07:25:34


I'm using this script for a game of mine, I understand it now, but I need it all to be inside a "world" movie clip, problem with that is I have to change all the _root.hero to _root.world.hero and there's ALOT of those, I've tried twice and I swear I got all of the done and it still doesn't work, maybe sticking it all inside an MC has different problem aswell, just don't know what it is.


Your path is dark.

Response to As: Movement on slopes 2006-02-22 11:03:07


Damn. I hate being a worthless newbie andhaving good tutorials not work for me.