AS3层之间的变量和响应

Variables and Responses between layers AS3

我在当前项目中遇到了一个小问题...我有一个对象,单击它会激活一个函数,该函数会隐藏多个对象并更改一个变量。改变所述变量然后应该导致另一层上的对象出现,但所发生的只是对象消失,而另一个没有出现......第一层的代码如下......有问题的功能是最后一个...

import flash.events.Event;
import flash.system.fscommand;
import flash.events.MouseEvent;
stage.displayState = StageDisplayState.FULL_SCREEN; 
var GameMode:Number=0;//Variable Setup
var Planet:Number=0;//
swslogo.alpha = .0;//Game Menu
voidlogomenu.alpha = .0;
newgame.alpha = .0;
loadgame.alpha = .0;
exitgame.alpha = .0;
yes.alpha = .0;
no.alpha = .0;
exitprompt.alpha = .0;
MenuExitFade.alpha = .0;
SwsLogoCorner.alpha = .0;
var IntroTimer = new Timer(3000,1); //add a 3 second timer
var QuitTimer = new Timer(1000,1); //add a 1 second timer
var IntroTimer2 = new Timer(5000,1); //add a 5 second timer
IntroTimer.addEventListener(TimerEvent.TIMER_COMPLETE,swsfadein);
intro();
function intro():void
{
     IntroTimer.reset()
     IntroTimer.start();
    for (var i:int = 0; i < 50; i++)
{
    var aStar:DisplayObject = new stars;
    MenuSpawner.addChild(aStar);
}
for (var i:int = 0; i < 50; i++)
{
    var aStarDust:DisplayObject = new Stardust;
    MenuSpawner.addChild(aStarDust);
}
}
function swsfadein(e:Event=null):void
{
    IntroTimer.stop();
    IntroTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,swsfadein);
    swslogo.addEventListener(Event.ENTER_FRAME, fadeinsws)
}
function fadeinsws(e:Event=null){
    if(swslogo.alpha >= 1){
        swslogo.removeEventListener(Event.ENTER_FRAME, fadeinsws);
        IntroTimer2.addEventListener(TimerEvent.TIMER_COMPLETE,swsfadeout);
        IntroTimer2.reset()
        IntroTimer2.start();
    } else {
        swslogo.alpha +=0.1;
    }
}
function swsfadeout(e:Event=null):void
{
    IntroTimer2.stop();
    IntroTimer2.removeEventListener(TimerEvent.TIMER_COMPLETE,swsfadeout);
    swslogo.addEventListener(Event.ENTER_FRAME, fadeoutsws)
}
function fadeoutsws(e:Event=null){
if(swslogo.alpha <=0){
        swslogo.removeEventListener(Event.ENTER_FRAME, fadeoutsws);
    IntroTimer.addEventListener(TimerEvent.TIMER_COMPLETE,voidfadein);
    IntroTimer.reset()
    IntroTimer.start()
    } else {
        swslogo.alpha -=0.1;
    }
}
function voidfadein(e:Event=null):void
{
        IntroTimer.stop();
    IntroTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,voidfadein);
    voidlogomenu.addEventListener(Event.ENTER_FRAME, fadeinvoid)
}
function fadeinvoid(e:Event=null){
    if(voidlogomenu.alpha >= 1){
        voidlogomenu.removeEventListener(Event.ENTER_FRAME, fadeinvoid);
        IntroTimer2.addEventListener(TimerEvent.TIMER_COMPLETE,menufadein);
        IntroTimer2.reset()
        IntroTimer2.start();
    } else {
        voidlogomenu.alpha +=0.1;
    }
}
function menufadein(e:Event=null):void
{
    IntroTimer2.stop();
    IntroTimer2.removeEventListener(TimerEvent.TIMER_COMPLETE,menufadein);
    newgame.addEventListener(Event.ENTER_FRAME, fadeinnewgame);
    loadgame.addEventListener(Event.ENTER_FRAME, fadeinloadgame);
    exitgame.addEventListener(Event.ENTER_FRAME, fadeinexitgame);
    SwsLogoCorner.addEventListener(Event.ENTER_FRAME, fadeinswslogocorner)
}
function fadeinnewgame(e:Event=null){
    if(newgame.alpha >= 1){
        newgame.addEventListener(MouseEvent.CLICK, newgameclicked);
        newgame.removeEventListener(Event.ENTER_FRAME, fadeinnewgame);
    } else {
        newgame.alpha +=0.1;
    }
}
function fadeinloadgame(e:Event=null){
    if(loadgame.alpha >= 1){
        loadgame.addEventListener(MouseEvent.CLICK, loadgameclicked);
        loadgame.removeEventListener(Event.ENTER_FRAME, fadeinloadgame);
    } else {
        loadgame.alpha +=0.1;
    }
}
function fadeinexitgame(e:Event=null){
    if(exitgame.alpha >= 1){
        exitgame.addEventListener(MouseEvent.CLICK, exitgameclicked)
        exitgame.removeEventListener(Event.ENTER_FRAME, fadeinexitgame);
    } else {
        exitgame.alpha +=0.1;
    }
}
function fadeinswslogocorner(e:Event=null){
    if(SwsLogoCorner.alpha >= 1){
        SwsLogoCorner.removeEventListener(Event.ENTER_FRAME, fadeinswslogocorner);
    } else {
        SwsLogoCorner.alpha +=0.1;
    }
}
function newgameclicked(e:MouseEvent):void
{
     yesnofadein();
     exitprompt.addEventListener(Event.ENTER_FRAME, exitpromptfadein)
     exitprompt.gotoAndStop(2)
     yes.addEventListener(MouseEvent.CLICK, createnewgame)
     removemenufunctions();
}
function loadgameclicked(e:MouseEvent):void
{
     removemenufunctions();
}
function exitgameclicked(e:MouseEvent)
{
     yesnofadein();
     exitprompt.addEventListener(Event.ENTER_FRAME, exitpromptfadein)
     exitprompt.gotoAndStop(1)
     yes.addEventListener(MouseEvent.CLICK, exit)
     removemenufunctions();
}
function exitpromptfadein(e:Event=null){
    if(exitprompt.alpha >= 1){
        no.addEventListener(MouseEvent.CLICK, exitpromptfadeout)
        exitprompt.removeEventListener(Event.ENTER_FRAME, exitpromptfadein);
    } else {
        exitprompt.alpha +=0.1;
    }
}
function exitpromptfadeout(e:Event=null):void
{
     no.removeEventListener(MouseEvent.CLICK, exitpromptfadeout)
     exitprompt.addEventListener(Event.ENTER_FRAME, fadeoutexitprompt);
}
function fadeoutexitprompt(e:Event=null){
if(exitprompt.alpha <=0){
        exitprompt.removeEventListener(Event.ENTER_FRAME, fadeoutexitprompt);
    } else {
        exitprompt.alpha -=0.1;
    }
}
function yesnofadein():void
{
    yes.addEventListener(Event.ENTER_FRAME, fadeinyes)
    no.addEventListener(Event.ENTER_FRAME, fadeinno)
}
function fadeinyes(e:Event=null){
    if(yes.alpha >= 1){
        yes.removeEventListener(Event.ENTER_FRAME, fadeinyes);
    } else {
        yes.alpha +=0.1;
    }
}
function fadeinno(e:Event=null){
    if(no.alpha >= 1){
        no.addEventListener(MouseEvent.CLICK, replacemenufunctions)
        no.addEventListener(MouseEvent.CLICK, yesnofadeout)
        no.removeEventListener(Event.ENTER_FRAME, fadeinno);
    } else {
        no.alpha +=0.1;
    }
}
function yesnofadeout(e:MouseEvent):void
{
    yes.removeEventListener(MouseEvent.CLICK, exit)
    yes.removeEventListener(MouseEvent.CLICK, createnewgame)
    yes.addEventListener(Event.ENTER_FRAME, fadeoutyes)
    no.addEventListener(Event.ENTER_FRAME, fadeoutno)
    no.removeEventListener(MouseEvent.CLICK, yesnofadeout)
}
function fadeoutyes(e:Event=null){
if(yes.alpha <=0){
        yes.removeEventListener(Event.ENTER_FRAME, fadeoutyes);
    } else {
        yes.alpha -=0.1;
    }
}
function fadeoutno(e:Event=null){
if(no.alpha <=0){
        no.removeEventListener(Event.ENTER_FRAME, fadeoutno);
    } else {
        no.alpha -=0.1;
    }
}
function removemenufunctions():void
{
    exitgame.removeEventListener(MouseEvent.CLICK, exitgameclicked);
    loadgame.removeEventListener(MouseEvent.CLICK, loadgameclicked);
    newgame.removeEventListener(MouseEvent.CLICK, newgameclicked);
}
function replacemenufunctions(e:Event=null):void
{
    exitgame.addEventListener(MouseEvent.CLICK, exitgameclicked);
    loadgame.addEventListener(MouseEvent.CLICK, loadgameclicked);
    newgame.addEventListener(MouseEvent.CLICK, newgameclicked);
}
function exit(e:MouseEvent)
{
    MenuExitFade.x = -35; 
    MenuExitFade.y = 0; 
    MenuExitFade.addEventListener(Event.ENTER_FRAME, exitblackfade);
}
function exitblackfade(e:Event)
{
    if(MenuExitFade.alpha >= 1){
        MenuExitFade.removeEventListener(Event.ENTER_FRAME, exitblackfade);
        QuitTimer.addEventListener(TimerEvent.TIMER_COMPLETE,forceclose);
        QuitTimer.reset()
        QuitTimer.start();
    } else {
        MenuExitFade.alpha +=0.1;
    }
}
function forceclose(e:Event)
{
    QuitTimer.stop();
    QuitTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,forceclose);
    fscommand('quit');
}
function createnewgame(e:MouseEvent)
{
    var GameMode=1;
    var Planet=1;
    MenuSpawner.alpha=0
    newgame.alpha=0
    loadgame.alpha=0
    exitgame.alpha=0
    voidlogomenu.alpha=0
    yes.alpha=0
    no.alpha=0
    exitprompt.alpha=0
    SwsLogoCorner.alpha=0
    yes.removeEventListener(MouseEvent.CLICK, createnewgame)
    no.removeEventListener(MouseEvent.CLICK, yesnofadeout)
}

第二个代码如下

import flash.events.Event;
import flash.system.fscommand;
import flash.events.MouseEvent;
setupplanet();
Foreground.alpha=0
function setupplanet():void
{
    Foreground.addEventListener(Event.ENTER_FRAME, PlanetLoop)
}
function PlanetLoop(e:Event=null):void
{
    if(GameMode==0){
        Foreground.alpha=0
    } else {
        Foreground.alpha=1
    }
            Foreground.gotoAndStop(1)
}

谢谢...

问题出在这里:

function createnewgame(e:MouseEvent)
{
    var GameMode=1;

你希望这个变量是"global",但你将它声明为一个函数局部变量,因此在函数执行时它不存在于函数内部以外的任何地方。

至少应该是:

// Declared in the frame, should be visible
// to ALL frame functions on the same timeline.
var GameMode;

function createnewgame(e:MouseEvent)
{
    GameMode=1;