【问题标题】:What am I missing in this Embed & Texture create code?我在这个嵌入和纹理创建代码中缺少什么?
【发布时间】:2017-07-05 22:04:15
【问题描述】:

我正在尝试学习 Hemanth Sharma 的 Starling 视频教程。我已经按照他在第二个视频 (linked here) 中所做的那样输入了代码,但我很难获得相同的结果。

起初,当我的代码与夏尔马先生的匹配并显示为:

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

我使用此代码收到错误。该错误表明我试图从非对象实例化一个类。

我通过将其分解为多个步骤来跟踪代码过程。现在我的代码显示为:

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 先生的代码出现错误的原因是 Sharma 先生使用 Assets[name] 访问的 classObj 初始化为 null

先生。 Sharma 没有构造函数来设置静态变量的值,所以我假设 Embed 元标记与为该字段赋值有关。

我将从该文件中复制我的代码并将其粘贴到下面。

  1. 有人知道我在哪里犯错了吗?

  2. 谁能解释嵌入元标记的工作原理,以便我能够以比现在更多的技能找到错误?

资产.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];
        }
    }
}

【问题讨论】:

  • 使用代码Assets[name],只有“BgWelcome”或“WelcomeHero”等名称值有效。然后,Assets.getTexture(...) 显然会失败,因为 Assets.gemeTextures 字典已初始化但从未填充。
  • 这是一个很好的观点。我应该添加跟踪输出,以便您可以看到导致错误的字符串。我相信正在使用的字符串是“BgWelcome”。
  • @Organis,您能否添加您的评论作为答案,以便我给您积分。正如我所怀疑的那样调用了 BgWelcome,但是当我进一步查看跟踪时,导致错误的字符串实际上是“WelcomePlayBtn”,应该是“WelcomePlayButton”。

标签: actionscript-3 embed embedded-resource


【解决方案1】:

我目前正在开发一款游戏,并且有与您类似的设置,这是我一直在成功使用的代码:

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 具有更多功能,但想法相同)。

【讨论】:

    【解决方案2】:

    您也可以尝试..捕捉它以立即诊断错误。

    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];
    }
    

    【讨论】:

      猜你喜欢
      • 2020-05-11
      • 2012-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多