0

我是 Starling 框架的新手,目前正在学习如何使用它。

我已经使用 starling 框架从嵌入的 PNG 文件创建了纹理,该框架可以完美地工作并显示在屏幕上,但我正在尝试让 spritesheet(Texture Atlas)工作,它给了我这个:

“错误 #1007:在非构造函数上尝试实例化。”

从我所做的所有研究中,我拥有的代码应该可以工作。

这是我的资产类中的适用代码。

public class Assets
{
    [Embed(source="assets/sky.png")]
    private static var SKY_CLASS:Class;
    public static var SKY:Texture;

    [embed(source="assets/generalsheet.png")]
    private static var GENERAL_SHEET_CLASS:Class;       
    [embed(source="assets/generalsheet.xml", mimeType="application/octet-stream")]
    private static var GENERAL_ATLAS_CLASS:Class;
    public static var GENERAL_SHEET:TextureAtlas;

    public static function init():void
    {
        SKY = Texture.fromBitmap(new SKY_CLASS());
        GENERAL_SHEET = new TextureAtlas(Texture.fromBitmap(new GENERAL_SHEET_CLASS()), XML(new GENERAL_ATLAS_CLASS())); // this is where Flash Builder tells me there is an error
    }
4

1 回答 1

1

您只需将Embed标签写成大写,只需更改:

[embed(source="assets/generalsheet.png")]
private static var GENERAL_SHEET_CLASS:Class;       
[embed(source="assets/generalsheet.xml", mimeType="application/octet-stream")]
private static var GENERAL_ATLAS_CLASS:Class;

到:

[Embed(source="assets/generalsheet.png")]
private static var GENERAL_SHEET_CLASS:Class;       
[Embed(source="assets/generalsheet.xml", mimeType="application/octet-stream")]
private static var GENERAL_ATLAS_CLASS:Class;

附带说明一下,类名通常用UpperCamelCase编写,而ALL_CAPITALIZED是为常量保留的。变量名通常用lowerCamelCaselowercase_separated_by_underscore书写。这是大多数 ActionScript3(和 Java)程序员遵循的约定,如果您坚持下去,您的代码将更具可读性,因此下次应该更容易为您提供帮助;)

所以我推荐:

[Embed(source="assets/sky.png")]
private static var SkyClass:Class;

public static var sky:Texture;

[Embed(source="assets/generalsheet.png")]
private static var GeneralSheetClass:Class;

[Embed(source="assets/generalsheet.xml", mimeType="application/octet-stream")]
private static var GeneralAtlasClass:Class;

public static var general_sheet;

public static function init():void
{
    sky = Texture.fromBitmap(new SkyClass());

    general_sheet = new TextureAtlas(Texture.fromBitmap(new GeneralSheetClass()), XML(new GeneralAtlasClass()));
}
于 2012-07-24T06:45:48.097 回答