Flex and Flash Actionscript Development Game 2

2010-11-03  来源:本站原创  分类:Flash  人气:121 

Easier to make money at home

Copy the code first thing to note currentState property to join. As mentioned before, currentState attribute defines the current state of the program (state). We will currentState is set to MainMenu, said the program is started by the MainMenu state.

We also joined the mx: States elements. This element defines a program that contains all the sub-state. Here we define two states MainMenu and Game. In the MainMenu state, the end user will see the start screen. The Game states that the game itself.

The two mx: State all have name attributes. Change the currentState property value, we can switch between the completion status. Game state includes enterState and exitState two events. We can use to add two events for this response function approach to the manual "synchronize" the internal logic of the game. You see, we use EnterGame function to start the GameObjectManager (after that) instance, and set the internal flag inGame is true. This flag is used in the game rendering loop, indicating the need to render the game screen to screen. ExitGame method is simple and will inGame flag is set to false, the user interface is not visible.

Remember I mentioned that Flex in a variety of state can be switched? MainMenu state shows its simple implementation. mx: AddChild node is used to add graphical interface elements. Here, we add a button so that users can click into the game. When we left the MainMenu state, Flex will automatically remove the button without any other code and settings.

In order to render the screen to the screen, we add a mx: Canvas element. canvas (more specifically, its graphics property) acts as a double-buffered rendering process in the pre-buffering role. Rear buffer exists in the GameObjectManager class. In the enterFrame function, we call GameObjectManager the enterFrame method, which allows the screen to draw to the rear buffer. When a drawing is completed, we use the canvas in the graphics object clear, beginBitmapFill, drawRect and endFill method to draw to the canvas.

GameObjectManager.as package

{

import mx.core .*;

import mx.collections .*;

import flash.display .*;

public class GameObjectManager

{

/ / Double buffer

public var backBuffer: BitmapData;

/ / Colour to use to clear backbuffer with

public var clearColor: uint = 0xFF0043AB;

/ / / Static instance

protected static var instance: GameObjectManager = null;

/ / The last frame time

protected var lastFrame: Date;

static public function get Instance (): GameObjectManager

{

if (instance == null)

instance = new GameObjectManager ();

return instance;

}

public function GameObjectManager ()

{

if (instance! = null)

throw new Error ("Only one Singleton instance should be instantiated");

backBuffer = new BitmapData (Application.application.width, Application.application.height, false);

}

public function startup (): void

{

lastFrame = new Date ();

}

public function shutdown (): void

{

}

public function enterFrame (): void

{

/ / Calculate the time since the last frame

var thisFrame: Date = new Date ();

var seconds: Number = (thisFrame.getTime () - lastFrame.getTime ()) / 1000.0;

lastFrame = thisFrame;

drawObjects ();

}

protected function drawObjects (): void

{

backBuffer.fillRect (backBuffer.rect, clearColor);

}

}

}

Copy the code GameObjectManager objects bear the responsibility for managing various elements of the game, including enemies, players and various background elements. It also determines which elements in the rear buffer needs to draw. If you remember the pre-buffer is implemented by the canvas element, the most simple way is to canvas as a child node directly into the Application. BitmapData object from the rear to achieve the buffer, so that we can quickly direct the operation of pixels to draw the final image.

clearColor property that is used to color the scene before the establishment of a good, clear color of the rear buffer. Eventually the whole post-game element of the buffer will be overwritten, making clearColor completely unrelated, but now it is very important, because it will create a final frame. 0xFF0043AB representative of dark blue. Alpha value of the first two representatives, after the six representatives of RGB values.

Static instance property is used to implement the singleton pattern. Our program requires only a GameObjectManager instance, refer to this static property will ensure that we will only create a GameObjectManager instance. The Singleton pattern is very common programming paradigm, although Actionscript does not support protected constructor, but the singleton pattern is still very useful (once you see the instance attributes, this class is probably designed in accordance with the singleton pattern).

lastFrame property stores the last frame rendering time of completion. Track this time, we know that the last frame in the time interval between the current frame, this allows us to update the game elements. Although we do not have any game elements, in the enterFrame method, we calculated the time between frames in the frame. lastFrame startup time is when the call is reset. This is because when the whole state of the game when not in Game state, GameObjectManager not updated. If we do not reset the lastFrame, the first frame to the next level will be equal players in the off time spent between the menu. Best to avoid the termination of the player out in the first frame.

Therefore, we done? In order to achieve a variety of state, we created a Main Menu screen, use the button to enter the game. Game, we achieved double-buffering model, you can draw a blue background. Next, we will start to do interesting things, to draw something on the screen.

Easier to make money at home

相关文章
  • Flex and Flash Actionscript Development Game 2 2010-11-03

    Easier to make money at home Copy the code first thing to note currentState property to join. As mentioned before, currentState attribute defines the current state of the program (state). We will currentState is set to MainMenu, said the program is s

  • Flex and Flash Actionscript Development Game 1 2010-11-03

    Easier to make money at home In the first article of the series, we created the Flex classes. This section, we will add a number of states and a double-buffered rendering process. States just as it did to explain his name: it represents a program tha

  • flex / flash application development on behalf of the do flash / flex work 2011-01-09

    Undertake professional flex / flash application development Make flash work on behalf of / graduation http://item.taobao.com/auction/item_detail.htm?item_num_id=9033573337

  • FLASH + JAVA development of real-time network game [Programming articles] with Socket API 2009-10-13

    FLASH + JAVA development of real-time network game [Programming articles] with Socket API (Color fox 【ColorFox , changsir 】 : [email protected] QQ: 146646) In this paper, as long as two parts: FLASH programming and Java programming, this article only

  • Relations and differences between Flex and Flash 2010-07-20

    Flash is not just a simple vector animation tool, but a scripting language with ActionScript functions and positioning in the evolving web application development tool. Flash MX, when early on has become a strategic tool for RIA macroemdia promotion.

  • Flex in Flash through the use of Google Maps 2009-04-22

    Put Google map embedded into web pages has now very easy to use API of Google Maps can be manipulated through JavaScript. But sometimes we need to come from Google's map service is embedded in Flash, this time should not the easy use of JavaScript. F

  • Zhou Yun's FLASH game development classes - preparation before making the game 2010-04-04

    Recently tired, although there are many friends and so brave in the 2, but I do not mind development. Idle is idle, to open a path to just FLASH game development enthusiasts write something. Veterans who do not have to read, huh, huh, laughing stock

  • flex and flash case of a call between 2010-04-16

    flex and flash case of a call between Note: This method loads the swf will be displayed on the page. Steps: first create an animation, After testing the generated swf file to flex the source directory, then you can call directly in flex. <? Xml versi

  • flex and flash call between two cases 2010-04-16

    flex and flash call between two cases Note: This method calls the swf does not need the front page display, but simply to call the swf inside the method or property The method is in the process of loading swf can not simply place the generated swf fi

  • flex and flash interaction of the LocalConnection 2010-10-14

    (First written about in this simple, back to finishing it) flex and flash swf files are all compiled, so the interaction between the more convenient, but note that version of the problem, if you are using AS3.0, that when the flash player will automa

  • [Transfer] flex and flash on the top two platforms using AMFPHP class mapping problem for 2010-10-29

    AMFPHP is a PHP remote call (RPC, Remote Procedure Call) tool. It enables seamless communication PHP with the following technologies: (1) Flash and Flex Remoting (2) JavaScript JSON and Ajax JSON (3) XML and XML-RPC AMFPHP works client (Flash / Flex)

  • Flex and Flash AS2 interactive communication between the 2011-05-17

    Flex and Flash AS2 interactive communication between the Can do a lot of rich Flash animation, which is unparalleled in Flex, but Flex does not mean that can not be achieved, it is a bit complicated, so we want the best you can hope is that any use o

  • as3 volume control, Flash ActionScript Sound 2011-05-22

    http://bbs.9ria.com/viewthread.php?tid=84152&extra=page% 3D1% 26amp% 3Borderby% 3Ddateline% 26amp% 3Bfilter% 3D2592000 News Type: Translation Source page: http://www.flashperfection.com/tutorials/Actionscript-knob-volume-control-as3-38351.html Inform

  • Flash ActionScript编程的原则 2011-05-31

    在学习 Flash ActionScript编程的时候,尤其是对于初学者来说应该多注意下面的一些 Flash ActionScript编程的原则会对学习 Flash ActionScript编程很有帮助. 1.熟悉Flash ActionScript编程添加动作的三种方式 在帧上加,在MC上加,在按钮上加.用play.stop.goto这三个命令反复练习.不要用太多的命令给自己吃迷晕药.用实例练,不要空练. 2.熟悉给按钮和MC添加动作的方法. 3.学习Flash ActionScript编程最

  • Flex Integration of J2EE development in mind + source code to download small 2010-03-09

    Flex development on the language and structure, introduced a well-known hierarchical structure, highlighting the view layer (flex to achieve), the service control layer and the domain model layer, and in accordance with the strict hierarchical, high

  • Flash ActionScript 2.0 Essentials 2010-04-03

    This Translator: egoldy Article Source: [Url = http://www.ultrashock.com/] http://www.ultrashock.com [/ Url] Article Type: Translation [1b] ActionScript 2.0-Introduction [/ 1b] With the introduction of FLASHMX2004, Macromedia has introduced a new typ

  • [Want to start a full SmartRcp] Flex + Java GUI program development 2010-06-18

    Ready to use before the development of GUI Java Desktop program, online google for a long time, most of SWT and Swing, accidentally discovered the OpenSource of SmartRcp, listening to the author's introduction, like that. Be prepared to learn a simpl

  • How to use Flex Builder Flash CS3 components 2010-11-11

    After several tests I found that direct reference to the Flash of the SWC package can only get the appropriate code hints, and can not completely solve the problem of pure FlexBuilder compiler. So if you want to completely abandon the FLA, they want

  • Why companies are reluctant to use domestic products, Flex as the main development platform and language 2011-05-07

    Turn http://www.k-zone.cn/zblog/post/why-not-use-flex.html More than one person on this subject asked me, and these people asked me about sharing: a variety of start-up company's CEO, CTO. Want to do the kinds of Flex as a product manager for platfor

  • FLASH / ActionScript Performance Optimization 2011-05-31

    One. Graphics optimization 1. To reduce both the number of objects on the screen 2. To minimize the scope of the screen needs repainting. 3. Try to avoid full-screen scroll 4. To keep the number of frames at 16-20, each frame is continuous, than woul