What you will be making- http://denvish.net/u..mer%20Code%20001.swf
As: Main
Preperation
Draw your character. Convert it into a movieclip, and make sure the + is on the bottem middle.
Make your ground and convert that into a movieclip. Give it the instance name of "ground" without the quotes. The ground can be anything, make it all in one movieclip., The code checks for walls and ceiling and floors by itself. Lucky you.
The Script
Click on your character, and go to actions. Now copy and paste this into him.
The // start a comment, they dont affect the script
//made by pyro111
// thanks to Vengance for helping
onClipEvent (load) {
grav = 0;
// sets falling speed
speed = 7;
// sets movement speed
jumpHeight = 15;
// sets height that you can jump
scale = _xscale;
// sets size
slowfall = .8;
// sets glide
}
onClipEvent (enterFrame) {
// happens every frame
grav++;
// makes you fall faster
_y += grav;
// makes your _y go up by the varaible grav
while (_root.ground.hitTest(_x, _y, true)) {
// when this is touching the ground
_y--;
// makes the y keep on going up, this enables slopes
grav = 0;
// make it so you dont fall
}
if (Key.isDown(68)) {
// when the d key is down
_x += speed;
// makes the x go up by the speed
_xscale = scale;
// makes it face right
if (_root.ground.hitTest(_x, _y+3, true)) {
// if it is touching the ground
this.gotoAndStop(1);
// goes to frame 1
} else {
// otherwise
this.gotoAndStop(2);
// stops at frame 2
}
} else if (Key.isDown(65)) {
// if that isnt happening and the a key is pressed
_x -= speed;
// makes the x go gown by the speed
_xscale = -scale;
// makes it face left
if (_root.ground.hitTest(_x, _y+3, true)) {
this.gotoAndStop(1);
} else {
this.gotoAndStop(2);
}
// see other code for movign right
} else {
if (_root.ground.hitTest(_x, _y+3, true) && !Key.isDown(79)) {
// if this is touching the ground and o isnt pressed
this.gotoAndStop(3);
// stops at frame 3
}
}
if (Key.isDown(83)) {
// if the s key is down
grav *= slowfall;
// slows down the gravity by multiplying it by the slowfall varialbe
}
if (Key.isDown(79) && !Key.isDown(87) && !Key.isDown(65) && !Key.isDown(68)) {
// if 0 is down, and a s d w arent
this.gotoAndStop(4);
// goes to frame 4
}
if (Key.isDown(87) && _root.ground.hitTest(_x, _y+3, true)) {
// if w is pressed and it is touching the ground
grav = -jumpHeight;
// makes gravity negative jumpheight, causes jump
_y -= 4;
// makes the y go up by 3
this.gotoAndStop(2);
// stops on frame 2
}
if (_root.ground.hitTest(_x+(_width/2), _y-(_height/2), true) || _root.ground.hitTest(_x+(_width/2), _y-((_height/6)*4), true)) {
_x -= speed;
}
if (_root.ground.hitTest(_x-(_width/2), _y-(_height/2), true) || _root.ground.hitTest(_x-(_width/2), _y-((_height/6)*4), true)) {
_x += speed;
}
if (_root.ground.hitTest(_x, _y-_height, true)) {
grav = 2;
}
// these all check for 2 points along the sides and top, to see if it is hitting a wall
}
How It Works
Well, first off the code makes the player go down by gravity. Gravity goes up every second, making the player fall faster. When the player touches the ground, gravity is reset to zero.
Then when the player presses the W key. Gravity is at negititve jump height. So the player will go up. But because gravity is goin up, the jump goes down. so gravity will be -14, -13, -12, -11, ect. that means it is going up. then it hits 0, and then 1, 2,3 and the player starts to fall again.