0

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.
    }
}
Warman Steve
  • 17
  • 1
  • 6
  • It doesn't look like you imported KeyboardEvent. Regardless, what you are doing is not the right way to handle this. By setting a Document Class to your main MC, you're removing the "dynamicness" of MC and you won't be able to add properties to it (which is totally the wrong way to handle this situation anyway). Why don't you start over with a new question that articulates the end goal instead of the solution you think you ought to be doing? – Amy Blankenship May 02 '13 at 20:19
  • Thanks for the reply! My code worked fine without the imports beforehand, so I didn't realize I needed to set that up. My end goal was to be able to pass variables between frames (And I didn't want to post a duplicate question), so I tried to use the solution laid out by another stack overflow answer (http://stackoverflow.com/questions/4028250/passing-a-variable-between-frames-with-actionscript-3). – Warman Steve May 02 '13 at 20:53
  • Hm, none of those solutions look like what you did, but if your code is working good on you. – Amy Blankenship May 03 '13 at 01:55

1 Answers1

1

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).

RafH
  • 4,504
  • 2
  • 23
  • 23