jeudi 16 février 2017

What am I missing in this Embed & Texture create code?

I am trying to follow the Starling video tutorials by Hemanth Sharma. I have typed the code as he has done on his 2nd video (linked here), but I am having difficulty getting the same results.

At first, when my code matched Mr. Sharma's and appeared as:

var bitmap:Bitmap = new Assets[name]();
gameTextures[name] = Texture.fromBitmap( bitmap );

I received the error using this code. The error stated that I was trying to instantiate a class from a non-object.

I traced the code process by breaking this into steps. Now my code appears as:

trace( "Building for", name );
var classObj : Class = Assets[name];
trace( "Class", classObj );
var bitmap : Bitmap = new classObj() as Bitmap;
trace( "Bitmap", bitmap );
trace( "Assign value" );
Assets["gameTextures"][name] = Texture.fromBitmap( bitmap );
trace("Value assigned" );

Using this code, I am able to see that the reason I get the error with Mr. Sharma's code is that the classObj, which Mr. Sharma accesses using Assets[name], initializes to null.

Mr. Sharma has no constructor to set the value of the static variables, so I assume that the Embed meta tag has something to do with the assignment of value to this field.

I will copy my code from that file and paste it below.

  1. Does anyone see where I am making my error?

  2. Can anyone explain how Embed metatags work so that I can find the error with more skill than I have now?

Assets.as:

package
{
    import flash.display.Bitmap;
    import flash.utils.Dictionary;

    import starling.textures.Texture;

    public class Assets
    {
        [Embed(source="../media/graphics/bgWelcome.jpg")]
        public static const BgWelcome:Class;

        [Embed(source="../media/graphics/welcome_hero.png")]
        public static const WelcomeHero:Class;

        [Embed(source="../media/graphics/welcome_title.png")]
        public static const WelcomeTitle:Class;

        [Embed(source="../media/graphics/welcome_playButton.png")]
        public static const WelcomePlayButton:Class;

        [Embed(source="../media/graphics/welcome_aboutButton.png")]
        public static const WelcomeAboutButton:Class;

        private static var gameTextures:Dictionary = new Dictionary();

        public static function getTexture( name: String ): Texture
        {
            if( Assets.gameTextures[name] == undefined ){
                trace( "Building for", name );
                var classObj : Class = Assets[name];
                trace( "Class", classObj );
                var bitmap : Bitmap = new classObj() as Bitmap;
                trace( "Bitmap", bitmap );
                trace( "Assign value" );
                Assets["gameTextures"][name] = Texture.fromBitmap( bitmap );
                trace("Value assigned" );
            }
            return Assets.gameTextures[name];
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire