Flash Game Design Notes: mouse games (4)

Source: Internet
Author: User
Tags addchild

Flash Game Design Notes: mouse games (1)

Http://blog.csdn.net/hero82748274/archive/2009/02/11/3878304.aspx

 

Flash Game Design Notes: mouse games (2)

Http://blog.csdn.net/hero82748274/archive/2009/02/16/3897058.aspx

 

Flash Game Design Notes: mouse games (3)

Http://blog.csdn.net/hero82748274/archive/2009/02/17/3899443.aspx

 

I. Create a turret

Previously, I wrote about the creation of some guns. This turret can follow the Rotation Angle of the mouse. Its principle is very simple. Math. atan2 (Y, x) is used to find the corresponding angle.

For example: (fire is the MC in the scenario)

Fire. Rotation = math. atan2 (mouseY-Fire.y, mousex-fire. X) * 180/Math. Pi;

 

Flash production of rotating turret: http://blog.csdn.net/hero82748274/archive/2008/11/04/3219431.aspx

 

Since rotation is an angle, radians must be converted to angle units.

 

Private function run (Event: Event): void <br/>{< br/> dx. TEXT = string (mousex); <br/> dy. TEXT = string (Mousey); <br/> photo. X = mousex; <br/> photo. y = Mousey; <br/> fire. rotation = math. atan2 (mouseY-Fire.y, mousex-fire. x) * 180/math. pi; </P> <p>}

 

We added some video clips named fire to the scene.

 

Ii. Bullet Running Track

Create a bullet class named ball in the FLA file. When we press the mouse to generate an object that runs at an angle and speed, because the speed is vector, two different axes can be decomposed. One is Dx and the other is dy.

 

DX = speed * Cos (angle );

DY = speed * sin (angle );

 

Physically H = Dy * t;

S = DX * t; run a constant speed motion at a certain angle within a certain period of time

 

 

Physical on flash:

 VaR ball: ball = new ball (); <br/> addchild (ball); <br/> ball. X = fire. X + math. cos (angle2 * Math. PI/180) * 85; <br/> ball. y = fire. Y + math. sin (angle2 * Math. PI/180) * 85;

 

Here we are just the initialization location of the computer's bullet:

 

 

2.1 bullet speed component:

Ball. VX = (speed + 1) * 3 * Math. cos (angle2 * Math. PI/180); // speed of the bullet in the X direction. The trick is here <br/> ball. vy = (speed + 1) * 3 * Math. sin (angle2 * Math. PI/180); // speed of the bullet in the Y direction <br/>

 

To put it simply: Calculate the two parts of speed based on the angle;

Ball. VX = speed * Math. Cos (angle * Math. PI/180 );

Ball. Vy = speed * Math. Sin (angle * Math. PI/180 );

 

 

2.2 bullet run

When we specify the initial angle and initial speed of a bullet, it will continue in this direction in an ideal state (without gravity introduced.

 

Next, what we do is to allow bullets to run at this speed and direction;

Ball. addeventlistener (event. enter_frame, ballmove); Use the event. enter_frame event to render each frame.

Private function ballmove (E: Event): void <br/>{< br/> var myball: movieclip = E. currenttarget as movieclip; <br/> myball. X + = myball. VX; <br/> myball. Y + = myball. vy; <br/>}

In this way, a pixel Shift is generated for each frame, and the coordinates of the bullet are also changed.

In physics, S = V * t; displacement = speed X time

 

In flash, the time is actually hidden, instead of frame.

Object. x + = speed * t and T is the Hidden Frame

Object. Y + = speed * t

 

 

 

 

2.3 introduce gravity

 

 Private function ballmove (E: Event): void <br/>{< br/> var myball: movieclip = E. currenttarget as movieclip; <br/> myball. vy + = 2; <br/> myball. X + = myball. VX; <br/> myball. Y + = myball. vy; <br/>}

 

Since the bullet is in the air, it is subject to gravity and other resistance, assuming we ignore other resistance of the bullet, the use of gravity allows the bullet to be in the gravity process

It runs on a parabolic motion track, so we need to add the acceleration of gravity in the velocity component of the Y axis. Assume 2.

In this way, we can simulate a simple gravity effect.

 

Effect:

Cool: Hey hey

 

Add bitmap

 

 

 

Code List:

Package <br/> {<br/> Import flash. display. movieclip; <br/> Import flash. events. *; <br/> Import flash. text. textfield; <br/> Import flash. UI. mouse; <br/> public class example3 extends movieclip <br/> {<br/> private var DX: textfield; <br/> private var DY: textfield; <br/> private var photo: mousephoto; <br/> private var power: Int = 0; <br/> private var maxpower: Number = 100; <br/> private var angle: Int =-45; <br/> private var angle2: int; <br/> private var speed: Int = 0; <br/> Public Function example3 () <br/>{< br/> addeventlistener (event. enter_frame, run); <br/> stage. addeventlistener (mouseevent. mouse_down, mousedown); <br/> stage. addeventlistener (mouseevent. mouse_up, mouseup); <br/> Init (); <br/>}< br/> private function Init (): void <br/>{< br/> dx = new textfield; <br/> addchild (dx); <br/> dx. width = 50; <br/> dx. height = 20; <br/> dx. X = 30; <br/> dx. y = 30; <br/> dx. border = true; </P> <p> DY = new textfield; <br/> addchild (dy); <br/> dy. width = 50; <br/> dy. height = 20; <br/> dy. X = 30; <br/> dy. y = 53; <br/> dy. border = true; <br/> mouse. hide (); // hide the mouse <br/> photo = new mousephoto; // create a map that replaces the mouse <br/> addchild (photo ); <br/>}< br/> private function run (Event: Event): void <br/>{< br/> dx. TEXT = string (mousex); <br/> dy. TEXT = string (Mousey); <br/> photo. X = mousex; <br/> photo. y = Mousey; <br/> fire. rotation = math. atan2 (mouseY-Fire.y, mousex-fire. x) * 180/math. pi; </P> <p >}< br/> private function adds (): void <br/>{< br/> stage. addeventlistener (event. enter_frame, addnum); <br/>}</P> <p> private function addnum (Event: Event ): void <br/>{< br/> speed = getscale (); <br/> trace (speed); <br/> mypower. scalex = speed * 10; <br/>}</P> <p> private function getscale (): int <br/> {<br/> If (power> =-1) <br/> {// angle =-45; <br/> power + = math. tan (angle * 180/math. pi); <br/> If (power = 10) <br/>{< br/> angle =-135; <br/>}< br/> If (power = 0) <br/>{< br/> angle =-45; <br/>}< br/> return power; <br/>}</P> <p> private function mousedown (Event: mouseevent ): void <br/>{< br/> adds (); <br/>}< br/> private function ballmove (E: Event ): void <br/> {<br/> var myball: movieclip = E. currenttarget as movieclip; <br/> myball. vy + = 2; <br/> myball. X + = myball. VX; <br/> myball. Y + = myball. vy; <br/>}< br/> private function mouseup (Event: mouseevent): void <br/>{< br/> stage. removeeventlistener (event. enter_frame, addnum); <br/> shoot (); <br/>}</P> <p> private function shoot (): void <br/> {<br/> angle2 = fire. rotation; <br/> var ball: ball = new ball (); <br/> addchild (ball); <br/> ball. X = fire. X + math. cos (angle2 * Math. PI/180) * 90; <br/> ball. y = fire. Y + math. sin (angle2 * Math. PI/180) * 90; <br/> ball. VX = (speed + 1) * 3 * Math. cos (angle2 * Math. PI/180); // speed of the bullet in the X direction. The trick is here <br/> ball. vy = (speed + 1) * 3 * Math. sin (angle2 * Math. PI/180); // speed of the Y direction of the bullet </P> <p> ball. addeventlistener (event. enter_frame, ballmove); <br/>}< br/>}

 

If you are tired, modify it tomorrow.

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.