Reputation: 1065
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.
Does anyone see where I am making my error?
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];
}
}
}
Upvotes: 1
Views: 57
Reputation: 517
I'm currently working on a game and have a similar setup like yours, this is the code I have been using with success:
EmbeddedAssets.as (inside a folder named utils):
package utils
{
import starling.textures.Texture;
public class EmbeddedAssets
{
[Embed(source = "./../assets/icons/stats.png")]
private static const stats:Class;
public static const statsButtonTexture:Texture = Texture.fromEmbeddedAsset(stats);
Embed(source = "./../assets/icons/stats_down.png")]
private static const stats_down:Class;
public static const statsButtonDownTexture:Texture = Texture.fromEmbeddedAsset(stats_down);
}
}
Then, somewhere else in my code I use those reference as follows:
private function setStatsButtonStyles(button:Button):void
{
var defaultICon:ImageLoader = new ImageLoader();
defaultIcon.source = EmbeddedAssets.statsButtonTexture;
defaultIcon.width = 60;
defaultIcon.height = 60;
var downIcon:ImageLoader = new ImageLoader();
downIcon.source = EmbeddedAssets.statsButtonDownTexture;
downIcon.width = 60;
downIcon.height = 60;
button.defaultIcon = defaultIcon;
button.downIcon = downIcon;
button.width = button.height = 60;
}
Note that I use a Feathers UI Button, not a Starling one (Feathers Buttons have more functionality but the idea is the same).
Upvotes: 0
Reputation: 7316
You as well might try..catch it to diagnose error immediately.
public static function getTexture(name:String):Texture
{
if (!Assets.gameTextures[name])
{
try
{
var aClass:Class = Assets[name];
var aRaster:Bitmap = new aClass();
Assets.gameTextures[name] = Texture.fromBitmap(aRaster);
}
catch (fail:Error)
{
throw "There's no texture <" + name + "> in Assets.";
}
}
return Assets.gameTextures[name];
}
Upvotes: 1