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