我对blitting很新,我还在努力理解这个过程。在我的View
对象中,我在构造函数中有这个;
this.bcg = new BitmapData(800,600,false,0x000000);
this.canvas = new Bitmap(bcg);
然后我将它添加到舞台上;
addChild(canvas);
然后在我的主游戏循环中,我遍历一个嵌套数组来blit rect
类型BitmapData
对象的副本;
if (array[i][j] !=0) {
canvas.bitmapData.copyPixels(rect, rect.rect, new Point(i, j));
}
我想也许我可以通过在我的循环之前将背景blit到canvas
来做到这一点;
canvas.bitmapData.copyPixels(bcg, bcg.rect, new Point(0, 0));
但这不起作用。我已经尝试过寻找答案,但是很难找到有关AS3中blitting的任何深入信息。
我要做的是清除所有像素的位图,然后再用我的背景bcg
重新开始。我在循环之前尝试过这样做;
this.bcg = new BitmapData(800,600,false,0x000000);
this.canvas = new Bitmap(bcg);
但我最终得到了一个完全黑屏,而我的rect
都没有被canvas
点缀。
答案 0 :(得分:1)
这是一个简单的blitting示例:
private var m_bg:BitmapData = new BitmapData( 400, 300, false, 0xffff0000 ); // simple solid red colour
private var m_canvas:BitmapData = new BitmapData( 400, 300, true, 0x00000000 ); // the canvas that we're drawing into
private var m_obj:BitmapData = new BitmapData( 20, 20, false, 0xff00ff00 ); // simple green square
private var m_objDrawPos:Point = new Point; // where we'll draw our obj
private var m_objDir:Point = new Point; // the direction that our obj is moving in
private var m_objSpeed:Number = 20.0; // the speed of our obj
public function Main()
{
// create the bitmap for our canvas and add it to the stage
var b:Bitmap = new Bitmap( this.m_canvas );
this.addChild( b );
// set our initial direction
this.m_objDir.setTo( 1.0, 1.0 ); // diagonal right
// add our enter frame
this.addEventListener( Event.ENTER_FRAME, this._onEnterFrame );
}
private function _onEnterFrame( e:Event ):void
{
// move our object by changing it's draw position
this.m_objDrawPos.x += this.m_objDir.x * this.m_objSpeed;
this.m_objDrawPos.y += this.m_objDir.y * this.m_objSpeed;
// simple bounds change, to reflect the object
if ( this.m_objDrawPos.x < 0 || this.m_objDrawPos.x + this.m_obj.width > this.m_canvas.width )
this.m_objDir.x *= -1;
if ( this.m_objDrawPos.y < 0 || this.m_objDrawPos.y + this.m_obj.height > this.m_canvas.height )
this.m_objDir.y *= -1;
// copy our this.m_bg - this will erase all previous changes
// NOTE: depending on what your bg is, you can all use fillRect() if it's a solid colour
// NOTE: passing new Point() as the destPoint, will mean that our bg will always be drawn
// in the top-left of the canvas. As this never changes - you can keep this as a variable
this.m_canvas.copyPixels( this.m_bg, this.m_bg.rect, new Point );
// copy our object to its new position
this.m_canvas.copyPixels( this.m_obj, this.m_obj.rect, this.m_objDrawPos );
}
关于blitting需要了解的是,它是destPoint
传入的位置,它确定了BitmapData
的左上角位置 - 将其视为{正常x
- y
的{1}}和DisplayObject
意味着它在左上角绘制,而(0,0)
意味着它在右下角绘制。
为了让您的生活更轻松,请创建一个简单的类来为您存储所有这些数据。类似的东西:
(canvas.width - obj.width, canvas.height - obj.height)
在blitting时要注意一件事,而这个类就是这样,它处理public class BlitObj
{
/************************************************************/
private var m_bmd:BitmapData = null; // our BitmapData graphics
private var m_offset:Point = null; // the offset (how we manage the reference point)
private var m_drawPos:Point = null; // our draw position for when we're blitting the object
private var m_position:Point = null; // the position of our object, in relation to the reference point
/************************************************************/
/**
* The position of the BlitObj, in relation to the reference point
*/
public function get position():Point { return this.m_position; }
/**
* The BitmapData graphics that we're going to blit
*/
public function get blitGraphics():BitmapData { return this.m_bmd; }
/**
* The position to blit this BlitObj at (top-left corner)
*/
public function get blitDestPos():Point { return this.m_drawPos; }
/************************************************************/
/**
* Creates a new BlitObj
* @param bmd The graphics for this BlitObj
* @param offset The offset to our registration point
*/
public function BlitObj( bmd:BitmapData, offset:Point = null )
{
this.m_bmd = bmd;
this.m_offset = ( offset == null ) ? new Point : offset;
this.m_drawPos = new Point( -this.m_offset.x, -this.m_offset.y );
this.m_position = new Point;
}
/**
* Sets the position of the BlitObj (in relation to its reference point)
* @param x The x position of the object
* @param y The y position of the object
*/
public function setPos( x:Number, y:Number ):void
{
this.m_position.setTo( x, y );
this.m_drawPos.setTo( x - this.m_offset.x, y - this.m_offset.y ) ;
}
}
。当您向offset
函数提供destPoint
时,它是在绘制之前放置copyPixels()
左上角的位置。但是,通常情况下,您希望对象具有左上角的不同参考点(例如,圆圈将在其中间具有参考点)。这可以让你处理它。
BitmapData
答案 1 :(得分:0)
我知道我现在做错了什么。
这有效;
this.bcg = new BitmapData(800,600,false,0x000000);
canvas.bitmapData.copyPixels(bcg, bcg.rect, new Point(0, 0));
我必须重新声明bcg
,然后将其blit到canvas
。因为我的rect
个对象正在bcg
上,所以对吗?我正在创建一个使用的位图;
this.canvas = new Bitmap(bcg);
因此,canvas会引用bcg
?