我在此嵌入和纹理创建代码中缺少什么?
What am I missing in this Embed & Texture create code?
我正在尝试学习 Hemanth Sharma 的 Starling 视频教程。我输入了他在第二个视频 (linked here) 中所做的代码,但我很难获得相同的结果。
起初,当我的代码与 Sharma 先生的匹配并显示为:
var bitmap:Bitmap = new Assets[name]();
gameTextures[name] = Texture.fromBitmap( bitmap );
我在使用此代码时收到错误消息。该错误表明我正在尝试从非对象实例化 class。
我通过将其分解为多个步骤来跟踪代码过程。现在我的代码显示为:
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" );
使用这段代码,我可以看出我在 Sharma 先生的代码中遇到错误的原因是 classObj
,Sharma 先生使用 Assets[name]
访问的 classObj
初始化为 null
.
先生Sharma 没有构造函数来设置静态变量的值,因此我假设 Embed meta 标记与为该字段赋值有关。
我将从该文件中复制我的代码并将其粘贴到下面。
有人看到我哪里出错了吗?
谁能解释一下嵌入元标记的工作原理,以便我可以比现在更熟练地找到错误?
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];
}
}
}
您也可以尝试..捕获它以立即诊断错误。
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];
}
我目前正在开发一款游戏,我的设置与您的类似,这是我成功使用的代码:
EmbeddedAssets.as(在名为 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);
}
}
然后,在我的代码中的其他地方,我使用这些引用如下:
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;
}
请注意,我使用的是 Feathers UI 按钮,而不是 Starling 按钮(Feathers Buttons 具有更多功能,但想法是相同的)。
我正在尝试学习 Hemanth Sharma 的 Starling 视频教程。我输入了他在第二个视频 (linked here) 中所做的代码,但我很难获得相同的结果。
起初,当我的代码与 Sharma 先生的匹配并显示为:
var bitmap:Bitmap = new Assets[name]();
gameTextures[name] = Texture.fromBitmap( bitmap );
我在使用此代码时收到错误消息。该错误表明我正在尝试从非对象实例化 class。
我通过将其分解为多个步骤来跟踪代码过程。现在我的代码显示为:
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" );
使用这段代码,我可以看出我在 Sharma 先生的代码中遇到错误的原因是 classObj
,Sharma 先生使用 Assets[name]
访问的 classObj
初始化为 null
.
先生Sharma 没有构造函数来设置静态变量的值,因此我假设 Embed meta 标记与为该字段赋值有关。
我将从该文件中复制我的代码并将其粘贴到下面。
有人看到我哪里出错了吗?
谁能解释一下嵌入元标记的工作原理,以便我可以比现在更熟练地找到错误?
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];
}
}
}
您也可以尝试..捕获它以立即诊断错误。
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];
}
我目前正在开发一款游戏,我的设置与您的类似,这是我成功使用的代码:
EmbeddedAssets.as(在名为 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);
}
}
然后,在我的代码中的其他地方,我使用这些引用如下:
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;
}
请注意,我使用的是 Feathers UI 按钮,而不是 Starling 按钮(Feathers Buttons 具有更多功能,但想法是相同的)。