In this tutorial you'll extend the side-scrolling game that you started in the last tutorial. You'll improve the physics and add some animations.
var xmov=0; // velocity in x direction is initially 0
var ymov=0; // velocity in y direction is initally 0
var acceleration=1; // acceleration constant
onEnterFrame=function () {
// check for input and adjust acceleration
if (Key.isDown(Key.RIGHT)) {
xmov += acceleration;
}
if (Key.isDown(Key.LEFT)) {
xmov -= acceleration;
}
if (Key.isDown(Key.UP)) {
ymov -= acceleration;
}
if (Key.isDown(Key.DOWN)) {
ymov += acceleration;
}
// this is where gravity should go
// this is where friction should go
// now move the background in the opposite direction of the ball
// so it looks like the ball is moving
bg._x -= xmov;
bg._y -= ymov;
}
In this code the variables xmov and ymov are the speed in the
x and y directions of the circle. Initially they are 0 but they increase
and decrease depending on the user input.
var height = bg._y;After the line
bg._y -= ymov;add the following.
if (bg._y < height) {
bg._y = height;
}
Test out your game. The ball should now stop at the horizon.
// this is where gravity should gowith the following:
// add gravity
if (bg._y > height) {
ymov += gravity;
}
Finally, add the following at the top of the actionScript:
var gravity=0.5; // acceleration due to gravity is 0.5 pixels/frameTest your game. Use the up arrow to make the circle rise above the horizon. If you stop pressing the arrow keys the circle will fall back to the horizon.
// this is where friction should gowith the following:
addFriction();This is a function call. Flash will look for this function and run its code. Add the function shown below to the bottom of your actionScript file.
function addFriction(){
if (bg._y == height) { // if the ball is on the horizon, add friction
if (xmov > 0) { // the ball is moving to the right
xmov -= friction; // slow the ball down
if (xmov <0) { // but don't make it change directions
xmov = 0;
}
}
if (xmov < 0) { // the basll is moving to the left
xmov += friction; // slow the ball down
if (xmov > 0) { // but don't change directions
xmov = 0;
}
}
}
}
This function checks to be sure the ball is on the horizon then slows it down.
The last thing we have to do is add the definition of friction. Copy the
following line to the top of your actionScript.
var friction=0.5; // deceleration due to friction is 0.5 pixel/frame
gotoAndPlay("fixed");
gotoAndPlay("fixed");
bg._y -= ymov;
if (bg._y < height) {
bg._y = height;
}
with the following
if (bg._y != height || ymov < 0) {
bg._y -= ymov;
// collide if necessary with the horizon
if (bg._y < height) {
bg._y = height;
circle.gotoAndPlay("squash");
}
}
var damping = .75; // damping factorNext, go to the circle movie clip symbol and click on final frame of the script layer. Change the line
gotoAndPlay("fixed");
to the following
_parent.ymov *= -_root.damping;
gotoAndPlay("fixed");
Note that the "_parent." prefix means, tells Flash to you the ymov and
damping defined one level above!