addChild()/隐式强制转换 - Flash AS3

addChild()/Implicit Coercion - Flash AS3

我是 Flash 的新手。我知道它几乎已经过时了,但我正在从事的项目是用 Flash 编写的。

我当前的任务是从 .jpg 文件中的任何像素获取 RGB 数据。到目前为止,我已经完成了以下工作:

我已将图像保存为 .fla 文件,并将图像本身转换为自己的自定义 class,称为 "StageWheel",以 BitmapData 作为其基础 class。但是,当我这样做时:

  var sWheel:StageWheel = new StageWheel();
  addChild(sWheel);
  sWheel.addEventListener(MouseEvent.CLICK, getColorSample);

  var bitmapWheel:BitmapData = new BitmapData(sWheel.width, sWheel.height);

我收到一个错误:

"Implicit coercion of a value of type StageWheel to an unrelated type flash.display.DisplayObject"

一致
addChild(sWheel);

这个错误是什么意思?难道不能用addChild这样往舞台上加东西吗?

编辑

成功@LDMS,谢谢。我现在正在尝试稍后执行此操作:

var rgb:uint = bitMapWheel.getPixel(sWheel.mouseX,sWheel.mouseY);

并得到一个错误

"1061: Call to a possibly undefined method getPixel through a reference with static type flash.display:Bitmap."

这是什么意思?我不能在位图上使用 getPixel 吗?很抱歉新手,出于某种原因,Flash 对我来说非常难学。

你的StageWheelclass是BitmapData,它本身不是可以添加到舞台的显示对象。

您需要将位图数据包装到 Bitmap 中,使其成为显示对象。

var sWheel:BitmapData = new StageWheel(); //This is bitmap data, which is not a display object, just data at this point.

//to display the bitmap data, you need to create a bitmap and tell that bitmap to use the bitmap data
var bitmapWheel:Bitmap = new Bitmap(sWheel);

//now you can add the bitmap to the display list
addChild(bitmapWheel);

编辑 对于问题的第二部分,您需要访问位图的位图数据才能使用 getPixel

bitmapWheel.bitmapData.getPixel(bitmapWheel.mouseX,bitmapWheel.mouseY);