【问题标题】:Actionscript 3.0: Addition of Document Class causes errorsActionscript 3.0:添加文档类会导致错误
【发布时间】:2013-05-02 20:10:26
【问题描述】:

我正在尝试使用我在 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.
    }
}

【问题讨论】:

  • 您好像没有导入 KeyboardEvent。无论如何,你正在做的不是处理这个问题的正确方法。通过为您的主 MC 设置一个文档类,您将删除 MC 的“动态性”,并且您将无法为其添加属性(无论如何,这完全是处理这种情况的错误方法)。您为什么不从一个阐明最终目标的新问题开始,而不是从您认为应该采取的解决方案开始?
  • 感谢您的回复!我的代码在没有事先导入的情况下运行良好,所以我没有意识到我需要设置它。我的最终目标是能够在帧之间传递变量(而且我不想发布重复的问题),所以我尝试使用另一个堆栈溢出答案(stackoverflow.com/questions/4028250/…)提出的解决方案。
  • 嗯,这些解决方案都不像你所做的那样,但如果你的代码对你很好的话。

标签: actionscript-3 compiler-errors flash-cs6 document-class


【解决方案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) 也是如此。

【讨论】:

    猜你喜欢
    • 2012-11-07
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多