Forum Topic: As: Movement on slopes

(9,925 views • 52 replies)

This topic is 2 pages long. [ 1 | 2 ]

<< < > >>
None

Joelasticot

Reply To Post Reply & Quote

Posted at: 6/23/05 12:16 PM

Joelasticot EVIL LEVEL 37

Sign-Up: 02/14/03

Posts: 867

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

var s=0;var p=0;addEventListener("enterFrame", function(){
s+=(25-p)/20;p+=Math.ceil(s);var o="";
for(var i=0;i<p;i++){o+=" ";};trace(o+"joelasticot");})

BBS Signature

None

Toast

Reply To Post Reply & Quote

Posted at: 6/23/05 12:21 PM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,914

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.


None

Joelasticot

Reply To Post Reply & Quote

Posted at: 6/23/05 12:23 PM

Joelasticot EVIL LEVEL 37

Sign-Up: 02/14/03

Posts: 867

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

var s=0;var p=0;addEventListener("enterFrame", function(){
s+=(25-p)/20;p+=Math.ceil(s);var o="";
for(var i=0;i<p;i++){o+=" ";};trace(o+"joelasticot");})

BBS Signature

None

Toast

Reply To Post Reply & Quote

Posted at: 6/23/05 12:23 PM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,914

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)


None

Toast

Reply To Post Reply & Quote

Posted at: 6/23/05 12:25 PM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,914

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 ^_^


None

Rustygames

Reply To Post Reply & Quote

Posted at: 6/23/05 01:55 PM

Rustygames LIGHT LEVEL 18

Sign-Up: 05/07/05

Posts: 6,662

Nice work mate...

Brilliant code. Gave me inspiration

- Matt, Rustyarcade.com


None

Starogre

Reply To Post Reply & Quote

Posted at: 6/30/05 10:58 AM

Starogre NEUTRAL LEVEL 18

Sign-Up: 05/08/04

Posts: 1,694

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

BBS Signature

None

dELtaluca

Reply To Post Reply & Quote

Posted at: 7/29/05 10:22 AM

dELtaluca LIGHT LEVEL 20

Sign-Up: 04/16/04

Posts: 5,542

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

My social worker says im special!

BBS Signature

None

dieEVILsanta

Reply To Post Reply & Quote

Posted at: 8/9/05 11:37 PM

dieEVILsanta NEUTRAL LEVEL 08

Sign-Up: 05/26/05

Posts: 486

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


None

ImpotentBoy2

Reply To Post Reply & Quote

Posted at: 8/10/05 12:26 AM

ImpotentBoy2 LIGHT LEVEL 18

Sign-Up: 04/01/03

Posts: 5,318

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

Some times my "L" key decides not to work.


Questioning

JCesta

Reply To Post Reply & Quote

Posted at: 9/19/05 09:27 PM

JCesta LIGHT LEVEL 09

Sign-Up: 07/08/05

Posts: 433

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


None

JCesta

Reply To Post Reply & Quote

Posted at: 9/19/05 09:37 PM

JCesta LIGHT LEVEL 09

Sign-Up: 07/08/05

Posts: 433

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


None

Joelasticot

Reply To Post Reply & Quote

Posted at: 9/19/05 09:41 PM

Joelasticot EVIL LEVEL 37

Sign-Up: 02/14/03

Posts: 867

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.

var s=0;var p=0;addEventListener("enterFrame", function(){
s+=(25-p)/20;p+=Math.ceil(s);var o="";
for(var i=0;i<p;i++){o+=" ";};trace(o+"joelasticot");})

BBS Signature

None

Hominind

Reply To Post Reply & Quote

Posted at: 9/20/05 12:25 AM

Hominind LIGHT LEVEL 04

Sign-Up: 03/01/04

Posts: 5

Wow impressive code lines


None

GringoMan

Reply To Post Reply & Quote

Posted at: 9/25/05 11:42 AM

GringoMan EVIL LEVEL 06

Sign-Up: 08/18/05

Posts: 18

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.


None

GringoMan

Reply To Post Reply & Quote

Posted at: 9/25/05 01:19 PM

GringoMan EVIL LEVEL 06

Sign-Up: 08/18/05

Posts: 18

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


None

GringoMan

Reply To Post Reply & Quote

Posted at: 9/25/05 01:47 PM

GringoMan EVIL LEVEL 06

Sign-Up: 08/18/05

Posts: 18

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.


None

GuyWithHisComp

Reply To Post Reply & Quote

Posted at: 12/19/05 01:31 PM

GuyWithHisComp LIGHT LEVEL 27

Sign-Up: 11/10/05

Posts: 4,010

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

BBS Signature

None

Zwickel

Reply To Post Reply & Quote

Posted at: 1/2/06 01:25 AM

Zwickel NEUTRAL LEVEL 22

Sign-Up: 07/21/03

Posts: 798

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


None

dELtaluca

Reply To Post Reply & Quote

Posted at: 1/2/06 05:17 AM

dELtaluca LIGHT LEVEL 20

Sign-Up: 04/16/04

Posts: 5,542

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

My social worker says im special!

BBS Signature

Questioning

Landlad

Reply To Post Reply & Quote

Posted at: 1/2/06 06:26 PM

Landlad NEUTRAL LEVEL 06

Sign-Up: 12/28/05

Posts: 10

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


None

Rabbi-Indie-The-Rad

Reply To Post Reply & Quote

Posted at: 1/12/06 05:29 PM

Rabbi-Indie-The-Rad NEUTRAL LEVEL 17

Sign-Up: 12/04/05

Posts: 2,305

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?


None

PolishMike

Reply To Post Reply & Quote

Posted at: 2/18/06 11:53 PM

PolishMike NEUTRAL LEVEL 07

Sign-Up: 01/03/05

Posts: 6

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?


None

Toast

Reply To Post Reply & Quote

Posted at: 2/19/06 05:21 AM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,914

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


None

T-H

Reply To Post Reply & Quote

Posted at: 2/19/06 05:22 AM

T-H LIGHT LEVEL 39

Sign-Up: 01/07/04

Posts: 4,893

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.


None

iameatingjam

Reply To Post Reply & Quote

Posted at: 2/19/06 05:40 AM

iameatingjam NEUTRAL LEVEL 11

Sign-Up: 01/02/05

Posts: 287

pretty 1337 code you got there, it fwnz mine.


None

dELtaluca

Reply To Post Reply & Quote

Posted at: 2/19/06 05:43 AM

dELtaluca LIGHT LEVEL 20

Sign-Up: 04/16/04

Posts: 5,542

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

My social worker says im special!

BBS Signature

None

Rantzien

Reply To Post Reply & Quote

Posted at: 2/19/06 07:16 AM

Rantzien FAB LEVEL 15

Sign-Up: 01/27/05

Posts: 3,426

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

None

FlashKid

Reply To Post Reply & Quote

Posted at: 2/19/06 07:25 AM

FlashKid EVIL LEVEL 14

Sign-Up: 11/22/03

Posts: 1,177

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.

Below is what would of been a picture of my real time stratergy, it's just too bad I can't fit it's awesomeness in 599X50. Who thinks up these dimensions?!

BBS Signature

None

MARINO

Reply To Post Reply & Quote

Posted at: 2/22/06 11:03 AM

MARINO FAB LEVEL 08

Sign-Up: 02/21/06

Posts: 857

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


All times are Eastern Standard Time (GMT -5) | Current Time: 01:24 PM

<< Back

This topic is 2 pages long. [ 1 | 2 ]

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!