【问题标题】:AS3 Sprite SheetsAS3 精灵表
【发布时间】:2010-11-14 18:43:39
【问题描述】:

我有一个图像 mySprite.png。该图像是 32x32 像素精灵的 5x5 网格。此图像已加载到项目的库中。

假设我在一个类中有一个 render() 函数,那么这个类如何从这个 sprite sheet 资源中将自己绘制为一个单独的 sprite?

【问题讨论】:

    标签: actionscript-3 sprite


    【解决方案1】:

    简短的回答是,您将希望使用BitmapData.copyPixels() 仅将一小部分从源精灵表复制到实际在屏幕上的显示精灵。

    类似:

    private function DrawSpriteIndex( displayBitmap:Bitmap, spriteSheet:Bitmap, spriteIndex:int ):void {
      var spriteW:int = 32;
      var spriteH:int = 32;
      var sheetW:int = 5;
    
      displayBitmap.bitmapData.copyPixels(spriteSheet.bitmapData, 
                                           new Rectangle( (spriteIndex % sheetW) * spriteW, Math.floor(spriteIndex / sheetW) * spriteH, 32, 32),
                                           new Point(0,0)
                                          );
    }
    

    您可能会发现这些链接很有帮助——当我学习这个时它们帮助了我:

    【讨论】:

      【解决方案2】:

      另一种可能的方法是在工作表上放置一个 32x32 的蒙版,然后移动工作表。

      它会像(伪代码)一样工作:

      var spriteMask:Sprite = new Sprite();
      spriteMask.graphics.drawRect(0,0,32,32);
      spriteSheetContainer.mask = spriteMask;
      
      function render():void {    // this function is on the container of the sprite sheet (spriteSheetContainer in this example)
          // run offsetX & Y iteration logic.  I would assume something that uses a frame counter, modulus, and the sprite layout grid dimensions
          _spriteSheet.x = offsetX;    // move the sprite around under the mask
          _spriteSheet.y = offsetY;
      }
      

      将蒙版放在精灵表的容器上而不是精灵表本身上至关重要,这样您就可以独立于蒙版移动精灵表。

      【讨论】:

      • 这样做会对性能产生重大影响,因为 Flash 播放器仍在绘制整个图像,即使您看不到它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-04
      • 1970-01-01
      • 1970-01-01
      • 2012-08-08
      • 2023-03-21
      • 2011-10-15
      相关资源
      最近更新 更多