Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 AS3-创建具有非整数宽度和高度的新位图数据_Actionscript 3 - Fatal编程技术网

Actionscript 3 AS3-创建具有非整数宽度和高度的新位图数据

Actionscript 3 AS3-创建具有非整数宽度和高度的新位图数据,actionscript-3,Actionscript 3,嘿,伙计们,希望有人能帮上忙。我正在使用以下功能将位图图像裁剪为平铺: function crop( _x:Number, _y:Number, _width:Number, _height:Number, callingScope:MovieClip, displayObject:DisplayObject = null, pixelSnapping:Boolean = false):Bitmap { var cropArea:Rectangle = new Rectangle( 0,

嘿,伙计们,希望有人能帮上忙。我正在使用以下功能将位图图像裁剪为平铺:

function crop( _x:Number, _y:Number, _width:Number, _height:Number, callingScope:MovieClip, displayObject:DisplayObject = null, pixelSnapping:Boolean = false):Bitmap
{
    var cropArea:Rectangle = new Rectangle( 0, 0, _width, _height );
    var croppedBitmap:Bitmap;

    if(pixelSnapping == true)
    {
        croppedBitmap = new Bitmap( new BitmapData( _width, _height ), PixelSnapping.ALWAYS, true );
    }
    else
    {
        croppedBitmap = new Bitmap( new BitmapData( _width, _height ), PixelSnapping.NEVER, true );
    }

    croppedBitmap.bitmapData.draw( (displayObject!=null) ? displayObject : callingScope.stage, new Matrix(1, 0, 0, 1, -_x, -_y) , null, null, cropArea, true );

    return croppedBitmap;
}
传入的宽度和高度是非整数值,例如18.75。创建新的BitmapData时,它总是将值向下舍入为整数,因为BitmapData的参数类型是这样的。根据我的需要,宽度和高度不可能是整数。有没有办法以我需要的准确宽度和高度创建另一幅图像的这些位图片段,或者只能使用高度和宽度的整数值创建新的bitmapData

谢谢你的洞察力


编辑:我知道你不能有一点点像素,但是。。。我试图实现的是将一个图像分割成瓷砖。我希望平铺的数量是可变的,比如说4行乘4列,或者6行乘8列。将图像分割为X个部分会导致宽度和高度在大多数情况下为非整数值,例如18.75。我们的目标是将一个图像分割成块,并使该图像在源图像上方显示、无缝组装,然后我将在其中操纵各个块以用于各种目的(益智游戏、新场景的平铺动画等)。我需要图像,当从所有瓷砖块组装时,是原始图像的精确副本,瓷砖之间没有线条,或者任何地方都没有白色边缘,我需要在构成瓷砖的bitmapData块的非整数宽度和高度下实现这一点。这有意义吗?O_O

只能使用维度的整数值创建位图数据

老实说,我在想一个尺寸为浮点数的BitmapData对象会是什么样子,但我的大脑开始痛苦地尖叫:没有意义

我的大脑有时有点夸张

--编辑-- 对您编辑的问题的回答:

2种选择:

1/只需复制原始完整位图数据,复制次数与您拥有的分幅相同,然后在位图对象上使用遮罩来显示分幅的适当部分

2/确保切片不会生成分数宽度或高度。例如,如果要将宽度为213的图像拆分为两行两列,则将左侧平铺的宽度设置为106,将右侧平铺的宽度设置为107


也许还有其他选择,但在我看来,这两种方法是最容易获得成功的方法。

如果您需要创建具有首选大小的瓷砖,您可以使用以下方法:

private function CreateTilesBySize( bigImage:Bitmap, preferredTileWidth:int, preferredTileHeight:int ):Tiles
{
  var result:Tiles = new Tiles();
  var y:int = 0;
  while ( y < bigImage.height ) {
    result.NewRow();
    const tileHeight:int = Math.min( preferredTileHeight, bigImage.height - y );
    var x:int = 0;
    while ( x < bigImage.width ) {
      const tileWidth:int = Math.min( preferredTileWidth, bigImage.width - x );
      const tile:Bitmap = Crop( x, y, tileWidth, tileHeight, bigImage );
      result.AddTile( tile );
      x += tileWidth;
    }
    y += tileHeight;
  }
  return result;
}

但是请记住,分片的大小会有所不同(行中的最后一个分片和列中的最后一个分片)

位图数据由像素组成。精确的像素数量是宽度*高度。像素是一个颜色点。BitmapData与可视元素无关。它是关于视觉元素如何存储在内存中的。不可能有一半的信息量。请解释您试图实现的目标,以便我们能够提供更好的解决方案。编辑我的答案以回答您编辑的问题。由于没有神奇的公式可以将(比如)5除以2而不带余数,因此您必须对图像进行填充、裁剪或缩放,以实现整数数学运算。当然,这不是一份精确的副本。
private function CreateTilesByNum( bigImage:Bitmap, cols:int, rows:int ):Tiles
{
  const preferredTileWidth:int = Math.ceil( bigImage.width / cols );
  const preferredTileHeight:int = Math.ceil( bigImage.height / rows );
  return CreateTilesBySize( bigImage, preferredTileWidth, preferredTileHeight );
}