As3 Summary (6)

Source: Internet
Author: User

Let's take a look at the summary in the as3 game programming, which is very classic.

1. Frame Rate event Motion

It is very easy to change their position on the screen by setting the coordinates of x and y in the video clip. If
We want to make this video clip move and set a specific speed for it, then we need to use
Enter_frame event. For example, you can draw any image on the stage and convert it into a video clip,
The class name is hero. Next, we use a small program to copy a copy of hero in the library and let it move each frame.
The Code is as follows:
VaR hero: Hero = new hero ();
Hero. x = 50;
Hero. y= 100;
Addchild (HERO );

Addeventlistener (event. enter_frame, animatehero );
Function animatehero (Event: Event ){
Hero. x ++;
}
Now, the test video shows that the hero role starts from the screen coordinate (50,100), each frame is represented as
To the right.

Note: You can also control the object motion speed by changing the Frame Rate on the stage attribute panel. The default value is
12 frames per second. You can change the value to any number within [1, 60. In fact, if you select the Frame Rate
60 does not mean that the video runs 60 frames per second. If the video clip is large and your machine is slow,
Then, he will not reach 60 frames. In this regard, we have a better way to solve this gap.
Time-based animation.

During the game process, we often encounter task walking. How can we solve this problem in flash? Don't worry. In fact, this is very simple. First, use an animation to make a video clip,
This video clip has 8
Frame, from the second frame to the eighth frame is a cycle of walking. Each frame represents a different step, and the first frame is retained as the standing position. After the preparation, enter the following frame rate events and frame rate functions in the timeline.
Within a few minutes, we will create a role to move each frame 7 pixels horizontally. The following condition code is used to check the current frame number of the video clip. If it is 8
If the frame is to be returned, the second frame continues to motion. Otherwise, the frame continues.

The source code is as follows:
VaR hero: Hero = new hero ();
Hero. x = 100;
Hero. y= 200;
Addchild (HERO );
Addeventlistener (event. enter_frame, animatehero );
Function animatehero (Event: Event)
{
Hero. x + = 7;
If (hero. currentframe = 8)
{
Hero. gotoandstop (2 );
} Else
{
Hero. gotoandstop (hero. currentframe + 1 );
}
}

2. Use a timer to control the motion of a video.

A timer is like a clock we use. You can also create a timer to call a function every second.
When you declare a timer, a timer object is created. You need to set the time interval in milliseconds and the cycle
The number of loops. If the value is 0, the cycle is countless times.

The following code creates a timer object and calls the timerfunction once every 1 s. The source code is as follows:
// Create a timer object and call the timerfunction every second
VaR mytimer: timer = new timer (1000 );
Mytimer. addeventlistener (timerevent. Timer, timerfunction );
// Draw a black filled dot in the function
Function timerfunction (Event: timerevent)
{
This. Graphics. beginfill (0x000000 );
// Currentcount is the total number of times the timer is triggered after it starts from 0. If the timer has been reset, only accounting
Number of triggers After resetting.
This.graphics.drawcircle(event.tar get. currentcount * 10,100, 4 );
}
// Start timing
Mytimer. Start ();

3. Time-based frame rate Animation

The principle of this method is to declare an integer variable to store the time after Flash Player initialization,
Then, the listener event is used to call the listener function, and the time difference between the current frame and the previous frame is calculated in the listener function, multiplied
A proportional coefficient is applied to the coordinates of the video clip to change the motion of the object.

Declare a variable to store the initial
The time elapsed after the start of Flash Player, in milliseconds. Then, use the gettimer () function to return
The time is passed to the declared variable lasttime.

The gettimer () function is used to return the milliseconds that have elapsed since the start of playing the SWF file.
VaR lasttime: Int = gettimer ();

Then, we use the enter_frame event to listen and declare an animateball listener function to call it.
Addeventlistener (event. enter_frame, animateball );

Function animateball (Event: Event ){
VaR timediff: Int = gettimer ()-lasttime;
Lasttime + = timediff;
Ball. x + = timediff *. 1;
}

4. Drag a video

Now let's take a look at another method of mobile film.
Method: when a user clicks a video, the drag starts. When the user releases the mouse, the drag stops. When you click a video
You can listen for the mouse_down event on the video. However, when you release the mouse, you cannot use this video as a listener.
Object, but must be replaced by the stage. Only in this way, when the mouse is not on the video, as long as
Click the mouse_up event to stop dragging. The Code is as follows:
// Set the listener
Mascot. addeventlistener (mouseevent. mouse_down, startmascotdrag );
Stage. addeventlistener (mouseevent. mouse_up, stopmascotdrag );
Mascot. addeventlistener (event. enter_frame, dragmascot );
Another factor is the cursor offset. we allow the mouse to drag from any point of the video. First, we need
Calculate the local coordinates of the point on the mouse over the video relative to the center of the video, and store the point to clickoffset.
In the variable, we can also use this variable to calculate whether there are any drag and drop operations at the moment. If yes, clickoffset will be set
A vertex. Otherwise, null is assigned to it.
// Offset of the cursor position relative to the video
VaR clickoffset. loint = NULL;
When a user clicks a video, the offset is obtained by the localx and localy of the click event.
// When you click
Function startmascotdrag (Event: mouseevent ){
Clickoffset = new point (event. localx, event. localy );
}
When the mouse is released, this point is set to null.
// When the mouse is released
Function stopmascotdrag (Event: mouseevent ){
Clickoffset = NULL;
}
For the dragmascot function called by the frame rate event, if this clickoffset is not a null value, we will set this
The position of the film is the position of the current cursor minus the offset.

// Frame Rate Animation
Function dragmascot (Event: Event ){
If (clickoffset! = NULL ){
Mascot. x = mousex-clickoffset. X;
Mascot. Y = mousey-clickoffset. Y;
}
}
Test the video. Click the video with the mouse and drag it from the same point to see how the clickoffset is.
.
You can also use startdrag () and stopdrag () to stop the drag process. The Code is as follows:
Mascot. addeventlistener (mouseevent. mouse_down, startmascotdrag );
Stage. addeventlistener (mouseevent. mouse_up, stopmascotdrag );
Function startmascotdrag (Event: mouseevent): void
{
Mascot. startdrag ();
}
Function stopmascotdrag (Event: mouseevent): void
{
Mascot. stopdrag ();
}

5. Collision Detection

In a game, it is very useful to detect the collision between many objects moving on the screen.
As3.0 contains two collision detection functions. The hittestpoint function is used to detect whether a point or a display object has occurred.
The hittestobject function is used to detect whether two display objects have collided.
Specifically, when using the hittestobject function as the method of the video, you must pass in the reference of another video as a parameter,
The format is as follows:
Sprite1.hittestobject (sprite2)
True is returned if two films collide. Otherwise, false is returned. However, this detection method is very accurate.
Low, mainly used to detect the collision of rectangular films; hittestpoint function is used to detect whether two films touch each other.
You must first define two number-type parameters as the detection point. Returns true or
False. In addition, the third parameter is optional and its value is of the boolean type. The default value is false. Check the video edge.
Specifies whether the frame matches the vertex. If it is set to true, it is used to check whether the actual shape of the video matches the vertex.
Next, we will use an example to verify the usage of these two functions. First, we will plot two dynamic texts on the stage,
Name messagetext1 and messagetext2 respectively. Then draw a crescent moon and a star video clip,
The instance names are Crescent and star respectively. The Code is as follows:
Addeventlistener (event. enter_frame, checkcollision );

Function checkcollision (Event: Event)
{
// Check whether the mouse pointer is in touch with a crescent video
If (Crescent. hittestpoint (mousex, Mousey, true ))
{
Messagetext1.text = "hittestpoint: yes ";
} Else
{
Messagetext1.text = "hittestpoint: No ";
}
// Use the star coordinates as the mouse to follow
Star. x = mousex;
Star. Y = Mousey;
 
// Check whether the star and crescent shape touch each other
If (Star. hittestobject (Crescent ))
{
Messagetext2.text = "hittestobject: yes ";
} Else
{
Messagetext2.text = "hittestobject: No ";
}
}

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.