重新安排舞台上物体的替代方法?

时间:2013-05-02 18:17:00

标签: actionscript-3 flash flash-cs6

我希望我的菜单在我的游戏打开时显示在其他所有内容之上,但是在那一刻,菜单首先位于顶部,直到我的计时器启动,然后所有其他对象出现在菜单顶部。如何更改它以便菜单位于顶部并且游戏仅开始播放并且只有在用户单击“播放”按钮并且将它们带到游戏中时才启动计时器?

这是我在Main.as文件中的代码。我一直试验无济于事,因为我试图找出最简单或最有效的方法来做到这一点,但我对此感到非常沮丧:

package  {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.ui.Mouse;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.text.TextFormat;
import flash.text.TextField;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.system.LoaderContext;

public class Main extends MovieClip {

public var gameTime:int;
public var levelDuration:int;

    public var crosshair:crosshair_mc;
static var score:Number;

var enemyShipTimer:Timer;
var enemyShipTimerMed:Timer;
var enemyShipTimerSmall:Timer;
var menu:menuMain = new menuMain;

static var scoreHeader:TextField = new TextField();
static var scoreText:TextField = new TextField();
static var timeHeader:TextField = new TextField();
static var timeText:TextField = new TextField();

public function Main()
{
    levelDuration = 30;
    gameTime = levelDuration;
    var gameTimer:Timer = new Timer(1000,levelDuration);
    gameTimer.addEventListener(TimerEvent.TIMER, updateTime);
    gameTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timeExpired)
    gameTimer.start();


    scoreHeader = new TextField();
    scoreHeader.x = 5;
    scoreHeader.text = String("Score: ");
    addChild(scoreHeader);

    scoreText = new TextField();
    scoreText.x = 75;
    scoreText.y = 0;
    scoreText.text = String(0);
    addChild(scoreText);

    timeHeader = new TextField();
    timeHeader.x = 490;
    timeHeader.y = 0;
    timeHeader.text = String("Time: ");
    addChild(timeHeader);

    timeText = new TextField();
    timeText.x = 550;
    timeText.y = 0;
    timeText.text = gameTime.toString();
    addChild(timeText);

    var scoreFormat = new TextFormat("Arial Rounded MT Bold", 20, 0xFFFFFF);
    scoreHeader.setTextFormat(scoreFormat);
    scoreText.setTextFormat(scoreFormat);
    timeHeader.setTextFormat(scoreFormat);
    timeText.setTextFormat(scoreFormat);

    enemyShipTimer = new Timer(2000);
    enemyShipTimer.addEventListener("timer", sendEnemy);
    enemyShipTimer.start();

    enemyShipTimerMed = new Timer(2500);
    enemyShipTimerMed.addEventListener("timer", sendEnemyMed);
    enemyShipTimerMed.start();

    enemyShipTimerSmall = new Timer(2750);
    enemyShipTimerSmall.addEventListener("timer", sendEnemySmall);
    enemyShipTimerSmall.start();

    crosshair = new crosshair_mc();
    addChild(crosshair);

    crosshair.mouseEnabled = crosshair.mouseChildren = false;

    Mouse.hide();

    stage.addEventListener(Event.ENTER_FRAME, moveCursor);
    resetScore();
    showMenu();
}

function showMenu()
{
    stage.addChild(menu);
    Mouse.show();
    enemyShipTimer.stop();
    enemyShipTimerMed.stop();
    enemyShipTimerSmall.stop();
}

function sendEnemy(e:Event)
{
    var enemy = new EnemyShip();
    stage.addChild(enemy);
    stage.addChild(crosshair);
}

function sendEnemyMed(e:Event)
{
    var enemymed = new EnemyShipMed();
    stage.addChild(enemymed);
    stage.addChild(crosshair);
}

function sendEnemySmall(e:Event)
{
    var enemysmall = new EnemyShipSmall();
    stage.addChild(enemysmall);
    stage.addChild(crosshair);
}

static function updateScore(points)
{
    score += points;
    scoreText.text = String(score);
    var scoreFormat = new TextFormat("Arial Rounded MT Bold", 20, 0xFFFFFF);
    scoreHeader.setTextFormat(scoreFormat);
    scoreText.setTextFormat(scoreFormat);
}

static function resetScore()
{
    score = 0;
    scoreText.text = String(score);
}

function updateTime(e:TimerEvent):void
{
    trace(gameTime);
    // your class variable tracking each second, 
    gameTime--;
    //update your user interface as needed
    var scoreFormat = new TextFormat("Arial Rounded MT Bold", 20, 0xFFFFFF);
    timeText.defaultTextFormat = scoreFormat;
    timeText.text = String(gameTime);
}

function timeExpired(e:TimerEvent):void
{
    var gameTimer:Timer = e.target as Timer;
    gameTimer.removeEventListener(TimerEvent.TIMER, updateTime)
    gameTimer.removeEventListener(TimerEvent.TIMER, timeExpired)
    // do whatever you need to do for game over
}

function moveCursor(event:Event) 
{
  crosshair.x=mouseX;
  crosshair.y=mouseY;
}
}
}

我一直在努力解决这个问题,但是我没有很多使用Actionscript的经验。

1 个答案:

答案 0 :(得分:1)

创建两个容器(Sprite或MovieClip),首先添加要在后面的容器,然后在前面添加一个容器。现在,将菜单添加到前面的菜单中,然后将其他菜单添加到后面的菜单中。