Learn flash Production Object Bounce game

Source: Internet
Author: User
Tags reset

I do not know how to play the name of this article, if you think my article name is not good, in the process of reprint you can change a good name. Don't forget to tell me! I hope this article is useful for people who are developing the Flash game entry.

  The game is, first set a map, and then come out a person (or anything else), from the sky down, and then we use the keyboard key to control the left and right movement, when you want to move up, use the space bar can be high jump. Simple research, I hope you like it. reprint must indicate the source and author, if add this station connection, then thank you.

Start to explain how to make. ( each step has a demo animation, at the end of the article also gives all the demo animation source files!) )

Create a flash document and ctrl+j modify the movie properties.

Then create a film clip element that is used as a map, with the name terrain, and the shape shown below.

Then create two buttons, one is go, and the other is reset. Finally, create a char movie clip symbol (see your painting time, build whatever you want).

Then put the three-class elements (terrain,char,2 buttons) into the scene. Then give the char movie clip symbol The instance name Walker, the map terrain instance named terrain. Then select the first frame, press F9 to open the Action panel, and enter the code.

Don't forget that the map terrain instance is named terrain. In the same way, no screenshots are given.

The first frame adds the following code:

go=false;//is primarily a variable that assigns an initial value

Go button:

On (release) {
Go = true;
}//began to exercise

Reset button:

On (release) {
Go = false;
walker._x = 107;
Walker._y = 49;
}//back to initial state

The Char movie clip then has the following action:

Onclipevent (load) {
Gravity = 0.2;
yspeed = 0;
}
Onclipevent (enterframe) {
if (_root.go) {
Yspeed + = gravity;
while (_root.terrain.hittest (_x, _Y+_HEIGHT/2, True)) {
_y--;
yspeed = 0;
}
_y + = Yspeed;
}
}

The effect is as follows (as soon as you click the Go button, the object will fall off and the point reset will return to its original state):

By looking at the above code you can see that the speed is always increasing, so the object always moves when it touches the ground.

  Improve the code above:

onclipevent (load) {
    gravity = 0.2;
    yspeed = 0;
}
Onclipevent (enterframe) {
    if (_root.go) {
         Yspeed + = gravity;
        while (_root.terrain.hittest (_x, _Y+_HEIGHT/2, True)) {
             _y--;
            yspeed = 0;
       }
        if (!_root.terrain.hittest (_x, _y+_height/2+1, True)) {
             _y + = Yspeed;
       } else {
             yspeed = 0;
       }
   }
}

Effect (via yspeed = 0; make contact with the ground speed to 0):

With the above two steps we have made the basics of this game. Here we start making the game using the keyboard to control movement.

With the keyboard to the left and right arrow keys to control the movement of objects around.

Onclipevent (load) {
Gravity = 0.2;
yspeed = 0;
XSpeed = 1;
}
Onclipevent (enterframe) {
if (_root.go) {
if (Key.isdown (Key.left)) {
_x-= xspeed;
}
if (Key.isdown (key.right)) {
_x + = XSpeed;
}
Yspeed + = gravity;
while (_root.terrain.hittest (_x, _Y+_HEIGHT/2, True)) {
_y--;
yspeed = 0;
}
if (!_root.terrain.hittest (_x, _y+_height/2+1, True)) {
_y + = Yspeed;
} else {
yspeed = 0;
}
}
}

The effect is as follows:

The above effect, everyone by using the keyboard to move the key, if the object to the edge of the map, you can move outside. Here we continue to refine the code to limit the movement of the object.

onclipevent (load) {
 gravity = 0.2;
 yspeed = 0;
 xspeed = 1;
}
Onclipevent (enterframe) {
 if (_root.go) {
  if (Key.isdown (key.left)) {
    if (!_root.terrain.hittest (_X-_WIDTH/2, _Y+_HEIGHT/4, True)) {
    _x-= xspeed;
  &NBSP}
  }
  if (Key.isdown (key.right)) {
   if (!_ Root.terrain.hitTest (_X+_WIDTH/2, _Y+_HEIGHT/4, True)) {
    _x = = xspeed;
  &NBSP}
  }
  yspeed + = gravity;
  while (_root.terrain.hittest (_x, _Y+_HEIGHT/2, True)) {
   _y--;
   yspeed = 0;
  }
  if (!_root.terrain.hittest (_x, _y+_height/2+1, True)) {
   _y + = yspeed;
 &NBSP} else {
   yspeed = 0;
 &NBSP}
 }

The code above controls the boundaries between the left and right sides. The effect is as follows (this will not move outside the bar!) ):

Here we start to improve the code so that the object can be controlled by the keyboard's space bar.

Onclipevent (load) {
Gravity = 0.2;
yspeed = 0;
XSpeed = 1;
}
Onclipevent (enterframe) {
if (_root.go) {
if (Key.isdown (Key.left)) {
if (!_root.terrain.hittest (_X-_WIDTH/2, _Y+_HEIGHT/4, True)) {
_x-= xspeed;
}
}
if (Key.isdown (key.right)) {
if (!_root.terrain.hittest (_X+_WIDTH/2, _Y+_HEIGHT/4, True)) {
_x + = XSpeed;
}
}
if (Key.isdown (Key.space)) {
Yspeed =-5;
}
Yspeed + = gravity;
while (_root.terrain.hittest (_x, _Y+_HEIGHT/2, True)) {
_y--;
yspeed = 0;
}
if ((!_root.terrain.hittest (_x, _y+_height/2+1, True)) or (yspeed<0) {
_y + = Yspeed;
} else {
yspeed = 0;
}
}
}

The effect is as follows (object move after pressing the space bar to try!!) ):

We pass the test above film, press the space bar will not jump?? And the jump is still very high!! If you always press the space bar can also jump out of the screen! Ha ha!!! So the following people should know what to do!! Make a limit to the range of jumps, you are very clever.

Onclipevent (load) {
Gravity = 0.2;
yspeed = 0;
XSpeed = 1;
Jumping = 0;
}
Onclipevent (enterframe) {
if (_root.go) {
if (Key.isdown (Key.left)) {
if (!_root.terrain.hittest (_X-_WIDTH/2, _Y+_HEIGHT/4, True)) {
_x-= xspeed;
}
}
if (Key.isdown (key.right)) {
if (!_root.terrain.hittest (_X+_WIDTH/2, _Y+_HEIGHT/4, True)) {
_x + = XSpeed;
}
}
if ((Key.isdown (Key.space)) and (!jumping)) {
Yspeed =-5;
jumping = 1;
}
Yspeed + = gravity;
while (_root.terrain.hittest (_x, _Y+_HEIGHT/2, True)) {
_y--;
yspeed = 0;
Jumping = 0;
}
if ((!_root.terrain.hittest (_x, _y+_height/2+1, True)) or (yspeed<0) {
_y + = Yspeed;
} else {
yspeed = 0;
Jumping = 0;
}
}
}

Final Demo Effect:

Finally a small game is done!! It's not hard!! I hope you will support me! Support the site. Thank you! I almost forgot!! All the above demo animation source files are provided to everyone!!!

  Click here to download the source file (remember to extract the password??) www.webjx.com)



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.