将对象添加到 bitMapData

发布于 2024-08-16 02:19:04 字数 307 浏览 3 评论 0原文

截至目前。我有3个对象。一张位图充当我的画布。和2个位图数据。一个是我的缓冲区,另一个是我的图块。我正在为游戏创建平铺效果。我想获取我的tile:BitMapData,并将其转换为自定义对象。原因是我希望每个图块都是交互式的。这样我就可以点击每一个。是否可以将代表图块的 bitMapData 转换为具有属性和方法的自定义对象。有点像电影剪辑。并将其绘制到我的缓冲区中?我可以创建一个扩展 bitMapData 的新类吗?或者我是否必须摆脱缓冲区并将图块对象直接绘制到位图中?

换句话说,将 Sprite 或图块放入 BitMapData 对象甚至 Bitmap 中的最佳方式是什么。

As of right now. I have 3 objects. One BitMap that acts as my canvas. And 2 bitmapDatas. One is my buffer and another is my tiles. I am creating a tiling effect for a game. I would like to take my tile:BitMapData, and turn it into a custom object. reason being is I want each tile to be interactive. So I can click on each one. Is it possible to turn my bitMapData that represents a tile, into a custom object that has properties and methods. Sort of like a movie clip. and draw it into my buffer ?? Could I create a new class that extends bitMapData ?? Or would I have to get rid of the buffer and draw the tile objects directly into the BitMap ??

In other words, what is the best way to put Sprite or a tile into a BitMapData object or even a Bitmap.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海未深 2024-08-23 02:19:04

首先,BitmapData 和 Bitmap 不可互换。它们是两种截然不同的东西。 BitmapData 类包含位图像素数据,并允许您操作该像素数据,例如绘制它、​​更改特定像素的颜色等。无法直接显示 BitmapData,即将其添加到显示列表中。

另一方面,Bitmap 类是一个 DisplayObject,如 MovieClips 和 Sprites,您可以将其添加到显示列表中。它的唯一目的是在显示列表中渲染 BitmapData。事实上,它甚至不是交互式的,因此您无法直接检测 Bitmap 实例上的点击。

关于您的问题:如果您有一个包含图块精灵的位图数据,并且您想在另一个位图数据中绘制该图块,则可以使用 BitmapData.draw() 方法或 BitmapData.copyPixels() 方法。后者是可用于任何 BitmapData 的最快方法之一,因此我强烈推荐它。

根据您的特定应用程序,在位图中绘制所有内容可能根本没有好处。听起来好像您希望能够检测所有图块上的单击事件,这让我认为您可能会受益于将它们作为单独的 DisplayObjects(例如 Sprites)。

如果需要,您可以创建一个扩展 Sprite 的 Tile 类,并使用位图填充绘制 BitmapData。这样,您就可以拥有所需的任何属性,并且还可以检测图块实例上的鼠标事件。

package
{
  /* ... imports ... */

  public class Tile extends Sprite 
  {
    private var _solid : Boolean;

    public function Tile(bmp : BitmapData, solid : Boolean)
    {
      this.graphics.beginBitmapFill(bmp, null, false, true);
      this.graphics.drawRect(0, 0, bmp.width, bmp.height);

      _solid = solid;
    }

    /**
     * Sample custom property. Could be used to define whether a tile
     * is solid, e.g. the player cannot pass it.
    */
    public function get isSolid() : Boolean
    {
      return _solid;
    }
  }
}

可以简单地为游戏中的每个图块实例化此类,并传入应在图块中绘制的位图数据。您还可以侦听此类图块实例上的事件。

var tile : Tile;

tile = new Tile(myBitmapData, false);
tile.x = 200;
tile.y = 200;
tile.addEventListener(MouseEvent.CLICK, handleTileClick);

addChild(tile);

这样,您根本不必使用 Bitmap 类来渲染图块。它们可以直接添加到显示列表中。

First of all, BitmapData and Bitmap are not interchangeable. They are two very different things. The BitmapData class contains bitmap pixel data, and allows you to manipulate that pixel data, e.g. draw to it, change the color of particular pixels, et c. There is no way of displaying a BitmapData directly, i.e. by adding it to the display list.

The Bitmap class, on the other hand, is a DisplayObject, like MovieClips and Sprites, which you can add to the display list. It's single purpose is to render a BitmapData in the display list. In fact, it is not even interactive, so you cannot detect clicks directly on a Bitmap instance, for example.

On to your question: If you have a bitmap data that contains a tile sprite, and you want to draw that tile in another bitmapdata, you can use the BitmapData.draw() method, or the BitmapData.copyPixels() method. The latter is one of the fastest methods you can use on any BitmapData, so I would highly recommend it.

Depending on your particular application, it might not be beneficial to draw everything in a bitmap at all. It sounds as if you want to be able to detect click events on all the tiles, which makes me think that you would probably benefit from having them be separate DisplayObjects, e.g. Sprites.

If you want to, you can create a Tile class that extends Sprite, and draws a BitmapData using a bitmap fill. That way, you can have any properties you want, and also detect mouse events on the tile instances.

package
{
  /* ... imports ... */

  public class Tile extends Sprite 
  {
    private var _solid : Boolean;

    public function Tile(bmp : BitmapData, solid : Boolean)
    {
      this.graphics.beginBitmapFill(bmp, null, false, true);
      this.graphics.drawRect(0, 0, bmp.width, bmp.height);

      _solid = solid;
    }

    /**
     * Sample custom property. Could be used to define whether a tile
     * is solid, e.g. the player cannot pass it.
    */
    public function get isSolid() : Boolean
    {
      return _solid;
    }
  }
}

This class could simply be instantiated for every tile in your game, passing in the bitmap data that should be drawn in the tile. You could also listen for events on such a tile instance.

var tile : Tile;

tile = new Tile(myBitmapData, false);
tile.x = 200;
tile.y = 200;
tile.addEventListener(MouseEvent.CLICK, handleTileClick);

addChild(tile);

This way, you don't have to use the Bitmap class at all to render the tiles. They can be added directly to the display list.

苏大泽ㄣ 2024-08-23 02:19:04

我想出了一个更好的解决方案,当您想要选择图块的某些部分而不影响精灵本身的位置时,

theMatrix.translate(30,0);
this.graphics.beginBitmapFill(tileImage,theMatrix);
//this.graphics.drawRect(0, 0,tWidth ,tHeight );
this.graphics.endFill();

您的权利,将绘制矩形保留在 0, 0。使用 Matrix.Translate,允许您移动图块的哪个部分的位置您想要的图块,而不影响精灵位置本身。

I figure a better solution for when you want to select certain parts of the tile without effecting the position of the sprite itself

theMatrix.translate(30,0);
this.graphics.beginBitmapFill(tileImage,theMatrix);
//this.graphics.drawRect(0, 0,tWidth ,tHeight );
this.graphics.endFill();

Your right, leave drawRect at 0, 0. using Matrix.Translate, allows you to move the positioning of what part of the tile you want, without affecting the sprite position itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文