Research on flash bitmap Technology (3): Moving the buffer area

Source: Internet
Author: User
Tags map class

Map class, we have already written a preliminary article, and the functions still need to be expanded and improved. Now we are trying to move our buffer area.

Upload a: 640x480 bitmap: we use this image as a map.

We use this material as our map and try to move some of our maps. If we move the whole map, the efficiency will naturally be low. We just move the rectangle in this way, dynamic bitmap cutting method.

Keyboard Control Method: (this method is provided by a person on the internet, borrow it)

This class is written in the package com. Hero. imagemanager Folder:

Package com. Hero. imagemanager
{
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];
}
}

}


Key Value: Specifies the key values. Top, bottom, left, right, and some key values.

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;

}
}


Document Type:

Import the above two classes:

Package
{
Import flash. display .*;
Import flash. Events .*;
Import flash. Geom. rectangle;
Import flash. Geom. Point;
Import flash. UI. keyboard;
Import com. Hero. imagemanager .*;
Public class main extends Sprite
{
Private var map: map;
Private var mapdata: bitmapdata = new MAP2 (640,480 );
Public Function main ()
{
Map = new map (mapdata, 0, 0 );
Map. x = 0;
Map. Y = 0;
Addchildat (MAP, 0 );
Key. INIT (stage );
Addeventlistener (event. enter_frame, drawsence );
}
Private function drawsence (E: Event): void
{
Map. createmaps (300,300 );
Keydownhandle ();
}
Private function keydownhandle (): void
{
If (key. isdown (keyid. vk_up ))
{
Map. rect. Y-= 5;
}
Else if (key. isdown (keyid. vk_down ))
{
Map. rect. Y + = 5;
}
Else if (key. isdown (keyid. vk_left ))
{
Map. rect. X-= 5;
}
Else if (key. isdown (keyid. vk_right ))
{
Map. rect. x + = 5;
}
}
}
}


Create a bitmap, private var mapdata: bitmapdata = new MAP2 (640,480); External import to our library.

Instantiate a map object and use this object mapdata as the expected object parameter.

Map = new map (mapdata, 0, 0 );
Map. x = 0;
Map. Y = 0;
Addchildat (MAP, 0 );

Specifies the location of the map.

Keyboard initialization: Key. INIT (stage );

Scenario:

Private function drawsence (E: Event): void
{
Map. createmaps (300,300 );
Keydownhandle ();
}

By controlling, move the rectangle up and down the left and right keyboards. While moving, the displacement of the copied pixels in our map class also changes accordingly.

Private function keydownhandle (): void
{
If (key. isdown (keyid. vk_up ))
{
Map. rect. Y-= 5;
}
Else if (key. isdown (keyid. vk_down ))
{
Map. rect. Y + = 5;
}
Else if (key. isdown (keyid. vk_left ))
{
Map. rect. X-= 5;
}
Else if (key. isdown (keyid. vk_right ))
{
Map. rect. x + = 5;
}
}


Overall:

 

Map class:

Package
{

Import flash. display. Sprite;
Import flash. display .*;
Import flash. Events .*;
Import flash. Geom .*;

Public class map extends Sprite
{
Private var mapwidth: Number; // map width
Private var mapheight: Number; // map height
Private var MapX: Number; // X coordinate of the map
Private var mapy: Number; // y coordinate of the map

Private var maparray: array;
Private var mapdata: bitmapdata;
Public var sprite: SPRITE = new sprite (); // used as a container
Public var rect: rectangle; // rectangular shape of the map
Public var bitmap: bitmap;
Private var Buffer: bitmapdata; // Buffer

Public Function Map (mapdata: bitmapdata, MapX: Number, mapy: Number)
{
This. mapdata = mapdata;
// This. MapX = MapX;
// This. mapy = mapy;
This. mapwidth = mapdata. width;
This. mapheight = mapdata. height;
Bitmap = new Bitmap (mapdata );
Rect = bitmap. getrect (Bitmap); // obtain the rectangle of the map
}
Public Function get Mapinfo (): bitmapdata
{
Return mapdata;
}
Public Function createmap (bufferwidth: Number, bufferheight: Number): void
{
Buffer = new bitmapdata (bufferwidth, bufferheight, false, 0 xffffffff); // set the buffer
Buffer. fillrect (New rectangle (0, 0, bufferwidth, bufferheight), 0x0000ff00); // fill the buffer
Trace (rect. y );

If (rect. x <0)
{
Rect. x = 0;
}
Trace (mapwidth );
If (rect. x + buffer. width> mapwidth)
{
Rect. x = mapWidth-buffer.width;
}
If (rect. Y <= 0)
{
Rect. Y = 0;
}
If (rect. Y + buffer. Height> mapheight)
{
Rect. Y = mapHeight-buffer.height;
}
Buffer. copypixels (mapdata, new rectangle (rect. x, rect. y, bufferwidth, bufferheight), new point (); // The coordinate change of the rectangle will crop different bitmap data.
Fillmap (buffer); // fill in bitmap data

}
Public Function fillmap (data: bitmapdata): void
{
Sprite. Graphics. Clear ();
Sprite. Graphics. beginbitmapfill (data, null, false, false );
Sprite. Graphics. drawrect (0, 0, Data. Width, Data. Height );
Sprite. Graphics. endfill ();
Addchild (sprite );

}
}
}




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.