Actionscript 3.0: Addition of Document Class causes errors

143 Views Asked by At

I'm trying to learn Actionscript 3.0 with a game I'm making in Flash CS6, and I'm having some issues with the Document Class. Initially, I had a working menu with some scripting for keyboard events and sound. I realized that I needed to store some variables in a way that I could access them from any frame, so I created a Document Class with an empty class and set my game to reference it, and now my menu scripting is generating a compiler error. The error I'm getting is "1046: Type was not found or was not a compile-time constant: KeyboardEvent," which doesn't make any sense to me since it worked just fine beforehand. Anybody have any idea what the problem might be? Thanks!

Document Class:

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

    }  
}

Menu Script:

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.
    }
}
1

There are 1 best solutions below

0
On

You need to import flash.events.KeyboardEvent as you use it in your code (Menu script).

Why dont you use the script you called "Menu Script" as Document Class ? If the goal of your SWF is what is designed in the Menu Script code, it should be the Document Class.

In a way or another, if you use stage.addEventListener(KeyboardEvent.KEY_DOWN, move); in your code, you must import flash.utils.KeyboardEvent. Same thing for Sound ( import flash.media.Sound) & SoundMixer (import flash.media.SoundMixer).