【问题标题】:How do I get this to work with a class instead of an embedded image?如何让它与类而不是嵌入图像一起使用?
【发布时间】:2012-06-19 13:37:09
【问题描述】:

我正在尝试创建一个游戏,在您单击对象后,该对象会发光。当我嵌入图像并通过位图传递时,它可以正常工作,如下所示:

   package  
    {
    import org.flixel.*;    

    public class HelloWorld extends FlxState

{

    [Embed(source = "pics/Cancer.png")] private var cancerIMG:Class;
    private var cancer:FlxSprite; 

    private var title_text:FlxText;     

    public function HelloWorld() 
    {
    }       

    override public function create():void
    {           

        /**
         * Glow method test
         */  
        //Start Test
        var bigbitmap:BitmapData = new BitmapData(320, 288, true, 0x00000000);
        bigbitmap.copyPixels(Bitmap(new cancerIMG).bitmapData, new Rectangle(0, 0, 160, 288), new Point(16, 16), null, null, true);

        cancer = new FlxSprite;
        cancer.pixels = bigbitmap;

        add(cancer);        
        //End Test

    }

    override public function update():void
    {

        super.update();          
    }       

}   
} 

但是,每当我尝试使用相同的方法传递类精灵时,就像这样:

        _bx = new box(120, 120);            

        var bigbitmap:BitmapData = new BitmapData(320, 288, true, 0x00000000);
        bigbitmap.copyPixels(Bitmap(_bx.pixels).bitmapData, new Rectangle(0, 0, 160, 288), new Point(16, 16), null, null, true);

        _bx.pixels = bigbitmap;         
        add(_bx);

我这样称呼盒子:

    public var _bx:box; 

它本身就是一个 FlxExtendedSprite(为了测试,将其恢复为常规 FlxSprite)。当我尝试这样做时,我收到以下错误:

错误 #1034:类型强制失败:无法将 flash.display::BitmapData@3960701 转换为 flash.display.Bitmap。

有没有办法可以修复这种效果并让它与这样的类一起使用?

提前致谢!

【问题讨论】:

  • 听起来您正在尝试在需要位图的地方使用 BitmapData。但是,根据您的代码;我不确定是哪一行引发了错误,而且我不熟悉 Flixel 类。
  • 这让我想到了一个可能的解决方案。有什么办法可以将一个转换成另一个?
  • 是的,您可以使用 bitMapData 属性从 BitMap 获取 BitMapData。 help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/… 。您可以通过将数据指定为构造函数参数help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/… 从 BitMapData 创建位图
  • 好的。我会试试看它是否有效。
  • 我还能做些什么来让它发挥作用?

标签: flash apache-flex flixel


【解决方案1】:

_bx.pixels 是一个 BitmapData,所以我认为它就这么简单。至少在我的测试设置中工作。

bigbitmap.copyPixels(_bx.pixels, new Rectangle(0, 0, 160, 288), new Point(16, 16), null, null, true);

【讨论】:

  • 你试过了吗?有帮助吗?
【解决方案2】:

Really not that hard to find..

//This goes in the package, at the top of your .as
import flash.filters.GlowFilter;

//This goes in your class
var myFilter:GlowFilter = new GlowFilter(0xffffff, 1, 1.5, 1.5, 30, 4); //(color, alpha, blurX, blurY, strength, quality);

//This goes wherever you want to apply this method
myMC.filters = [myFilter];

【讨论】:

  • 投反对票的人,请告诉我你为什么这样做,以便我考虑这个建议:)
  • 好的,但这是 Adob​​e 文档,它不会随时关闭。但我明白你的意思。不过,我这样做的原因是,在回答时,我还说 OP 应该先查看它,然后再在 SO 上发布这样的问题。
猜你喜欢
  • 2022-10-01
  • 2017-07-16
  • 1970-01-01
  • 2010-12-07
  • 1970-01-01
  • 1970-01-01
  • 2020-09-28
  • 1970-01-01
  • 2011-01-31
相关资源
最近更新 更多