Senior
Flash production Object Bounce computer game, this is a background not moving an object can bounce, can move around the small game. Comparison of the basics of the game. at the end of the article provide all the demo flash source files.
Start Flash, first modify the document properties.
First make two movie clips one is the background, draw a rectangular small block to the background. Add as: Stop () to stop it from starting.
The other is a moving object, give you screenshots as follows.
Back to the main scene, we use ActionScript to achieve other effects. Add the following code directly to the first frame of the main scene:
xspeed = 0;
yspeed = 0;
Max_yspeed = 16;
Gravity = 1;
Walk_speed = 4;
Level = new Array ();
_root.createemptymovieclip ("Lev", _root.getnexthighestdepth ());
Level[0] = new Array (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
LEVEL[1] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[2] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[3] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[4] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[5] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1);
LEVEL[6] = new Array (1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[7] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[8] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[9] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[10] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[11] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[12] = new Array (1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[13] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[14] = new Array (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
For (y=0 y<=14; y++) {
for (x=0 x<=24; x + +) {
if (Level[y][x]!= 0) {
Place_brick = Lev.attachmovie ("block", "Block_" +lev.getnexthighestdepth (), lev.getnexthighestdepth (), {_x:x*20+10, _ Y:Y*20+10});
Place_brick.gotoandstop (Level[y][x]);
}
}
}
_root.attachmovie ("Player", "Player", _root.getnexthighestdepth (), {_x:40, _y:40});
Player.onenterframe = function () {
Yspeed + = gravity;
if (yspeed>max_yspeed) {
Yspeed = Max_yspeed;
}
if (Key.isdown (Key.left)) {
XSpeed =-walk_speed;
}
if (Key.isdown (key.right)) {
XSpeed = Walk_speed;
}
while (_root.lev.hittest (this._x, This._y+this._height/2-1+yspeed, True)) {
yspeed--;
}
while (_root.lev.hittest (This._x-this._width/2+1+xspeed, this._y, True)) {
xspeed++;
}
while (_root.lev.hittest (This._x+this._width/2-1+xspeed, this._y, True)) {
xspeed--;
}
This._y + = Yspeed;
This._x + = XSpeed;
xspeed = 0;
};
The effect of the above code is that there will be a fixed background. The effect is as follows.
Then put the bouncing objects in.
yspeed = 0;
Max_yspeed = 16;
Gravity = 1;
Level = new Array ();
_root.createemptymovieclip ("Lev", _root.getnexthighestdepth ());
Level[0] = new Array (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
LEVEL[1] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[2] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[3] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[4] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[5] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1);
LEVEL[6] = new Array (1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[7] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[8] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[9] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[10] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[11] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[12] = new Array (1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[13] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[14] = new Array (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
For (y=0 y<=14; y++) {
for (x=0 x<=24; x + +) {
if (Level[y][x]!= 0) {
Place_brick = Lev.attachmovie ("block", "Block_" +lev.getnexthighestdepth (), lev.getnexthighestdepth (), {_x:x*20+10, _ Y:Y*20+10});
Place_brick.gotoandstop (Level[y][x]);
}
}
}
_root.attachmovie ("Player", "Player", _root.getnexthighestdepth (), {_x:40, _y:40});
Player.onenterframe = function () {
Yspeed + = gravity;
if (yspeed>max_yspeed) {
Yspeed = Max_yspeed;
}
while (_root.lev.hittest (this._x, This._y+this._height/2-1+yspeed, True)) {
yspeed--;
}
This._y + = Yspeed;
};
Effect (a static background, a bouncing object appears)
Then let the object move around.
xspeed = 0;
yspeed = 0;
Max_yspeed = 16;
Gravity = 1;
Walk_speed = 4;
Level = new Array ();
_root.createemptymovieclip ("Lev", _root.getnexthighestdepth ());
Level[0] = new Array (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
LEVEL[1] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[2] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[3] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[4] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[5] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1);
LEVEL[6] = new Array (1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[7] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[8] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[9] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[10] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[11] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[12] = new Array (1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[13] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[14] = new Array (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
For (y=0 y<=14; y++) {
for (x=0 x<=24; x + +) {
if (Level[y][x]!= 0) {
Place_brick = Lev.attachmovie ("block", "Block_" +lev.getnexthighestdepth (), lev.getnexthighestdepth (), {_x:x*20+10, _ Y:Y*20+10});
Place_brick.gotoandstop (Level[y][x]);
}
}
}
_root.attachmovie ("Player", "Player", _root.getnexthighestdepth (), {_x:40, _y:40});
Player.onenterframe = function () {
Yspeed + = gravity;
if (yspeed>max_yspeed) {
Yspeed = Max_yspeed;
}
if (Key.isdown (Key.left)) {
XSpeed =-walk_speed;
}
if (Key.isdown (key.right)) {
XSpeed = Walk_speed;
}
while (_root.lev.hittest (this._x, This._y+this._height/2-1+yspeed, True)) {
yspeed--;
}
while (_root.lev.hittest (This._x-this._width/2+1+xspeed, this._y, True)) {
xspeed++;
}
while (_root.lev.hittest (This._x+this._width/2-1+xspeed, this._y, True)) {
xspeed--;
}
This._y + = Yspeed;
This._x + = XSpeed;
xspeed = 0;
};
Effect (at this point you can see the effect by pressing the keyboard left and RIGHT ARROW keys)
Finally let the object can jump up Ah!
xspeed = 0;
yspeed = 0;
Max_yspeed = 16;
Gravity = 1;
Walk_speed = 4;
Can_jump = false;
Jump_power = 10;
Jump_walk = true;
Level = new Array ();
_root.createemptymovieclip ("Lev", _root.getnexthighestdepth ());
Level[0] = new Array (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
LEVEL[1] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[2] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[3] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[4] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[5] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1);
LEVEL[6] = new Array (1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[7] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[8] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[9] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[10] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[11] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[12] = new Array (1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[13] = new Array (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
LEVEL[14] = new Array (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
For (y=0 y<=14; y++) {
for (x=0 x<=24; x + +) {
if (Level[y][x]!= 0) {
Place_brick = Lev.attachmovie ("block", "Block_" +lev.getnexthighestdepth (), lev.getnexthighestdepth (), {_x:x*20+10, _ Y:Y*20+10});
Place_brick.gotoandstop (Level[y][x]);
}
}
}
_root.attachmovie ("Player", "Player", _root.getnexthighestdepth (), {_x:40, _y:40});
Player.onenterframe = function () {
if (Key.isdown (Key.left)) {
if (Jump_walk or can_jump) {
XSpeed =-walk_speed;
}
}
if (Key.isdown (key.right)) {
if (Jump_walk or can_jump) {
XSpeed = Walk_speed;
}
}
if (Key.isdown (key.space) and Can_jump) {
Yspeed-= Jump_power;
Can_jump = false;
}
Yspeed + = gravity;
if (yspeed>max_yspeed) {
Yspeed = Max_yspeed;
}
while (_root.lev.hittest (this._x, This._y+this._height/2-1+yspeed, True)) {
yspeed--;
Can_jump = true;
}
while (_root.lev.hittest (This._x-this._width/2+1+xspeed, this._y, True)) {
xspeed++;
}
while (_root.lev.hittest (This._x+this._width/2-1+xspeed, this._y, True)) {
xspeed--;
}
while (_root.lev.hittest (this._x, This._y-this._height/2+1+yspeed, True)) {
yspeed++;
}
This._y + = Yspeed;
This._x + = XSpeed;
if (Jump_walk or can_jump) {
xspeed = 0;
}
};
Final effect (press LEFT ARROW key and Space bar to try)
All of the above demo animation source files: Click here to download the source file