0

我正在尝试使用我在 Flash CS6 中制作的游戏来学习 Actionscript 3.0,但我在 Document Class 方面遇到了一些问题。最初,我有一个工作菜单,其中包含一些用于键盘事件和声音的脚本。我意识到我需要以可以从任何框架访问它们的方式存储一些变量,因此我创建了一个带有空类的文档类并将我的游戏设置为引用它,现在我的菜单脚本正在生成编译器错误。我得到的错误是“1046:找不到类型或不是编译时常量:KeyboardEvent”,这对我来说没有任何意义,因为它事先工作得很好。有人知道问题可能是什么吗?谢谢!

文档类:

package  
{  
    import flash.display.MovieClip;  
    public class Main extends MovieClip  
    {  

    }  
}

菜单脚本:

import flash.utils.getDefinitionByName;
import flash.ui.Keyboard;

stop();//Used to stay on the current frame

var selection:int = 0;//Will be used to determine which button has its "On" animation activated
var canMove:Boolean = true;

var menuSong:Sound = new MenuSong();
menuSong.play (0 , 9999);//Plays and loops(9999 times) menu theme

var menuMove:Sound = new MenuMove();
var menuSelect:Sound = new MenuSelect();

stage.addEventListener(KeyboardEvent.KEY_DOWN, move);//Calls move function when a key is pressed

function move(event:KeyboardEvent):void{//The line causing the error
    if(canMove){
        if(event.keyCode == 40){
            selection = (selection + 1)%3;//Occurs when down key is pressed
            menuMove.play();
        }
        else if(event.keyCode == 38){
            selection = (selection + 2)%3;//Occurs when up key is pressed
            menuMove.play();
        }
        else if(event.keyCode == 32){
            canMove = false;
            SoundMixer.stopAll();
            menuSelect.play();
            fadeOut.gotoAndPlay(1);
        }

        switch(selection){
            case 0:
                this.singlePlayer.gotoAndPlay("On");
                this.multiplayer.gotoAndStop("Off");
                this.credits.gotoAndStop("Off");
                break;
            case 1:
                this.singlePlayer.gotoAndStop("Off");
                this.multiplayer.gotoAndPlay("On");
                this.credits.gotoAndStop("Off");
                break;
            case 2:
                this.singlePlayer.gotoAndStop("Off");
                this.multiplayer.gotoAndStop("Off");
                this.credits.gotoAndPlay("On");
                break;  
        }//All this just tells the selected button (Based on the selection variable)
        //to play its "On" animation, and the other buttons to play their "Off" animation.
    }
}
4

1 回答 1

1

您需要flash.events.KeyboardEvent在代码中使用它时导入(菜单脚本)。

为什么不使用称为“菜单脚本”的脚本作为文档类?如果 SWF 的目标是在菜单脚本代码中设计的,那么它应该是 Document Class。

以某种方式,如果您stage.addEventListener(KeyboardEvent.KEY_DOWN, move);在代码中使用,则必须导入flash.utils.KeyboardEvent. Sound ( import flash.media.Sound) 和 SoundMixer ( import flash.media.SoundMixer) 也是如此。

于 2013-05-02T22:18:28.973 回答