Flash Game Design Notes: walking by character

Source: Internet
Author: User

 

 

 

At present, I still won't make a small game. You need to know a lot about games. Flash is just an entertaining thing. Don't blame me for being misled.

Well, before we make this thing, this article has already been repeated, but it is just a demo. Use the keyboard to control the movement of a person.

The above is a transparent PNG bitmap. There are four different directions. We can see that the upper, lower, and left are complete. Now. Let's take a look at this together.

This does not involve strong object-oriented and reusability. So pay attention to the following code. The purpose here is to play the flash together.

 

So much nonsense. With this image, I plan to cut the image into 16 parts by copying pixels, each representing an image that is not synchronized in different directions.

 

First, copy the bitmap through a class. Returns a two-dimensional array. Note that the class can be modified. It is relatively simple to release here. No extension

 

 Package COM. game <br/> {<br/>/* returns a two-dimensional array */<br/> // version 1.0 <br/> Import flash. display. sprite; <br/> Import flash. display. bitmap; <br/> Import flash. display. bitmapdata; <br/> Import flash. geom. *; <br/> public class bitmapsplice <br/> {<br/> private var step: array = new array (); // number of access steps array <br/> Public Function bitmapsplice () <br/>{</P> <p >}< br/> // cut <br/> Public Function splice (bitmapdata: bitmapdata, titlewidth: uint, titleheight: uint): array <br/>{< br/> var iwidth: Int = bitmapdata. width/titlewidth; <br/> var iheight: Int = bitmapdata. height/titleheight; <br/> for (var I: uint = 0; I <titleheight; I ++) <br/>{< br/> var array: array = new array (); <br/> for (var j: uint = 0; j <titlewidth; j ++) <br/>{< br/> var tempbmp: bitmapdata = new bitmapdata (iwidth, iheight, true, 0x00000000); <br/> tempbmp. copypixels (bitmapdata, new rectangle (J * iwidth, I * iheight, iwidth, iheight), new point (0, 0); <br/> array. push (tempbmp); <br/>}< br/> This. step. push (array); <br/>}< br/> bitmapdata. dispose (); </P> <p> return this. step; </P> <p >}< br/>}

 

Through this class, we get a two-dimensional array. Next we will use a keyboard for control.

For demonstration purposes, it is just a convenient demonstration. You can change it. Later, we have time to release other versions. Because it is not a game, we can only play with it and learn the filling bitmap method in it.

Package <br/> {<br/> Import flash. display. movieclip; <br/> Import flash. display. sprite; <br/> Import flash. events. *; <br/> Import flash. display. bitmap; <br/> Import flash. display. bitmapdata; <br/> Import flash. UI. keyboard; <br/> Import COM. game. bitmapsplice; <br/> public class main extends movieclip <br/>{</P> <p> private var array: array; // Save the image sequence <br/> private var bitmap: bitmapsplice; <br/> private var step: I Nt = 0; // number of steps <br/> private var direction: uint = 0; // direction <br/> private var speed: Int = 10; </P> <p> private var ISUP: Boolean; <br/> private var isdown: Boolean; <br/> private var isleft: Boolean; <br/> private var isright: Boolean; </P> <p> private var bitmapdata: bitmapdata = new player (0, 0); <br/> private var contain: sprite = new sprite (); </P> <p> private var VX: Number; <br/> private var Vy: Number; <br/> public function main () <BR/>{< br/> Init (); <br/>}< br/> // initialization <br/> private function Init (): void <br/> {<br/> addchild (contain); <br/> bitmap = new bitmapsplice (); // cut bitmap <br/> array = bitmap. splice (bitmapdata,); // divides the bitmap into multiple blocks. <br/> addeventlistener (event. enter_frame, gameloop); <br/> keyboardlistener (); </P> <p >}< br/> // game loop <br/> private function gameloop (event: event): void <br/>{< br/> Control (); <br/> paint (); // draw and fill </P> <p >}< br/> // keyboard listener <Br/> private function keyboardlistener (): void <br/> {<br/> stage. addeventlistener (keyboardevent. key_down, keydownhandler); <br/> stage. addeventlistener (keyboardevent. key_up, keyuphandler); <br/>}< br/> private function keydownhandler (Event: keyboardevent): void <br/>{< br/> switch (event. keycode) <br/>{</P> <p> case keyboard. up: <br/> ISUP = true; <br/> break; </P> <p> case keyboard. down: <br/> isdown = true; <br/> Break; </P> <p> case keyboard. left: <br/> isleft = true; <br/> break; </P> <p> case keyboard. right: <br/> isright = true; <br/> break; </P> <p >}< br/> private function keyuphandler (event: keyboardevent): void <br/>{< br/> switch (event. keycode) <br/>{</P> <p> case keyboard. up: <br/> ISUP = false; <br/> break; </P> <p> case keyboard. down: <br/> isdown = false; <br/> break; </P> <p> case keyboard. left: <br/> isleft = false; <Br/> break; </P> <p> case keyboard. right: <br/> isright = false; <br/> break; </P> <p> break; <br/>}< br/> // fill drawing <br/> private function paint (): void <br/>{< br/> contain. graphics. clear (); <br/> contain. graphics. beginbitmapfill (array [direction] [STEP]); <br/> contain. graphics. drawrect (0, 0, array [direction] [STEP]. width, array [direction] [STEP]. height); <br/> contain. graphics. endfill (); <br/>}< br/> // move <br/> PR Ivate function move (directtion: uint, DX: int, DY: INT): void <br/>{< br/> direction = directtion; <br/> step ++; <br/> vx = DX * speed; <br/> Vy = Dy * speed; <br/> If (Step> 3) <br/>{< br/> step = 0; <br/>}< br/> contain. X + = VX; // generated shift <br/> contain. Y + = Vy; // generated shift <br/>}< br/> private function control (): void <br/>{< br/> If (ISUP &&! Isdown &&! Isleft &&! Isright) <br/>{< br/> move (3,0,-1); <br/>}< br/> If (isdown &&! ISUP &&! Isleft &&! Isright) <br/>{< br/> move (0, 0, 1); <br/>}< br/> If (isleft &&! ISUP &&! Isdown &&! Isright) <br/>{< br/> move (1,-1, 0); <br/>}< br/> If (isright &&! ISUP &&! Isdown &&! Isleft) <br/>{< br/> move (2, 1, 0); <br/>}< br/> If (! ISUP &&! Isdown &&! Isleft &&! Isright) <br/>{< br/> step = 0; <br/>}< br/>}

 

The code is not very long. Here it is the same as the introduction of the last bitmap cut (it can be viewed in the previous article. It is not added here ). However, this time we changed a way to display bitmaps.

This section describes how to display bitmap. There are several methods. The last one is bitmap display, which adds the display list to addchild for image display. This time, the bitmap is filled through the drawing API. Later, we will continue to explore how to render a bitmap using the new paintors of Flash 10. It is not discussed here.

// Fill the drawing <br/> private function paint (): void <br/> {<br/> contain. graphics. clear (); <br/> contain. graphics. beginbitmapfill (array [direction] [STEP]); <br/> contain. graphics. drawrect (0, 0, array [direction] [STEP]. width, array [direction] [STEP]. height); <br/> contain. graphics. endfill (); <br/>}

This function is mainly used for drawing. In this way, some bitmaps are displayed.

Finally, paste a background to show the effect. You can control the movement of a person.

(You can cheat yourself by setting a background)

This small program does not involve a lot of profound knowledge. Here I just hope to bring a little joy to learn flash, and this joy is what I want to share.

 

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.