无论我做什么,图像都保持为空

时间:2015-03-30 07:06:38

标签: actionscript-3 starling-framework

我正在制作一个动作脚本项目,无论我做了什么,我都无法从Atlas中获得纹理,最后我不得不做一些我不想保留的快速修改。有没有人知道为什么我无法使用createDiabloCrashArt方法获取纹理:

package gameObjects
{
    import starling.core.Starling;
    import starling.display.Image;
    import starling.display.MovieClip;
    import starling.display.Sprite;
    import starling.events.Event;
    import starling.utils.deg2rad;

    public class Diablo extends Sprite
    {
        private var _type:int;
        private var _speed:int;
        private var _distance:int;
        private var _alreadyHit:Boolean;
        private var _position:String;
        private var _hitArea:Image;
        private var diabloImage:Image;
        private var diabloAnimation:MovieClip;
        private var diabloCrashImage:Image;

        public function Diablo(_ptype:int, _pdistance:int)
        {
            super();

            this._type = _ptype;
            this._distance = _pdistance;
            this._speed = GameConstants.DIABLO_SPEED;

            _alreadyHit = false;

            this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        }

        private function onAddedToStage(e:Event):void
        {
            this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

            createDiabloArt();
        }

        private function createDiabloArt():void
        { //Gets some other stuff using the same getAtlas but as MovieAnimation and works.
        }

        private function createDiabloCrashArt():void
        {
            // trace("diablo_chingo" + _type + "KO");

            if (diabloCrashImage == null)
            {
                diabloCrashImage = new Image(Assets.getAtlas().getTexture("diablo_chingo" + _type + "KO"));
                this.addChild(diabloCrashImage);
            }
            else
            {
                diabloCrashImage.texture = Assets.getAtlas().getTexture("diablo_chingo" + _type + "KO");
            }

            diabloCrashImage.visible = false;
        }

        private function hidePreviousInstance():void
        {
            if (diabloAnimation != null && _type <= GameConstants.DIABLO_TYPE_4)
            {
                diabloAnimation.visible = false;
                Starling.juggler.remove(diabloAnimation);
            }

            if (diabloImage != null) diabloImage.visible = false;
        }

        public function get type():int { return _type; }

        public function set type(value:int):void 
        {
            _type = value;
            resetForReuse();
            hidePreviousInstance();
            createDiabloArt();
        }

        public function get alreadyHit():Boolean { return _alreadyHit; }
        public function set alreadyHit(value:Boolean):void
        {
            _alreadyHit = value;

            if (value)
            {
                diabloCrashImage.visible = true;

                if (_type >= GameConstants.DIABLO_TYPE_1 || _type <= GameConstants.DIABLO_TYPE_4)
                {
                    diabloAnimation.visible = false;
                }
                else
                {
                    diabloImage.visible = false;
                    Starling.juggler.remove(diabloAnimation);
                }
            }
        }

        public function resetForReuse():void
        {
            this.alreadyHit = false;
            this.rotation = deg2rad(0);
        }
  }
 }

但它通过改变以下事项起作用:

private function onAddedToStage(e:Event):void
{
    this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

    createDiabloArt();
    createDiabloCrashArt();
}

private function createDiabloCrashArt():void
{
    // trace("diablo_chingo" + _type + "KO");

    if (diabloCrashImage == null)
    {
        diabloCrashImage = new Image(Assets.getTexture("Diablo1"));
        this.addChild(diabloCrashImage);
    }
    else
    {
        diabloCrashImage.texture = Assets.getTexture("Diablo1");

        //Assets.getAtlas().getTexture("diablo_chingo" + _type + "KO");
    }

    diabloCrashImage.visible = false;
}

我通过调试器,跟踪等一次又一次地验证了正确的参数到达函数,例如_type,事实上,在我没有获得我需要的纹理的长方法中使用上面的语法非常有效。

我试图:

  1. 将图像更改为MovieClip
  2. 在进入createCrashArt之前初始化
  3. 获取其他纹理,例如我已经可以显示的纹理(没有工作)
  4. 盯着它真的很难。
  5. 设置纹理的默认字符串。
  6. 在我将代码更改为第二个代码段之前,没有任何东西产生任何东西,问题是我不知道为什么它不起作用而我宁愿不依赖于可能的快速修复可能不会在以后变成挫折。

    说真的,任何理解这一点的帮助都会很棒。我甚至不在乎现在正在工作,我不能为我的生活找出最初的错误。

    提前致谢。

    编辑:

        public static function getAtlas():TextureAtlas
        {
            if (gameTextureAtlas == null)
            {
                var texture:Texture = getTexture("AtlasTextureGame");
                var xml:XML = XML(new AtlasXmlGame());
                gameTextureAtlas=new TextureAtlas(texture, xml);
            }
    
            return gameTextureAtlas;
        }
    
        /**
         * Returns a texture from this class based on a string key.
         * 
         * @param name A key that matches a static constant of Bitmap type.
         * @return a starling texture.
         */
    
        public static function getTexture(name:String):Texture
        {
    
            if (gameTextures[name] == undefined)
            {
    
                var bitmap:Bitmap = new Assets[name]();
                gameTextures[name]=Texture.fromBitmap(bitmap);
            }
    
            //trace("Tomando textura!");
            return gameTextures[name];
        }
    

    编辑:资产类

    package
    {
        import flash.display.Bitmap;
        import flash.utils.Dictionary;
    
        import starling.textures.Texture;
        import starling.textures.TextureAtlas;
    
        public class Assets
        {
    
                /**
                 * Atlas de texturas. 
                 */
    
                [Embed(source="../Witchmedia/graphics/Spritesheet/ScarletWitch.png")]
                public static const AtlasTextureGame:Class;
    
                [Embed(source="../Witchmedia/graphics/Spritesheet/ScarletWitch.xml", mimeType="application/octet-stream")]
                public static const AtlasXmlGame:Class;
    
                /**
                 *  Assets de Fondo y botones.
                 */
    
                [Embed(source="../Witchmedia/graphics/bgLayer3.jpg")]
                public static const BgLayer1:Class;
    
                [Embed(source="../Witchmedia/graphics/Diablo1.png")]
                public static const Diablo1:Class;
    
                /**
                 * Cache de Texturas 
                 */
    
                private static var gameTextures:Dictionary = new Dictionary();
                private static var gameTextureAtlas:TextureAtlas;
    
                /**
                 * Returna una instancia del atlas de texturas.
                 * @return the TextureAtlas instance (Singleton)
                 */
    
                public static function getAtlas():TextureAtlas
                {
                    if (gameTextureAtlas == null)
                    {
                        var texture:Texture = getTexture("AtlasTextureGame");
                        var xml:XML = XML(new AtlasXmlGame());
                        gameTextureAtlas=new TextureAtlas(texture, xml);
                    }
    
                    return gameTextureAtlas;
                }
    
                /**
                 * Returns a texture from this class based on a string key.
                 * 
                 * @param name A key that matches a static constant of Bitmap type.
                 * @return a starling texture.
                 */
    
                public static function getTexture(name:String):Texture
                {
    
                    if (gameTextures[name] == undefined)
                    {
    
                        var bitmap:Bitmap = new Assets[name]();
                        gameTextures[name]=Texture.fromBitmap(bitmap);
                    }
    
                    //trace("Tomando textura!");
                    return gameTextures[name];
                }
            }
    

1 个答案:

答案 0 :(得分:1)

Can you check with this method, Give the texture declaration in a separate class, i guess it doesnt take the appropriate texture. 


public static function getAtlas(atlasNumb:uint = 1):TextureAtlas
        {
            if(sTextureAtlas[atlasNumb - 1] == null)
            {
                var texture:Texture = getTexture("AtlasTexture"+atlasNumb);
                var xml:XML = XML(create("AtlasXml"+atlasNumb));
                sTextureAtlas[atlasNumb-1] = new TextureAtlas(texture, xml);
            }
            return sTextureAtlas[atlasNumb - 1];
        }

        private static function create(name:String):Object
        {
            var textureClass:Class =  AssetEmbeds_2x;                
            return new textureClass[name];
        }


And define the textures .png and xml in a separate file called AssetEmbeds_2x.

while calling the texture define like 
Assets.getAtlas(1).getTextures("xxx"). I hope it wll