【问题标题】:AS3: Getting a color value from a bitmapAS3:从位图中获取颜色值
【发布时间】:2012-06-18 12:32:45
【问题描述】:

当我单击舞台上的图像时,我正在尝试检索颜色值。我打算用它来为我正在开发的游戏创建高度图,使角色在崎岖地形(高度图的具有特定颜色值的部分)上移动得更慢,但我不断收到以下错误:

TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::MovieClip@2fc84f99 to flash.display.Bitmap.
at testGetColor2_fla::MainTimeline/frame1()

到目前为止,这是我的代码:

import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.events.MouseEvent;
import flash.display.Sprite;

var container:Sprite = new Sprite();

var myHeightMap:Bitmap = Bitmap(heightMap);

this.addChild(container);
container.addChild(myHeightMap);

container.addEventListener(MouseEvent.CLICK, onClick);

function onClick(e:MouseEvent):void
{
    var obj:Sprite = e.currentTarget as Sprite;

    var myHeightMap:Bitmap = Bitmap(obj.getChildAt(0));

    var pixelValue:uint = myHeightMap.bitmapData.getPixel(mouseX,mouseY);

    trace(pixelValue.toString(16));
}

我做错了什么?

【问题讨论】:

    标签: actionscript-3 bitmap


    【解决方案1】:

    问题在于,您不能像在创建 myHeightMap 时尝试做的那样,通过强制转换将 MovieClip 转换为 Bitmap。要进行该转换,请使用以下命令:

    var bitmapData:BitmapData = new BitmapData(heightMap.width, heightMap.height);
    bitmapData.draw(heightMap);
    var myHeightMap:Bitmap = new Bitmap(bitmapData);
    

    当然,可能还有其他方法可以做到这一点。如果您能在调用此代码之前找到将heightMap 作为Bitmap 的方法,那会更有效。

    【讨论】:

    • 你先生,摇滚!!!!它的工作!非常感谢 :) 希望我可以投票,但我没有足够的声誉。
    • 很高兴为您提供帮助!如果您的问题得到解决,那么您应该将最有帮助的答案标记为“已接受”。 :)
    • 啊,明白了。谢谢你:)
    【解决方案2】:

    你的做法不太对:)

    这是您将影片剪辑转换为位图的方法:

    function toBitmap(value:DisplayObject):Bitmap
    {
        var bmpData:BitmapData = new BitmapData(value.width, value.height, true, undefined);
        bmpData.draw(value, null, null, null, null, true);
    
        return new Bitmap(bmpData, "auto", true);
    }
    

    然后你就可以得到像素了。

    记住,RegistrationPoint 必须是 TOP_LEFT。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多