显示超过对象/影片剪辑主菜单? [英] Showing main menu over objects / movieclips?

查看:201
本文介绍了显示超过对象/影片剪辑主菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个游戏,Flash和我创建一个主菜单的游戏(有了这样的播放,怎么玩,Hiscores等按钮),并想知道什么是最好的方式去关于它?

I am creating a game in Flash and I am creating a main menu for the game (With buttons like 'Play', 'How to Play', 'Hiscores' etc.) and was wondering what is the best way to go about it?

我的所有的ActionScript code是外部.as文件,我已经使用的类在整个但我无法搞清楚如何得到它,这样的菜单将尽快在游戏运行显示。主要的问题是,有有事件处理程序连接到他们,我试图想从根本上阻止这些计时器,直到用户实际点击播放最好的办法在我的游戏计时器,否则对象产卵在顶部菜单和计时器滴答下来。

All of my Actionscript code is in external .as files and I've used classes throughout but I was having trouble figuring out how to get it so that the menu will be shown as soon as the game is ran. The main problem is that there are timers in my game that have event handlers attached to them and I was trying to think of the best way to essentially stop these timers until the user actually clicks 'Play', otherwise the objects spawn over the top of the menu and the timer ticks down.

可否停止定时器,但再加入一个事件处理程序来播放按钮启动定时器是一个好主意?我试图找出这样做以供将来参考的最好方法。

Would stopping the timers but then adding an event handler to the play button to start the timers be a good idea? I am trying to figure out the best way to do this for future reference.

感谢您的任何援助。

编辑:尝试Cherniv的建议,得到了一些错误

Tried Cherniv's advice, getting some errors.

Main.as:

package  {
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.display.DisplayObject;
    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;
    import flash.display.Sprite;
    import flash.net.Socket;


    public class Main extends MovieClip {

    public static var gameLayer:Sprite = new Sprite;
    public static var endGameLayer:Sprite = new Sprite;
    public static var menuLayer:Sprite = new Sprite;

    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;

    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()
    {
        var mainMenu:myMenu = new myMenu;
        addChild(gameLayer);
        addChild(endGameLayer);
        addChild(menuLayer);

        playBtn.addEventListener(MouseEvent.CLICK, startButtonPressed);
    }

    function startButtonPressed(e:Event)
    {
        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: ");
        gameLayer.addChild(scoreHeader);

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

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

        timeText = new TextField();
        timeText.x = 550;
        timeText.y = 0;
        timeText.text = gameTime.toString();
        gameLayer.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();
        gameLayer.addChild(crosshair);

        crosshair.mouseEnabled = crosshair.mouseChildren = false;

        Mouse.hide();

        gameLayer.addEventListener(Event.ENTER_FRAME, moveCursor);
        resetScore();
    }

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

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

    function sendEnemySmall(e:Event)
    {
        var enemysmall = new EnemyShipSmall();
        gameLayer.addChild(enemysmall);
        gameLayer.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;
    }
  }
}

Menu.as:

Menu.as:

package  {
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.display.DisplayObject;
    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;
    import flash.display.Sprite;
    import flash.net.Socket;

    public class Menu extends MovieClip
    {

    var mainMenu:Menu = new Menu();

    Main.menuLayer.addChild(myMenu);

    playBtn.addEventListener(MouseEvent.CLICK, playBtnPressed);

    function playBtnPressed()
    {
        Main.menuLayer.removeChild(myMenu);
        dispatchEvent(new Event("playButtonPressed"))
    }
    }

我的菜单被命名为 MYMENU 一个影片剪辑和类被设置为菜单,但我得到的错误,如:

My menu is a movieclip named myMenu and the class is set as Menu but I get errors such as:

Main.as, Line 50 1120: Access of undefined property playBtn

我把按钮playBtn实例名称之前,我转换菜单到MovieClip所以不知道这是怎么回事那里。我可能失去了一些东西真的很容易,但输入了一整天后,这是一个有点混乱给我。

I gave the button an instance name of playBtn before I converted the menu to a movieclip so not sure what's going on there. I'm probably missing something really easy but it's a bit confusing for me after typing all day.

推荐答案

如果您有您的所有初始化的东西(包括定时器初始化)的主要构造函数,所以你需要将其拆分为两个函数,主要构造函数将显示菜单和开始按钮,将触发第二个功能,这将包括所有的初始化东西

If you have all of your initialization stuff (including timers initializations) in Main constructor function , so you need to split it to two functions , Main constructor will show the menu , and "start" button will fire the second function , that will include all the initialization stuff

这篇关于显示超过对象/影片剪辑主菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆