Forum Topic: AS3: Basic Platform

(2,279 views • 10 replies)

This topic is 1 page long.

<< < > >>
None

medhopz

Reply To Post Reply & Quote

Posted at: 8/13/07 01:57 PM

medhopz EVIL LEVEL 13

Sign-Up: 04/09/06

Posts: 178

first tut on NG! w00t!

AS3: Main

things i'll cover:
- listeners
- key detection
- variables
- detecting collision
***I am covering all the basics of the subject above, this tut is made for people who are new to AS3
Final Product

Begining
First, we need to make our character and ground
just make a square, convert to a move clip and give an instence name of "man"
make sure you put his registration point at the buttom middle ('2' if you are looking at your numPad)
Next, make your ground
i just made a square, but make sure it's pretty thick
convert it to a movie clip and call it "ground"
place your "ground" movie clip at the buttom of the screen
on we go to the middle section

Middle
in AS3 there is no such thing as writing code inside a movie clip
in AS3 all your code goes on the main timeline
so open up your actions panal
first of all we need key detection
i didn't go into this, i just took TrueDarkness's functions
you can find them here
since i don't want you to go wandering off in the middle of the tut i'll just give you the code:

var right:Boolean = false;
var left:Boolean = false;
var up:Boolean = false;
var down:Boolean = false;
function checkKeys(event:KeyboardEvent) {
if(event.keyCode == 37) {
left = true;
}
if(event.keyCode == 38) {
up = true;
}
if(event.keyCode == 39) {
right = true;
}
if(event.keyCode == 40) {
down = true;
}
}
function checkKeyUp(event:KeyboardEvent) {
if(event.keyCode == 37) {
event.keyCode = 0;
left = false;
}
if(event.keyCode == 38) {
event.keyCode = 0;
up = false;
}
if(event.keyCode == 39) {
event.keyCode = 0;
right = false;
}
if(event.keyCode == 40) {
event.keyCode = 0;
down = false;
}
}
stage.addEventListener(KeyboardEvent.KEY _DOWN, checkKeys);
stage.addEventListener(KeyboardEvent.KEY _UP, checkKeyUp);

for the code explnation go to the link above
put this code on you main timeline
now we need to set some variables

var grav:Number = 0; // set gravity to 0
var jump:Boolean = false; // jump is currently false
var vx:Number = 0; // set x velocity is 0
var vy:Number = 0; // set y velocity is 0
var acc:Number = 0.9; // set deccelaration rate
var maxSpeed:Number = 15; //max x speed
var jumpHeight:Number = 10; // how high we will jump
var jumpAble = false; // is the character able to jump?
var minusNum:Number = 2; // our high will you bounce
var minusNumNum:Number = 2; // another bouncing variable

now that we have all the variables figured out we can go on to our main function
before we go on to the code, i'll tell you what we have in this function
this is a ENTER_FRAME function (like onEnterFrame in AS2)
we will have our characters gravity and bounce
we will have our character jump
we will have our character side movement

naming the function:
basic function stuff, nothing special

function gravity(Event) {

next we will make our character's gravity

grav++;

this makes the 'grav' variable go up by one
easy huh?
remember, all of this happends every frame
next we do hit detection with the ground

if(man.hitTestObject(ground)) {

basic AS3 hitTesting, simple stuff
but what will happen when he hits the grounds?

grav--;
grav = 0;

we make our gravity variable go down one every time we hit the ground
and just to make sure that it doesn't bounce up and down by one (at the start we up the grav by one and now we make it go down by --- makes a "bouncing" effect that we really don't want) we set the grav variable to 0.
we continue on to the bouce

grav -= jumpHeight/minusNum;
minusNum += minusNumNum;

what we do here is decrease from out gravity variable
remeber this happens inside the hitTest function
we decrease out grav variable by the jumpHeight divided by minusNum
which gives us: grav -= 15/2
that's how high we will bounce the first time we hit the ground
you are wondering what does 'jumpHeight' have to do with our bounce?
we need to have a link between them. that way we don't jump really high and bounce really low
continuing to the next line in that piece of code
we add to our minusNum variable so that the next time we hit the ground we will bounce lower
but what happens when the variable gets so small that our bounce will be tiny?
we don't like tiny bounces do we?
so we do the next if statement

if(minusNum >= jumpHeight) {
minusNumNum = 0;
grav += jumpHeight/minusNum;
}

the 'if' statement checks if out minusNum variable is bigger or equal to out jumpHeight variable
if that happes, we set our minusNumNum variable to 0, thus not adding anymore to our minusNum
we also need to stop the bounce, we do that by adding to our 'grav' variable what we substracted before, thus making the grav variable 0
next we go on to jumping!
still inside out hitTest function we set our jumpAble boolean to true (only jump if you touching the ground)

jumpAble = true;

now close your hitTest function, it's all finished
now for out jumping code

if(up) {
if(jumpAble) {
grav -= jumpHeight;
jumpAble = false;
minusNum = 2;
minusNumNum = 2;
}
}

this code goes inside our gravity function but outside the hitTest
if you looked at the key detection code you will see that to check if the key is down, all we need to do is do a simple if with our key in the brackets, if(up), if(down), if(right), if(left)
now that we know that our 'up' arrow key has been pressed we want to know if we are able to jump
if both pass as true, our gravity will decrease the jumpHeight variable thus making us jump
now that we are in the air we need to set our jumpAble boolean to false
remeber how before our minusNum and minusNumNum changed from 2? (our starting variable)
we need to reset them! or else our bounce the next time we land will be ...none
so after we did that we close up our 'if' statements and go on to side movement

if(right) {
vx++
}
if(left) {
vx--
}

just as we checked if 'up' was pressed here we check if 'left' and 'right' are pressed
if they are, we add to the x velocity variable one, or if we press left we decrease one
this happens every time we press 'left' or 'right'
we dont want our character to be ultra speedy right? we want to set him a limit

if(vx >= maxSpeed) {
vx = maxSpeed;
}
if(vx <= -maxSpeed) {
vx = -maxSpeed;
}

if our x velocity is bigger than maxSpeed than our x velocity equals max speed
same thing with smaller than maxSpeed but here our maxSpeed is negative
we want our character to stop
but not just stop if we don't press the arrow keys, we want him to stop slowly

if((!right) && (!left)) {
vx *=acc;
}

if neither of the keys is pressed than multiply our our side velocity variable by our deccelration variable
if this happesns every frame, our character will eventually come to a stop
we also need to set bonderies for our guy

if((man.x + man.width > 550) || (man.x - man.width < 0)) {
vx *= -1
}

a simple if that checks wheater our character's x plus/minus his width our bigger/smaller the stage's borders
if it does happen, we mu;tiply our x velocity variable by -1 change it from positive to negative and vice-versa, this eventually leads to out character changing direction
now we have finished our movement,jump, gravity.
but we didn't do anything with our 'grav' and 'vx' variables
they are just calculated
so we add them to our character's x and y values

man.y += grav;
man.x += vx;

now close your gravity funciton and go on to the end!
end
we now have to add an eventListener to out function, making it run every frame

stage.addEventListener(Event.ENTER_FRAME , gravity);

there we are done!!!
final FLA
if you don't want 10 frames in front of the key frame like i did you can just put the code on the same frame like i told you here

enjoy!

medhopz

BBS Signature

None

crushy

Reply To Post Reply & Quote

Posted at: 8/13/07 01:58 PM

crushy LIGHT LEVEL 15

Sign-Up: 09/17/05

Posts: 1,843

wow, I never realised how different AS3 and AS2 were...

Learning AS3 :D
Help me fix Flash :(

BBS Signature

None

dELtaluca

Reply To Post Reply & Quote

Posted at: 8/13/07 02:05 PM

dELtaluca LIGHT LEVEL 20

Sign-Up: 04/16/04

Posts: 5,541

At 8/13/07 01:58 PM, crushy wrote: wow, I never realised how different AS3 and AS2 were...

and there's your mistake, THEY AERN'T.

My social worker says im special!

BBS Signature

None

medhopz

Reply To Post Reply & Quote

Posted at: 8/13/07 02:13 PM

medhopz EVIL LEVEL 13

Sign-Up: 04/09/06

Posts: 178

delta is correct
they are not that different
in this tut i tried really really making the reader understand what her does and not jsut copy

BBS Signature

None

gangstamonkey

Reply To Post Reply & Quote

Posted at: 3/7/08 09:18 PM

gangstamonkey NEUTRAL LEVEL 02

Sign-Up: 12/24/07

Posts: 1

Sweet tut, Worked Perfectly!
But one question, How do you use mutible platforms??

BBS Signature

None

bcapecci

Reply To Post Reply & Quote

Posted at: 3/10/08 07:19 PM

bcapecci NEUTRAL LEVEL 06

Sign-Up: 03/07/08

Posts: 271

At 8/13/07 02:05 PM, dELtaluca wrote:
At 8/13/07 01:58 PM, crushy wrote: wow, I never realised how different AS3 and AS2 were...
and there's your mistake, THEY AERN'T.

actually bud they are very different. as3 is more of an actual language... they added alot. game programming is going to be much the game why because pi will always be pi, tan will always be tan, 1 will always be 1... u get me. math never changes and in games theres alot of math. as3 can be as different as u want it to be. wither u include custom classes/packages, varible declarions, is up to u but it changes the laguage very much. but compared form the as1 to as2 transtition and all the new things as3 has to offer different is an understtement, if you make use of the new things.


None

bcapecci

Reply To Post Reply & Quote

Posted at: 3/12/08 05:29 PM

bcapecci NEUTRAL LEVEL 06

Sign-Up: 03/07/08

Posts: 271

yeah how do u use many platforms. also how do u reduce the bounce height. i changed the bounce varibles considerably but must have done soemthin wrong. thanks for the great tut.


None

MADSOFT

Reply To Post Reply & Quote

Posted at: 3/25/09 09:00 PM

MADSOFT DARK LEVEL 17

Sign-Up: 02/01/07

Posts: 177

To do multiple platforms, try creating an array containing the amount of platforms and adding a for loop.
You can make a check that automatically counts the number of platforms and adds them to the array, or you could just place them all in yourself.
I won't write any code because I'd like you to experiment and try and figure it out. I just gave the concept for you to work with. Enjoy~

MADSOFT Games LLC.

BBS Signature

None

DELUCA2400

Reply To Post Reply & Quote

Posted at: 4/3/09 11:48 PM

DELUCA2400 LIGHT LEVEL 24

Sign-Up: 05/04/07

Posts: 1,236

Good tutorial you've actually explained what the code does instead of just spoon feeding us the stuuf. I have a question though;No I understood the whole ground hittest thing but I want to add a little pillar now what should I put down exactly (man I sound like a hypocrite right now)

I tried putting

if (man.hitTestObject(pillar)) {

right above

if(man,hitTestObject(ground)) {

and that didn't work.


None

Kenzai93

Reply To Post Reply & Quote

Posted at: 4/7/09 12:23 AM

Kenzai93 NEUTRAL LEVEL 13

Sign-Up: 04/02/09

Posts: 69

At 8/13/07 01:57 PM, medhopz wrote:

:we make our gravity variable go down one every time we hit the ground
:and just to make sure that it doesn't bounce up and down by one (at the start we up the grav by one :and now we make it go down by --- makes a "bouncing" effect that we really don't want) we set the :grav variable to 0.

Well this part isnt totally correct actually. First you make it go up by one (grav++), then down by one (grav--), but that means, as an example if it was X when it hit the ground, it will remain X and go down into the ground with a static speed of X, and NOT bounce back up because you didnt use "grav = -grav" in which case it would become -X and go UP with the speed of X.

Though this doesnt cause any problems as you declare a totally new value for grav in the same frame anyway.

Another thing is:
You dont need to set new values for grav many times in the same place.
As an example in our case its the hittest where it all happens at once, and theres no if statement - or anything like that - that devides the "grav--", "grav=0" and "grav=jumpHeight/minusNum" statements.
As they all happen after one another BUT before any output, its only the last one that "passes through".
In our case, "grav=jumpHeight/minusNum" (which sets a static value to grav irrelevant of any value before) is the only thing that applies to the output, and the other two statements that alter grav before (those would be grav-- and grav=0) are discarded.
THOUGH if there would be something like "grav++" or "grav=grav-5" (which alter grav in a form TIED TO the initial grav value) AFTER something like "grav=(a static value)" THEN it WOULD apply to the output because it is "tied to" an already existing grav value.

I have tested this for a simple grav=0 versus a grav=0 with a grav-- before, and it was the same.
WARNING: Be careful though, NOT to put man.y+=grav in between the grav++ and the hittest (which contains other things that alter grav) or else, after hittest, grav will always be one more than what its supposed to be. Its best to put man.y+=grav after everything that alters grav except you think you can make use of putting it elsewhere...

OMG thinking logical stuff is so fun. This is definitely my future job. LOL
Ive just spent tens of minutes testing and thinking about all this.
***EXP points upped, new AS3 skill level reached: 5/100.*** LOL


None

Kenzai93

Reply To Post Reply & Quote

Posted at: 4/7/09 12:27 AM

Kenzai93 NEUTRAL LEVEL 13

Sign-Up: 04/02/09

Posts: 69

How well is double posting tolerated?
We need an edit button.

IN MY POST ABOVE, the first paragrapgh is what medhopz wrote. Sorry for not being careful about the quote thing.

:A little test on how it works... Nevermind please.

At 01/01/01 00:00 AM, noone wrote:

:So this is how you put quote code?
:Also wheres the spam forum? LOL


All times are Eastern Standard Time (GMT -5) | Current Time: 09:14 PM

<< Back

This topic is 1 page long.

<< < > >>
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!