Flash Game Design Notes: cutting bitmap Technology

Source: Internet
Author: User
Tags addchild

Today, I will discuss the technology of cutting bitmap. This method is actually very simple. I have defined it as two common cutting technologies: One-Dimensional Cutting and two-dimensional cutting. Save different cut images in the array.

: File

 

One-Dimensional Cutting: for example, contain [image encoding]

Two-Dimensional Cutting: for example, contain [direction] [step number]

This technology is applied when we create an RPG image. From the picture downloaded from the Internet, we can see that this image has eight directions and belongs to the eight-square diagram.

From this image, we can see that it is arranged like a two-dimensional array. We create an array 2D array to store these images and convert them into a 2D array.

In as3, one of the methods is to copy the image and access it by proportion, width, and size. Finally, we can see that the array is changed:

For example, contain [direction] [number of steps]

Assume that the first image, contain [0] [0] indicates the first row, and the first column of the image. Similarly, contain [1] [0] is the second row, the image in the first column. In this way, the first number of arrays is designed as the direction, and the second number is designed as the step number.

The following describes how to cut two-dimensional data into a class:

Package
{
/* Cut bitmap class, returns a two-dimensional array */
Import flash. display. Sprite;
Import flash. display. Bitmap;
Import flash. display. bitmapdata;
Import flash. Geom .*;
Public class bitmapsplice extends Sprite
{
Public var contain: SPRITE = new sprite (); // container
// Public var direction: uint; // direction
Private var titlewidth: uint; // The width of the image element.
Private var titleheight: uint; // The height of the image element.
Private var bitmapdata: bitmapdata; // bitmap source Image
Private var step: array = new array (); // The number of access steps Array
Public Function bitmapsplice (bitmapdata: bitmapdata, titlewidth: uint, titleheight: uint)
{
This. bitmapdata = bitmapdata;
This. titlewidth = titlewidth;
This. titleheight = titleheight;
}
// Cut
Public Function splice (): Array
{
VaR iwidth: Int = This. bitmapdata. width/This. titlewidth;
VaR iheight: Int = This. bitmapdata. Height/This. titleheight;
For (var I: uint = 0; I <titleheight; I ++)
{
VaR array: array = new array ();
For (var j: uint = 0; j <titlewidth; j ++)
{
VaR tempbmp: bitmapdata = new bitmapdata (iwidth, iheight, true, 0x00000000 );
Tempbmp. copypixels (bitmapdata, new rectangle (J * iwidth, I * iheight, iwidth, iheight), new point (0, 0 ));
Array. Push (tempbmp );
}
This. Step. Push (array );
}
Bitmapdata. Dispose ();

Return this. step;

}
}
}


Next, we will conduct a test to allow the cropped image to be displaced by the keyboard.

Write a control class to control the movement of characters. The method we use is addchild.
At the same time, the image in the container is also deleted. In this way, the memory usage is reduced. When the number of steps exceeds the array width, we change it to the original 1, so that the bitmap is moved in the same region,
It moves in a range just like a pointer. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] The number of moving steps increases, and the image changes accordingly. This is how it works.

Imgae [direction] [number of steps]

Package
{
Import flash. display. stage;
Import flash. Events .*;
Import flash. display. Sprite;
Import flash. display. Bitmap;
Import flash. display. bitmapdata;
Import com. Hero. imagemanager .*;
Public class control extends eventdispatcher
{
Private var sence: SPRITE;
Private var images: array;
Private var direction: uint = 1; // direction
Private var step: uint = 1; // number of steps
Private var VX: Int = 0;
Private var Vy: Int = 0;
Private var speed: Int = 10;
Private var n_step: uint;
Private var player: SPRITE = new sprite ();
Public Function Control (stage: stage, sence: Sprite, images: array, n_step: uint)
{
Key. INIT (stage); // keyboard Initialization
This. sence = sence; // scenario
This. Images = images;
This. n_step = n_step;
This. sence. addchild (player );
Player. x = 250;
Player. Y = 150;
Stage. addeventlistener (event. enter_frame, run); // you can use stage to listen.
}
Private function run (Event: Event): void
{
While (player. numchildren> 0)
{
Player. removechildat (0 );
}
Player. addchild (New Bitmap (images [direction] [STEP]);
Keyboardlistener ();
}
Private function keyboardlistener (): void
{
If (key. isdown (keyid. vk_up) & Key. isdown (keyid. vk_left ))
{
Goleftandtop ();
}
Else if (key. isdown (keyid. vk_up) & Key. isdown (keyid. vk_right ))
{
Gorightandtop ();
Trace ("D ");
}
Else if (key. isdown (keyid. vk_down) & Key. isdown (keyid. vk_left ))
{
Goleftanddown ();
}
Else if (key. isdown (keyid. vk_down) & Key. isdown (keyid. vk_right ))
{
Gorightanddown ();
}
Else if (key. isdown (keyid. vk_up ))
{
Goup ();
}
Else if (key. isdown (keyid. vk_down ))
{
Godown ();
}
Else if (key. isdown (keyid. vk_left ))
{
Goleft ();
}
Else if (key. isdown (keyid. vk_right ))
{
Goright ();
}
Else if (! Key. isdown (keyid. vk_left )&&! Key. isdown (keyid. vk_up )&&! Key. isdown (keyid. vk_right )&&! Key. isdown (keyid. vk_down ))
{
Stand ();
}

}

Private function goleftandtop (): void
{
Go (-1,-1, 3 );
}
Private function gorightandtop (): void
{
Go (1,-1, 6 );

}
Private function goleftanddown (): void
{
Go (-1, 1, 4 );

}
Private function gorightanddown (): void
{
Go (1, 1, 7 );

}
Private function goup (): void
{
Go (0,-1, 0 );

}
Public Function godown (): void
{
Go (0, 1 );
}
Private function goleft (): void
{
Go (-1, 0, 2 );
}
Private function goright (): void
{
Go (1, 0, 5 );

}
Private function stand (): void
{
VX = 0;
Vy = 0;
Step = 0;
}
Private function go (_ xV: int, _ YV: int, direction: uint): void
{
This. Direction = direction;
VX = _ xV * speed;
Vy = _ YV * speed;
Step ++;
If (Step> n_step-1)
{
Step = 1;
}
Player. x + = VX;
Player. Y + = Vy;
SENCE. X-= VX;
SENCE. Y-= Vy;
}

}
}


Main class run test.

Package
{
Import flash. display. Sprite;
Import flash. display. displayobject;
Import flash. display. bitmapdata;
Import flash. display. Bitmap;
Import flash. Events .*;
Import com. Hero. imagemanager .*;
Public class test extends Sprite
{
Private var bit: bitmapsplice;
Private var images: array;
Private var sence: SPRITE = new sprite (); // scenario class, designed as a container
Private var mycontrol: control;
Public Function Test ()
{Sence. addchildat (New Bitmap (New sence1 (0, 0), 0 );
Bit = new bitmapsplice (new player (), 9, 8); // The Player class is the bitmap of the Library Link.
Images = bit. splice (); // split two-dimensional array
Mycontrol = new control (stage, sence, images, 9 );
Addchild (Sence); // bitmap container Initialization
Addeventlistener (event. enter_frame, run );
}
Private function run (Event: Event): void
{

}
}
}


Key used for keyboard monitoring

Package com. Hero. imagemanager
{
/* Keyboard button judgment class.
Initialize the object to be listened to before using it.
Key. INIT (stage );
Key. isdown (40); returns true or false
*/
Import flash. Events. event;
Import flash. Events. keyboardevent;
Import flash. display. displayobject;
Public class key {
Private Static Var keyobj: Key = NULL;
Private Static Var keys: object;
Public static function Init (_ stage: displayobject): void {
If (keyobj = NULL ){
Keys = {};
_ Stage. addeventlistener (keyboardevent. key_down, key. keydownhandler );
_ Stage. addeventlistener (keyboardevent. key_up, key. keyuphandler );
}
}
Public static function isdown (keycode: uint): Boolean {
Return keys [keycode];

}
Private Static function keydownhandler (E: keyboardevent): void {
Keys [E. keycode] = true;
Trace (Keys [E. keycode]);
}
Private Static function keyuphandler (E: keyboardevent): void {
Delete keys [E. keycode];
}
}

}


Declaration of the following category as the keyboard Value

Package com. Hero. imagemanager
{
Public final class keyid
{
Public static const vk_left: uint = 37;
Public static const vk_up: uint = 38;
Public static const vk_right: uint = 39;
Public static const vk_down: uint = 40;

}
}


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.