Be a Supporter!

As: Movement on slopes

  • 12,165 Views
  • 53 Replies
New Topic Respond to this Topic
Joelasticot
Joelasticot
  • Member since: Feb. 14, 2003
  • Offline.
Forum Stats
Supporter
Level 46
Game Developer
As: Movement on slopes Jun. 23rd, 2005 @ 12:16 PM Reply

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
Toast
Toast
  • Member since: Apr. 2, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to As: Movement on slopes Jun. 23rd, 2005 @ 12:21 PM Reply

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
Joelasticot
Joelasticot
  • Member since: Feb. 14, 2003
  • Offline.
Forum Stats
Supporter
Level 46
Game Developer
Response to As: Movement on slopes Jun. 23rd, 2005 @ 12:23 PM Reply

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
Toast
Toast
  • Member since: Apr. 2, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to As: Movement on slopes Jun. 23rd, 2005 @ 12:23 PM Reply

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
Toast
Toast
  • Member since: Apr. 2, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to As: Movement on slopes Jun. 23rd, 2005 @ 12:25 PM Reply

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
Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to As: Movement on slopes Jun. 23rd, 2005 @ 01:55 PM Reply

Nice work mate...

Brilliant code. Gave me inspiration


- Matt, Rustyarcade.com

Starogre
Starogre
  • Member since: May. 8, 2004
  • Offline.
Forum Stats
Member
Level 18
Artist
Response to As: Movement on slopes Jun. 30th, 2005 @ 10:58 AM Reply

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
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to As: Movement on slopes Jul. 29th, 2005 @ 10:22 AM Reply

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
dieEVILsanta
dieEVILsanta
  • Member since: May. 26, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to As: Movement on slopes Aug. 9th, 2005 @ 11:37 PM Reply

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

GeoKureli
GeoKureli
  • Member since: Apr. 1, 2003
  • Offline.
Forum Stats
Supporter
Level 19
Game Developer
Response to As: Movement on slopes Aug. 10th, 2005 @ 12:26 AM Reply

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

JCesta
JCesta
  • Member since: Jul. 8, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to As: Movement on slopes Sep. 19th, 2005 @ 09:27 PM Reply

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

JCesta
JCesta
  • Member since: Jul. 8, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to As: Movement on slopes Sep. 19th, 2005 @ 09:37 PM Reply

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

Joelasticot
Joelasticot
  • Member since: Feb. 14, 2003
  • Offline.
Forum Stats
Supporter
Level 46
Game Developer
Response to As: Movement on slopes Sep. 19th, 2005 @ 09:41 PM Reply

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
Hominind
Hominind
  • Member since: Mar. 1, 2004
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to As: Movement on slopes Sep. 20th, 2005 @ 12:25 AM Reply

Wow impressive code lines

GringoMan
GringoMan
  • Member since: Aug. 18, 2005
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to As: Movement on slopes Sep. 25th, 2005 @ 11:42 AM Reply

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.

GringoMan
GringoMan
  • Member since: Aug. 18, 2005
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to As: Movement on slopes Sep. 25th, 2005 @ 01:19 PM Reply

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

GringoMan
GringoMan
  • Member since: Aug. 18, 2005
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to As: Movement on slopes Sep. 25th, 2005 @ 01:47 PM Reply

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.

GuyWithHisComp
GuyWithHisComp
  • Member since: Nov. 10, 2005
  • Offline.
Forum Stats
Member
Level 28
Blank Slate
Response to As: Movement on slopes Dec. 19th, 2005 @ 01:31 PM Reply

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


BBS Signature
Zwickel
Zwickel
  • Member since: Jul. 21, 2003
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to As: Movement on slopes Jan. 2nd, 2006 @ 01:25 AM Reply

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

dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to As: Movement on slopes Jan. 2nd, 2006 @ 05:17 AM Reply

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
Landlad
Landlad
  • Member since: Dec. 28, 2005
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to As: Movement on slopes Jan. 2nd, 2006 @ 06:26 PM Reply

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

Rabbi-Indie-The-Rad
Rabbi-Indie-The-Rad
  • Member since: Dec. 4, 2005
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to As: Movement on slopes Jan. 12th, 2006 @ 05:29 PM Reply

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?

PolishMike
PolishMike
  • Member since: Jan. 3, 2005
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to As: Movement on slopes Feb. 18th, 2006 @ 11:53 PM Reply

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?

Toast
Toast
  • Member since: Apr. 2, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to As: Movement on slopes Feb. 19th, 2006 @ 05:21 AM Reply

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


BBS Signature
T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to As: Movement on slopes Feb. 19th, 2006 @ 05:22 AM Reply

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.

iameatingjam
iameatingjam
  • Member since: Jan. 2, 2005
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to As: Movement on slopes Feb. 19th, 2006 @ 05:40 AM Reply

pretty 1337 code you got there, it fwnz mine.

dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to As: Movement on slopes Feb. 19th, 2006 @ 05:43 AM Reply

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
Rantzien
Rantzien
  • Member since: Jan. 27, 2005
  • Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to As: Movement on slopes Feb. 19th, 2006 @ 07:16 AM Reply

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
FlashKid
FlashKid
  • Member since: Nov. 22, 2003
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to As: Movement on slopes Feb. 19th, 2006 @ 07:25 AM Reply

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.

MARINO
MARINO
  • Member since: Feb. 21, 2006
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to As: Movement on slopes Feb. 22nd, 2006 @ 11:03 AM Reply

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