Developing 2D games with flex2 (first try 2D performance)

Source: Internet
Author: User
There are more games developed by flash. But they are all games, especially games with small screens. It is said that the engine of Flash 9 is re-designed, and the efficiency is much higher than before. In addition, flex2/as3 is ideal Program The developer's ide and development language also support binary TCP communication. I think flex2/flash9 should be able to replace 2D game client development tools and engines.

Before using it, you still need to test the efficiency of a single engine. Of course, you can also experience the development efficiency.

Design a simple scenario: On a background, many UFOs are flying, and these UFOs are mixed with the background for a certain level of Alpha. The test screen is as follows:

The program structure is simple:

Sprite, bitmapasset, and movisponasset are the base classes provided by actionscript3. We need to write the exufo and ufosprite Sprite. Backclass and ufoclass are the entity classes of background images and UFO animations. They can be used only after being defined in the program.
Exufo class:

Package {
Import flash. display. * ;
Import flash. Events. * ;
Import MX. Core. bitmapasset;
Import flash. Text. textfield;
Import flash. utils. gettimer;

Public   Class Exufo extends Sprite
{
// Define embedded image categories
[Embed (Source = " 201734.jpg " )]
Private VaR backclass: class;
// Tcount is used to display the number of Sprite instances and tframerate is used to display the frame rate.
Private VaR tcount: textfield, tframerate: textfield;
Public Function exufo ()
{
// Create a background image
VaR Bak: bitmapasset =   New Backclass () As Bitmapasset;
Addchild (BAK );
// Create two textfields to display the sprite count and Frame Rate
Tcount =   New Textfield ();
Tcount. x =   20 ;
Tcount. Y =   20 ;
Tframerate =   New Textfield ();
Tframerate. x =   120 ;
Tframerate. Y =   20 ;
Addchild (tcount );
Addchild (tframerate );

// Add a mouse event (used to generate a new UFO)
Parent. addeventlistener (mouseevent. mouse_up, clickhandler );
// Add frame start event (used to calculate Frame Rate)
Parent. addeventlistener (flash. Events. event. enter_frame, enterframe );
}

// When the mouse button is released, add a UFO
Public Function clickhandler (E: mouseevent ): Void  
{
VaR T: ufosprite =   New Ufosprite ();

T. X = E. stagex;
T. Y = E. stagey;
T. Alpha = Math. Max ( 0.2 , Math. Random ());
Addchild (t );
}

Private VaR old: Number =   0 , Framecount: Number =   0 ;

// Count and display sprite count and Frame Rate
Public Function enterframe (E: Event ): Void
{
Tcount. Text =   " Sprites: "   + String ( This . Numchildren );

Framecount ++ ;

If (Old =   0 )
{
Old=Gettimer ();
Return;
}
VaR temp: Number = Gettimer ();
VaR MS: Number = Temp - Old;
If MS >   1000 )
{
VaR rate: Number = Framecount * 1000   / MS;
Old = Temp;
Framecount =   0 ;
Tframerate. Text =   " FPS: "   + String (rate). substring ( 0 , 7 );
}

}
}
}

Ufosprite class:

Package
{
Import flash. display. Sprite;
Import MX. Core. movisponasset;
Import flash. utils. * ;

Public   Class Ufosprite extends Sprite
{
// Define embedded UFO Animation
[Embed (Source = " Ufo.swf " )]
Private VaR ufoclass: class;

Public Function ufosprite ()
{
// Create a UFO Animation
VaR UFO: movisponasset =   New Ufoclass () As Movisponasset;
Addchild (UFO );
// Set scheduled events
Setinterval (timerhandler, 1000 / 30 );
}

Private VaR D: Number =   5 , DX: Number = D, DY: Number = D;
// Scheduled UFO flight
Public Function timerhandler (): Void  
{
VaR PW: Number = Parent. getchildat ( 0 ). Width;
VaR pH: Number = Parent. getchildat ( 0 ). Height;
X + = DX; y + = Dy;
If (X <=   0 ) Dx = D;
If (X > = PW - Width) dx =   - D;
If (Y <=   0 ) Dy = D;
If (Y > = PH - Height) dy =   - D;
}
}
}

compile
If flex2 SDK is used, enter
D: \ test \ exufo> mxmlc-use-network = true-default-size 800 600-default-frame-rate30 exufo. as
the execution result is:
loading configuration file C: \ Program Files \ Adobe \ flex builder 2 \ flex SDK 2 \ frameworks \ flex-config.xml
D: \ test \ exufo \ exufo.swf (84431 bytes)
If flex2 builder is used, create an actionscriptproject and then create an actionscriptfile (that is *. and then add "-default-size 800 600-default-frame-rate30 " to the compiler options ", the purpose is to ensure that the generated SWF file is 800*600 in size and the frame rate is 30 FPS as the benchmark.
Source Code contains the complete flex builder 2 project, which can be opened directly in flex builder 2.

Example (install the Flash 9 browser plug-in): Click the left mouse button once to add a UFO.

Download Sample files:
Http://images.cnblogs.com/cnblogs_com/ly4cn/exUfo.swf
CompleteCodeDownload:
Http://files.cnblogs.com/ly4cn/exUfo.rar

Flex2 SDK and flex2 builder trial download: http://www.adobe.com/cn/products/flex? Promoid = binv
Flex2 builder includes the flex2 SDK and a standalone Flash 9 player.

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.