As a matter of fact, when talking about the game engine, many beginners will feel a sense of fear and will think this is a very advanced thing. The web game industry was once messy. Just like mobile games, non-computer students started their programmer journey after a short training.
This article explains the principle of the game engine in display rendering from a simple perspective and uses bitmap rendering for beginners to learn and communicate. If there are any shortcomings, I hope to point out.
The simplest graphics engine consists of three parts: Camera (CAMERA), canvas, and displayobject ).
To make it easy for everyone to understand, I directly ignore camera. Assume that the whole window of Flash Player is camera, and this is the case.
Flash itself is a graphics engine, but now I discard Flash Rendering and use bitmap to render our game in my own way to make it easier for everyone to understand.
First, define the canvas:
Suppose our game frame rate is based on flashplayer.
Public class canvas extends sprite {// bitmap on the canvas private VaR _ canvasbitmap: bitmap = NULL; // bitmap data on the canvas private VaR _ canvasbitmapdata: bitmapdata = NULL; // Display object private VaR _ objlist on the canvas: array = NULL; Public Function canvas () {addeventlistener (event. enter_frame, onenterframe); // assume that the canvas is 1000*600 in size _ canvasbitmapdata = new bitmapdata (1000,600); _ canvasbitmap = new Bitmap (_ canvasbitmapdata ); addchild (_ canvasbitmap);} // The heartbeat entry of each frame. Private function onenterframe (E: Event): void {Update (); Draw ();} // update private function Update (): void {for (var I: Int = 0; I <_ objlist. length; I ++) {_ objlist [I]. update () ;}// render private function draw (): void {clear (); For (var I: Int = 0; I <_ objlist. length; I ++) {_ objlist [I]. draw (_ canvasbitmapdata) ;}/// clear the bitmap data of the previous frame private function clear (): void {}// Add a display object private function addchild (OBJ: displayobject): void {_ objlist. push (OBJ );}}
Second, define displayobject
Public class displayobject {// display the object's own bitmap data private VaR _ bitmapdata: bitmapdata = NULL; Public var X: Number; Public var y: Number; Public var Height: number; public var width: Number; Public Function displayobject () {}// update Display object data public function Update (): void {}// render Display object public function draw (data: bitmapdata): void {data. copypixels (_ bitmapdata,); // copy your own bitmap data to the canvas }}
In this simple way, our Bitmap-based rendering engine comes out.
Interface Update ()
public function update():void
This interface and draw () are called at every frame. We usually use it to process something?
1. location update, that is, coordinate update. Every Display object has coordinates on the canvas. If it is a car with a uniform speed, we can follow its running speed, the time of each frame to obtain its current coordinate.
2. For collision detection, we can also put some collision detection here for processing.
3. for example, we have an action sequence table [1, 2, 3, 4, 5, 6]. We play a sequence value for each frame. What is the sequence value, determines the specific calculation method of _ bitmapdata.
Interface draw ()
Public Function draw (data: bitmapdata): void {data. copypixels (_ bitmapdata,); // copy your own bitmap data to the canvas}
The purpose of this interface isUpdate: A New bitmap data after calculation.The copypixels given above is incorrect. The purpose of this writing is to tell everyone that copying the bitmap data to the canvas, it is mainly related to the coordinates and width and height of the new bitmap data.
A simple game engine is actually like this. With Io, sound, socekt added, and then rationalized it with the MVC mode, we can use it to develop games.
Engine rendering, that is, draw (), consumes a lot of CPU resources. We often make some optimizations in these places, for example, if we have some display objects, we can obviously figure out that they will be fully archived by the previous display objects, can we consider not to render them during draw.
Now flash supports hardware acceleration, and adds a stage3d file in ActionScript. As long as we understand the principles of the image engine, it is not difficult for us to try to write a game engine to practice, it is also easy to understand a mature engine, such as Starling.
Principles of flash as Game Engine