我試圖在單擊舞臺上的圖像時檢索顏色值。我打算使用它爲我正在製作的遊戲創建一個高度圖,使得角色在粗糙地形(具有特定顏色值的高度圖部分)上移動得更慢,但是我不斷收到以下錯誤:AS3:從位圖獲取顏色值
TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::[email protected] 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));
}
我在做什麼錯?
你先生,搖滾!!!!它工作!非常感謝:) 希望我能夠upvote,但我沒有足夠的聲譽。 – vagabond80
很高興幫助!如果你的問題得到解決,那麼你應該標記哪個答案最有幫助爲'接受'。 :) – CheeseWarlock
啊gotcha。感謝那 :) – vagabond80