Three ways to realize object motion in Flash

Source: Internet
Author: User
Tags final implement reference

The games now developed with Flash are more and more. Many friends also want to learn how to use Flash to make the game. You know, no matter in what game, the realization of object movement is the premise of the game, and different games in the way objects move differently. So, want to learn to make Flash game friends with me today first learn how to implement object movement in Flash several ways bar. Several methods are as follows:

Method One: Implement unit motion of an object by clicking a button

This method is generally used in games that require keystrokes. You can also add this code to an entity and then click the entity to achieve some kind of game effect.

1, open Flash, press the shortcut key "Ctrl + M" to open the [Movie Properties] panel. Set the scene size and background color according to your requirements.

2, press the shortcut key "Ctrl + F8" to create a new graphic symbol named graphic (Movie clip symbol is also OK).

3, press the shortcut key "Ctrl + R" To import an animal picture (the face of the animal in the image I import is facing to the right, so that it is more realistic to move). You can also draw a square or circle in the move movie scene. But the effect is less obvious.

4, press the shortcut key "Ctrl + F8" to create a new movie clip symbol named move, and drag the graphic symbol to frame 1th of the motion movie. Double-click Frame 1th to enter the function stop (). Right-click Frame 1th to select the copy Frames command. Then right click on frame 2nd and select paste Frames command.

5, select the picture in frame 2nd of the move movie, and perform the [modify]→[transform]→[flip horizontal] command to flip the picture horizontally. Note: The position of the movie in Frames 1th and 2nd should be the same (using the shortcut CTRL + ALT + I) to open the instance panel to set the x-axis y-coordinate of the picture.

6, back to the main scene, press the shortcut key "Ctrl + L" to open the library and hold down the left mouse button to drag the movie to the scene. Press the shortcut key "Ctrl + I" To open the instance panel and name the move movie MC.

7, click [Windows] Menu →[common libraries]→[bottons] to open the button symbol library. Select the gel left and gel right buttons in the (circle) VCR button Set, and drag them to the appropriate location in the scene.

8, right-click the Gel left button and enter the following code:

On (release) {
Telltarget ("_ROOT.MC") {//Tell the movie to be called, jump to frame 2nd
gotoAndStop (2); When you jump to the second frame, the movie moves in a different direction.
}
Movex = GetProperty ("/MC", _x); Attach the x coordinates of the movie in the scene to Movex
Vmovex = movex-10; The coordinate value is changed in 10 pixel units to realize the movement of the object.
SetProperty ("/MC", _x, Movex); Redefine the _x coordinate properties of a movie
}

9, right click the "Gel" button, enter the following code:

Click this button to jump to frame 1th. To flip a picture
On (release) {//The following code meaning reference above
Telltarget ("/MC") {
gotoAndStop (1);
}
Movex = GetProperty ("/MC", _x);
Movex = movex+10;
SetProperty ("/MC", _x, Movex);
}


The above two pieces of code are used to control the movement of objects around. The code that controls the vertical motion of the object can be written in conjunction with "Method two". The final effect is shown in Figure 1.

In addition, you can use the "onclipevent (MouseDown) {}" statement to implement the left mouse button to achieve the one-way movement of the object. Code references added in "{}" "Method One", very simple, the length of the relationship I do not have to say more. But there are some imperfections in the design of "method one". These we will solve in "method two".

Method Two: To achieve the unit motion of an object by tapping the direction key

This method is often used in games, especially RPG games, learn it is the design of RPG and many other games (such as Tetris) Foundation. An example is a keyboard key, you can change it to another key to make all the effects you want. The

1-5 step is the same as method one.

6, right-click the graphic picture in frame 1th of the move movie (right-click the picture, not the frame), and select the Actions command. Enter the following code:

Onclipevent (KeyDown) {//When the key is pressed, execute the following statement
if (key.getcode () = = Key.left) {
If the keystroke is ← key, the movie jumps to the 2nd frame to execute. To flip a picture
Telltarget ("/MC") {
gotoAndStop (2);
}
}
if (key.getcode () = = Key.right) {//if the keystroke is → Key executes the following statement
width = GetProperty ("/MC", _width); Attach the width of the film to the variable width
Movex = GetProperty ("/MC", _x);
Movex = MOVEX+WIDTH/10; With One-tenth of the film width as picture moving unit value
SetProperty ("/MC", _x, Movex);
}
if (key.getcode () = = Key.up) {//The following code meaning reference above
Height = GetProperty ("/MC", _height);
Movex = GetProperty ("/MC", _y);
Movex = MOVEX-HEIGHT/40;
SetProperty ("/MC", _y, Movex);
}
if (key.getcode () = = Key.down) {
Height = GetProperty ("/MC", _height);
Movex = GetProperty ("/MC", _y);
Movex = MOVEX+HEIGHT/40;
SetProperty ("/MC", _y, Movex);
}
}


7. Right-click the move movie in frame 2nd and select the Actions command. Enter the following code:

Onclipevent (KeyDown) {//The following code meaning reference above
if (key.getcode () = = Key.left) {
width = GetProperty ("/MC", _width);
Movex = GetProperty ("/MC", _x);
Movex = MOVEX-WIDTH/10;
SetProperty ("/MC", _x, Movex);
}
if (key.getcode () = = Key.right) {
If the keystroke is the → key, it tells the movie to jump to frame 1th execution. To flip a picture
Telltarget ("/MC") {
gotoAndStop (1);
}
}
There are two pieces of code to control the motion of the picture above and above
}


8, back to the main scene, press the shortcut key "Ctrl + L" Open the library and hold down the left mouse button to drag the movie move to the scene. Press the shortcut key "Ctrl + I" To open the instance panel and name the movie move the MC.

In this way, the film can be done up and down around the movement. Don't believe you can press the keypad on the arrow keys to try. The final effect is shown in Figure 2. But we found another problem, that the entity is moving in one direction, and then it will run without a trace. What about it? Don't worry, let's add the following code to the entity so that the entity does not go out of bounds:

Right-click the graphic picture in frame 1th of the move movie, and then in the "Movex = MOVEX+WIDTH/10;" Code below to add the following code:

if (movex>=300-width) {//Assuming the scene width of the movie is 300;
Movex=300-width; This setting is because the scene is the point at the top left corner (0,0)
}
Right-click the graphic picture in frame 2nd of the move movie, and then in the "Movex = MOVEX-WIDTH/10;" Add the following code:
if (movex<=0) {
movex=0;
}


In the vertical direction of the movement of the code you can write it yourself, I do not say anything here.

In the movie, you can also change the picture to animation. If you want to do your own character animation (if you can't do it yourself, remove the GIF animation.) You got it! , you can make 8 pictures, two a group, the left and right direction of a total of four groups, and then add a few frames in the film OK (you can explore yourself). This will not be like the introduction of GIF animation after the film has been moving non-stop.

method Three, drag and click through the mouse to achieve arbitrary movement of objects

The implementation of drag-and-drop method is very simple, but its use in the game is very wide. such as puzzles, push boxes, play mice, chess and other games have to use this method.

Add a movie called "MC". Then right click the mouse to enter the following code:

Onclipevent (MouseDown) {//execute the following code when the mouse is pressed
StartDrag (MC); Move the movie with the mouse and follow the mouse
}
Onclipevent (mouseUp) {//When the mouse is released execute the following code
Stopdrag (); Movie Out of the mouse
}

The function of this code is to click the mouse, where the film appears.

Add a movie called "MC". Then right click the mouse to enter the following code:

Onclipevent (MouseDown) {
x = _root._xmouse; Attach the value of the mouse's X coordinate value to the variable x in the current scene
y = _root._ymouse; Attach the value of the y-coordinate of the mouse in the scene to the variable y
SetProperty ("/MC", _x, X);
SetProperty ("/MC", _y, y);
}

Well, that's pretty much it. As long as we master the above several physical movement methods, combined with some other flash production and programming means, we will be able to develop their own flash game. Here, I wish you all a smooth sailing on the "Broad Road" of Flash.



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.