TypeError:错误#1009:无法访问空对象引用的属性或方法| FLASH BUILD/STARLING FRMWRK
我正在使用的语言是Actionscript,我对它还是一种新手。在Flash构建中运行调试模式后(使用starling框架)出现以下错误 我将列出并显示每个类的所有代码 游戏类-TypeError:错误#1009:无法访问空对象引用的属性或方法| FLASH BUILD/STARLING FRMWRK,flash,Flash,我正在使用的语言是Actionscript,我对它还是一种新手。在Flash构建中运行调试模式后(使用starling框架)出现以下错误 我将列出并显示每个类的所有代码 游戏类- package { import starling.display.Sprite; import starling.events.Event; import screens.Welcome; public class Game extends Sprite {
package
{
import starling.display.Sprite;
import starling.events.Event;
import screens.Welcome;
public class Game extends Sprite
{
private var screenWelcome:Welcome;
public function Game()
{
super();
this.addEventListener(starling.events.Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(event:Event):void
{
trace("starling framework initialized!");
screenWelcome = new Welcome();
this.addChild(screenWelcome);
screenWelcome.initialize();
}
}
}
资产类别-
package
{
import flash.utils.Dictionary;
import starling.textures.Texture;
import flash.display.Bitmap;
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 WelcomePlayBtn:Class;
[Embed(source="../media/graphics/welcome_aboutButton.png")]
public static const WelcomeAboutBtn:Class;
private static var gameTextures:Dictionary = new Dictionary()
public static function getTexture(name:String):Texture
{
if (gameTextures[name] == undefined)
{
var bitmap:Bitmap = new Assets[name]();
gameTextures[name] = Texture.fromBitmap(bitmap);
}
return gameTextures[name];
}
}
}
欢迎光临
package screens
{
import starling.display.Button;
import starling.display.Image;
import starling.display.Sprite;
import starling.events.Event;
public class Welcome extends Sprite
{
private var bg:Image;
private var title:Image;
private var hero:Image;
private var playBtn:Button;
private var aboutBtn:Button;
public function Welcome()
{
super();
this.addEventListener(starling.events.Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(event:starling.events.Event):void
{
trace("welcome screen initialized!");
drawScreen();
}
private function drawScreen():void
{
bg= new Image(Assets.getTexture("BgWelcome"));
this.addChild(bg);
title= new Image(Assets.getTexture("WelcomeTitle"));
title.x = 440;
title.y = 20;
this.addChild(title);
bg= new Image(Assets.getTexture("WelcomeHero"));
this.addChild(hero);
hero.x = -hero.width;
hero.y = 100;
playBtn = new Button(Assets.getTexture("WelcomePlayBtn"));
this.addChild(playBtn);
playBtn.x = 500;
playBtn.y = 260;
this.addChild(playBtn);
aboutBtn = new Button(Assets.getTexture("WelcomeAboutBtn"));
aboutBtn.x = 410;
aboutBtn.y = 380;
this.addChild(playBtn);
}
public function initialize():void
{
this.visible = true;
hero.x = -hero.width;
hero.y = 100;
}
}
}
最后但并非最不重要的是“StarlingProject.as”
package
{
import flash.display.Sprite;
import starling.core.Starling;
[SWF(frameRate="60", width="800". baxkroundColour="0x3333333")]
public class StarlingProject extends Sprite
{
private var myStarling:Starling;
public function StarlingProject()
{
myStarling = new Starling(Game, stage);
myStarling.antiAliasing = 1;
myStarling.start();
}
}
}
请试着给我回电话。:)
谢谢,
DT.资产类别的实例没有BgWelcome、WelcomeHero等属性 这将是一个实例属性:
public const BgWelcome:Class;
public static const BgWelcome:Class;
public const BgWelcome:Class;
这是一个类属性:
public const BgWelcome:Class;
public static const BgWelcome:Class;
public const BgWelcome:Class;
static关键字使其成为类属性,而不是实例属性
当您这样做时:
new Assets[name]();
创建资产实例并尝试访问该范围中不存在的属性。如果您像这样声明变量,它将起作用: