Use flash as to create a box to avoid collision

Source: Internet
Author: User

Use flash as to create a box to avoid collision

In this Actionscript 3 tutorial, I will show you how to create a box to avoid a ball collision. View the above effect (click the white ball to make it exercise). Let's get started right away!

Set environment

1. Create a new Flash Actionscript 3 video (340x200 ).
2. Draw a rectangle on the stage. Set the size to 20x20.
3. Convert the rectangle to MC. Give it a name you like and move the registration point to the center!
4. Set the MC class name in the connection property to "Box". If you are unfamiliar with the MC connection property, please refer to the extension class tutorial of Actionscript 3.
5. Draw a 10x10 garden on the stage.
6. Convert the garden to MC. Give it a name you like and move the registration point to the center!
7. Set the MC class name in the connection property to "Ball ".
8. Remove the garden and rectangle from the stage.
Enter Actionsctipt9. enter the following Actionscript code in the first frame.
// This array contains all the boxes
Var boxes: Array = new Array ();
// Set the ball speed
Var ballSpeed: Number =-4;
// Add 8 boxes to the stage in a loop
For (var I = 0; I <9; I ++ ){
// Create a box
Var box: Box = new Box ();
// Add a location
Box. y = 150;
Box. x = box. width * I * 1.5 + 40;
// Add a box to the array
Boxes. push (box );
// Add a box on the stage
AddChild (box );
}
// Create a box and set its right side
Var ball: Ball = new Ball ();
Ball. x = 320;
Ball. y = 155;
// Make the ball look like a button (hand-shaped cursor)
Ball. buttonMode = true;
// Add the ball to the stage
AddChild (ball );
// When the user clicks the ball
Ball. addEventListener (MouseEvent. CLICK, ballClicked );
// This function is called when the user clicks the ball
Function ballClicked (e: Event): void {
// Add ENTER_FRAME during the animation process
AddEventListener (Event. ENTER_FRAME, enterFrameHandler );
}
// The function is called at each frame.
Function enterFrameHandler (e: Event): void {
// Move the ball 2 pixels left
Ball. x + = ballSpeed;
For (var I = 0; I <boxes. length; I ++ ){
// Obtain a box from the array
Var box: Box = boxes as Box;
// Detect the x distance from the ball to the box
Var distX: Number = ball. x-box. x;
// The ball comes from the right side
If (distX <50 & distX> 0 & ballSpeed <0 ){
// Push the box
Box. y-= 2;
}
// The ball leaves the left
Else if (distX <50 & distX <0 & ballSpeed <0 ){
// Fall if the ball is not in the original position
If (box. y <= 150 ){
Box. y + = 2;
}
}
// The ball comes from the left
If (distX <0 & distX>-50 & ballSpeed> 0 ){
// Push the box up
Box. y-= 2;
}
// The ball leaves the right side
Else if (distX <50 & distX> 0 & ballSpeed> 0 ){
// Fall if the ball is not in the original position
If (box. y <= 150 ){
Box. y + = 2;
}
}
// If the ball is left, the direction is changed.
// Or the right edge
If (ball. x + 5> stage. stageWidth | ball. x-5 <0 ){
// Reverse speed
BallSpeed * = (-1 );
}
}
}
Test your video. I hope you can learn something new here. Remember, if you have any questions, please do not hesitate to ask questions in the forum.

The following code is provided for study:

Var boxes: Array = new Array ();
Var ballSpeed: Number =-4;
For (var I = 0; I <9; I ++ ){
Var box: Box = new Box ();
Box. y = 150;
Box. x = box. width * I * 1.5 + 40;
Boxes. push (box );
AddChild (box );

Var ball: Ball = new Ball ();
Ball. x = 320;
Ball. y = 155;
Ball. buttonMode = true;
AddChild (ball );
Ball. addEventListener (MouseEvent. CLICK, ballClicked );
Function ballClicked (e: Event): void {
AddEventListener (Event. ENTER_FRAME, enterFrameHandler );

Function enterFrameHandler (e: Event): void {
Ball. x + = ballSpeed;
For (var I = 0; I <boxes. length; I ++ ){
Var box: Box = boxes [I];
Var distX: Number = ball. x-box. x;
If (distX <50 & distX> 0 & ballSpeed <0 ){
Box. y-= 2;

Else if (distX <50 & distX <0 & ballSpeed <0 ){
If (box. y <= 150 ){
Box. y + = 2;


If (distX <0 & distX>-50 & ballSpeed> 0 ){
Box. y-= 2;

Else if (distX <50 & distX> 0 & ballSpeed> 0 ){
If (box. y <= 150 ){
Box. y + = 2;


If (ball. x + 5> stage. stageWidth | ball. x-5 <0 ){
BallSpeed * = (-1 );


}

Effect

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.