Actionscript 3 持续接收错误:访问AS3中未定义的属性菜单
我正在学习AS3使用flash develop创建pong克隆的教程(),但在显示主菜单时遇到了问题。我以前有一个功能正常的原型,带有一个移动的拨片,所以我相信错误出现在我修改过的(或新的)主菜单、主菜单、PongGame或CustomEvents文件中。当我按照教程的指示运行代码时,我收到了错误;在my Main.as文件中访问未定义的属性菜单:Actionscript 3 持续接收错误:访问AS3中未定义的属性菜单,actionscript-3,flashdevelop,Actionscript 3,Flashdevelop,我正在学习AS3使用flash develop创建pong克隆的教程(),但在显示主菜单时遇到了问题。我以前有一个功能正常的原型,带有一个移动的拨片,所以我相信错误出现在我修改过的(或新的)主菜单、主菜单、PongGame或CustomEvents文件中。当我按照教程的指示运行代码时,我收到了错误;在my Main.as文件中访问未定义的属性菜单: package { import flash.display.Sprite; import flash.even
package
{
import flash.display.Sprite;
import flash.events.Event;
/**
* ...
* @author
*/
public class Main extends Sprite
{
private var game:PongGame;
****private var menu:MainMenu;****
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
}
private function buildMenu():void {
menu = new MainMenu();
addChild(menu);
menu.addEventListener(CustomEvents.LAUNCH_GAME, startGame, false, 0, true);
}
private function startGame(e:CustomEvents):void {
removeChild(menu);
menu.removeEventListener(CustomEvents.LAUNCH_GAME, startGame);
menu = null;
game = new PongGame();
addChild(game);
}
}
}
所以我添加了“private-var-menu:MainMenu;”部分,但我不知道引用哪个类/.as文件?或如何定义变量菜单:
很抱歉附加了大量代码。我没有改变桨。因为这是最长的文件
资产负债表
划桨
蓬盖马斯
CustomEvents.as
我不确定从这里到哪里去修复这个错误。任何帮助都将不胜感激 尽管您已正确添加了menu属性,但由于从未调用
buildMenu()
函数,因此从未显示该菜单
从主类init()
,调用buildMenu()
实例化并将菜单添加到后台:
Main.as
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
buildMenu();
}
如果所有类都驻留在同一个包中,那么代码应该可以工作。添加菜单后,您会收到什么错误?还请注意,您的“CustomEvent.as”类应该是:“CustomEvents.as”事件是一个打字错误,已修复。我没有收到实际错误,但只显示了我的背景。我正在使用的教程没有*'d var菜单,这是当我收到一个错误时。所以,我添加了var菜单,但是没有显示任何内容,我不确定我是否纠正了它。没有错误,只是现在没有显示任何内容!我认为buildMenu从未被调用过,这很奇怪,但我不知道该在哪里调用它。我的教程中一定有错误。谢谢
package
{
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class Paddle extends Sprite {
private var pic:Bitmap = new Assets.Pad();
public function Paddle():void {
addEventListener(Event.ADDED_TO_STAGE, go);
}
private function die(e:Event):void {
removeChild(pic);
pic = null;
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
removeEventListener(Event.ENTER_FRAME, enterFrame);
removeEventListener(Event.REMOVED_FROM_STAGE, die);
}
private function go(e:Event) : void {
removeEventListener(Event.ADDED_TO_STAGE, go);
addChild(pic);
y = stage.stageHeight - pic.height;
x = stage.stageWidth * .5 - pic.width * .5;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler, false, 0, true);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler, false, 0, true);
addEventListener(Event.ENTER_FRAME, enterFrame, false, 0, true);
addEventListener(Event.REMOVED_FROM_STAGE, die);
}
private var movingLeft:Boolean;
private var movingRight:Boolean;
private function enterFrame(e:Event):void {
if (movingLeft) {
if(x-speed >= 0) {
x -= speed;
}
else {
x = 0;
}
}
else if (movingRight) {
if (x + speed + pic.width <= 600) {
x += speed;
}
else {
x = 600 - pic.width;
}
}
}
private var speed:int = 10;
private function keyDownHandler(e:KeyboardEvent):void {
if (e.keyCode == 38 || e.keyCode == 87) {
if (!movingLeft) {
movingLeft = true;
}
}
else if (e.keyCode == 40 || e.keyCode == 83) {
if (!movingRight) {
movingRight = true;
}
}
}
private function keyUpHandler(e:KeyboardEvent):void {
if (e.keyCode == 38 || e.keyCode == 87) {
if (movingLeft) {
movingLeft = false;
}
}
if (e.keyCode == 40 || e.keyCode == 83) {
if (movingRight) {
movingRight = false;
}
}
}
}
}
package
{
import flash.events.Event;
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.MouseEvent;
public class MainMenu extends Sprite
{
private var pongButton:Sprite;
public function MainMenu():void{
addEventListener(Event.ADDED_TO_STAGE, go);
}
private function go(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, go);
pongButton = item("Play", 20, 30, launchGame, 0xFF0000);
addChild(pongButton);
}
private function launchGame(e:MouseEvent):void {
dispatchEvent(new CustomEvents(CustomEvents.LAUNCH_GAME));
}
private function item(buttonText:String, X:int, Y:int, Funct:Function, txtColor:uint = 0xFFFFFF):Sprite {
var item:Sprite = new Sprite();
item.graphics.beginFill(0);
item.graphics.lineStyle(1, txtColor, .5);
item.graphics.drawRect(0, 0, 250, 30);
var myText:TextField = new TextField();
myText.selectable = false;
myText.width = 250;
myText.height = 30;
item.addChild(myText);
myText.autoSize = "center";
myText.text = buttonText;
myText.textColor = txtColor;
item.addEventListener(MouseEvent.CLICK, Funct);
item.x = X;
item.y = Y;
return item;
}
}
}
package
{
import flash.display.Sprite;
import flash.events.Event;
public class PongGame extends Sprite
{
private var paddle:Paddle;
public function PongGame():void {
addEventListener(Event.ADDED_TO_STAGE, go);
}
private function go(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, go);
paddle = new Paddle();
addChild(paddle);
}
}
}
package
{
import flash.events.Event;
public class CustomEvents extends Event
{
public static const LAUNCH_GAME:String = "launch_game";
public function CustomEvents(e:String):void {
super(e);
}
}
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
buildMenu();
}