对于一个类(class)项目,我正在研究“Pissed Off Pigs”(我想你明白了, pig 会报复,别担心,我不会发布它!)我想创造用位图填充的圆圈(圆圈是因为碰撞数学更容易)。这是它们现在的样子。
当然,白色是问题所在。下面是我的“ pig ”类。有任何想法吗?我试过 gif、png、8 位 png 等,似乎没有什么区别。图片本身是 20px 的正方形,圆的半径是 10px(所以直径是 20px)。这是我正在使用的图像:
而且我知道我可能应该在类之外加载图像,并在我创建新 pig 时只传递 BitmapData,这样我就不必在每次创建新 pig 时都加载图像,但现在它可以工作了! (排序)。
矩阵平移有问题吗?
哦,我已经检查了 bmpImage.transparent 并且它返回 true(即使我没有在构造函数中指定 true)。
` 包裹 { 导入 flash.display.; 导入 flash.net。; 导入 flash.events.; 导入 flash.geom。; 导入 flash.display.BitmapDataChannel;
public class pig extends Sprite {
public var radius:uint;
public var velocity:Vec2;
public var tt:Sprite;
public var pigLoader:Loader;
public var bmpImage:BitmapData;
public var framesAlive:uint;
public function pig(xx:uint, yy:uint, radius:uint, vx:Number, vy:Number, sprite:String){
this.x = xx;
this.y = yy;
this.radius = radius;
this.velocity = new Vec2(vx, vy);
pigLoader = new Loader();
pigLoader.load(new URLRequest(sprite));
pigLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, picLoaded);
}
private function picLoaded(event:Event):void {
bmpImage = new BitmapData(pigLoader.width, pigLoader.height, true);
bmpImage.draw(pigLoader);
tt = new Sprite();
var matrix:Matrix;
matrix = new Matrix();
matrix.translate( -10, -10);
tt.graphics.beginBitmapFill(bmpImage, matrix, false, false);
tt.graphics.drawCircle(0, 0, this.radius)
tt.graphics.endFill();
this.addChild(tt);
}
}
`
最佳答案
你把问题复杂化了。当您想用平铺图案填充大面积区域时,通常使用位图填充,并且图案来自位图(想想墙纸),您不需要在这里使用它。您应该做的是使用 Loader.content 位图。您不需要 draw() 或矩阵,只需使用 .x 和 .y 将您的 pig 位图移动到以您的 Sprite 的 (0,0) 点为中心(如果这是您用于碰撞检测的圆的中心)。
没有理由让你的 pig Sprite 有一个可视圆圈来使用基于圆圈的碰撞检测。视觉表示对您的碰撞检测根本不重要。基于圆的碰撞检测通常只是毕达哥拉斯距离公式的应用,即如果中心之间的距离小于(或等于)被测圆的半径之和,则存在重叠(碰撞)。
如果你用这个替换最后一个函数会怎样:
private function picLoaded(event:Event):void
{
var pigPic:Bitmap = event.target.loader.content;
pigPic.x -= 10;
pigPic.y -= 10;
this.addChild(pigPic);
}
关于flash - 如何获得透明度以在 Actionscript 中处理位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5766691/