如何将actionscript3.0中的对象计时器和分数对齐到中心

发布于 2024-11-07 06:56:12 字数 6861 浏览 5 评论 0原文

我正在做一个匹配游戏。使用adobeFlash,actionScript3.0。我不知道如何在游戏开始时将计时器 n 分数对齐到中心.. n 在哪里插入这些编码..

package {
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;
    import flash.display.StageAlign;
    import flash.utils.getTimer;
    import flash.utils.Timer;
    import flash.media.Sound;
    import flash.media.SoundChannel;

    public class MatchingGameObject10 extends MovieClip {
        // game constants
        private static const boardWidth:uint = 8;
        private static const boardHeight:uint = 8;
        private static const cardHorizontalSpacing:Number = 52;
        private static const cardVerticalSpacing:Number = 52;
        private static const boardOffsetX:Number = 300;
        private static const boardOffsetY:Number = 200;
        private static const pointsForMatch:int = 100;
        private static const pointsForMiss:int = -5;

        // variables
        private var firstCard:Card10;
        private var secondCard:Card10;
        private var cardsLeft:uint;
        private var gameScore:int;
        private var gameStartTime:uint;
        private var gameTime:uint;


        // text fields
        private var gameScoreField:TextField;
        private var gameTimeField:TextField;

        // timer to return cards to face-down
        private var flipBackTimer:Timer;

        // set up sounds
        var theFirstCardSound:FirstCardSound = new FirstCardSound();
        var theMissSound:MissSound = new MissSound();
        var theMatchSound:MatchSound = new MatchSound();

        // initialization function
        public function MatchingGameObject10():void {
            // make a list of card numbers
            var cardlist:Array = new Array();
            for(var i:uint=0;i<boardWidth*boardHeight/2;i++) {
                cardlist.push(i);
                cardlist.push(i);
            }

            // create all the cards, position them, and assign a randomcard face to each
            cardsLeft = 0;
            for(var x:uint=0;x<boardWidth;x++) { // horizontal
                for(var y:uint=0;y<boardHeight;y++) { // vertical
                    var c:Card10 = new Card10(); // copy the movie clip
                    c.stop(); // stop on first frame
                    c.x = x*cardHorizontalSpacing+boardOffsetX; // set position
                    c.y = y*cardVerticalSpacing+boardOffsetY;
                    var r:uint = Math.floor(Math.random()*cardlist.length); // get a random face
                    c.cardface = cardlist[r]; // assign face to card
                    cardlist.splice(r,1); // remove face from list
                    c.addEventListener(MouseEvent.CLICK,clickCard); // have it listen for clicks
                    c.buttonMode = true;
                    addChild(c); // show the card
                    cardsLeft++;
                }
            }

            // set up the score
            gameScoreField = new TextField();
            //gameScoreField = center;
            ////gameScoreField = center;
            addChild(gameScoreField);
            gameScore = 0;
            showGameScore();

            // set up the clock
            gameTimeField = new TextField();
            gameTimeField.x = 450;

            //gameTimeField = center;
            ////gameTimeField.stage = StageAlign.TOP;
            addChild(gameTimeField);
            gameStartTime = getTimer();
            gameTime = 0;
            addEventListener(Event.ENTER_FRAME,showTime);
        }

        // player clicked on a card
        public function clickCard(event:MouseEvent) {
            var thisCard:Card10 = (event.target as Card10); // what card?

            if (firstCard == null) { // first card in a pair
                firstCard = thisCard; // note it
                thisCard.startFlip(thisCard.cardface+2);
                playSound(theFirstCardSound);

            } else if (firstCard == thisCard) { // clicked first card again
                firstCard.startFlip(1);
                firstCard = null;
                playSound(theMissSound);

            } else if (secondCard == null) { // second card in a pair
                secondCard = thisCard; // note it
                thisCard.startFlip(thisCard.cardface+2);

                // compare two cards
                if (firstCard.cardface == secondCard.cardface) {
                    // remove a match
                    removeChild(firstCard);
                    removeChild(secondCard);
                    // reset selection
                    firstCard = null;
                    secondCard = null;
                    // add points
                    gameScore += pointsForMatch;
                    showGameScore();
                    playSound(theMatchSound);
                    // check for game over
                    cardsLeft -= 2; // 2 less cards
                    if (cardsLeft == 0) {
                        MovieClip(root).gameScore = gameScore;
                        MovieClip(root).gameTime = clockTime(gameTime);
                        MovieClip(root).gotoAndStop("gameover");
                    }
                } else {
                    gameScore += pointsForMiss;
                    showGameScore();
                    playSound(theMissSound);
                    flipBackTimer = new Timer(2000,1);
                    flipBackTimer.addEventListener(TimerEvent.TIMER_COMPLETE,returnCards);
                    flipBackTimer.start();
                }

            } else { // starting to pick another pair
                returnCards(null);
                playSound(theFirstCardSound);
                // select first card in next pair
                firstCard = thisCard;
                firstCard.startFlip(thisCard.cardface+2);
            }
        }

        // return cards to face-down
        public function returnCards(event:TimerEvent) {
            firstCard.startFlip(1);
            secondCard.startFlip(1);
            firstCard = null;
            secondCard = null;
            flipBackTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,returnCards);
        }

        public function showGameScore() {
            gameScoreField.text = "Score: "+String(gameScore);
        }

        public function showTime(event:Event) {
            gameTime = getTimer()-gameStartTime;
            gameTimeField.text = "Time: "+clockTime(gameTime);
        }

        public function clockTime(ms:int) {
            var seconds:int = Math.floor(ms/1000);
            var minutes:int = Math.floor(seconds/60);
            seconds -= minutes*60;
            var timeString:String = minutes+":"+String(seconds+100).substr(1,2);
            return timeString;
        }

        public function playSound(soundObject:Object) {
            var channel:SoundChannel = soundObject.play();
        }
    }
}

I'm doing a matching game.using adobeFlash, actionScript3.0. I have no idea how to align the Timer n Score to the center when game is on.. n where to insert those coding..

package {
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;
    import flash.display.StageAlign;
    import flash.utils.getTimer;
    import flash.utils.Timer;
    import flash.media.Sound;
    import flash.media.SoundChannel;

    public class MatchingGameObject10 extends MovieClip {
        // game constants
        private static const boardWidth:uint = 8;
        private static const boardHeight:uint = 8;
        private static const cardHorizontalSpacing:Number = 52;
        private static const cardVerticalSpacing:Number = 52;
        private static const boardOffsetX:Number = 300;
        private static const boardOffsetY:Number = 200;
        private static const pointsForMatch:int = 100;
        private static const pointsForMiss:int = -5;

        // variables
        private var firstCard:Card10;
        private var secondCard:Card10;
        private var cardsLeft:uint;
        private var gameScore:int;
        private var gameStartTime:uint;
        private var gameTime:uint;


        // text fields
        private var gameScoreField:TextField;
        private var gameTimeField:TextField;

        // timer to return cards to face-down
        private var flipBackTimer:Timer;

        // set up sounds
        var theFirstCardSound:FirstCardSound = new FirstCardSound();
        var theMissSound:MissSound = new MissSound();
        var theMatchSound:MatchSound = new MatchSound();

        // initialization function
        public function MatchingGameObject10():void {
            // make a list of card numbers
            var cardlist:Array = new Array();
            for(var i:uint=0;i<boardWidth*boardHeight/2;i++) {
                cardlist.push(i);
                cardlist.push(i);
            }

            // create all the cards, position them, and assign a randomcard face to each
            cardsLeft = 0;
            for(var x:uint=0;x<boardWidth;x++) { // horizontal
                for(var y:uint=0;y<boardHeight;y++) { // vertical
                    var c:Card10 = new Card10(); // copy the movie clip
                    c.stop(); // stop on first frame
                    c.x = x*cardHorizontalSpacing+boardOffsetX; // set position
                    c.y = y*cardVerticalSpacing+boardOffsetY;
                    var r:uint = Math.floor(Math.random()*cardlist.length); // get a random face
                    c.cardface = cardlist[r]; // assign face to card
                    cardlist.splice(r,1); // remove face from list
                    c.addEventListener(MouseEvent.CLICK,clickCard); // have it listen for clicks
                    c.buttonMode = true;
                    addChild(c); // show the card
                    cardsLeft++;
                }
            }

            // set up the score
            gameScoreField = new TextField();
            //gameScoreField = center;
            ////gameScoreField = center;
            addChild(gameScoreField);
            gameScore = 0;
            showGameScore();

            // set up the clock
            gameTimeField = new TextField();
            gameTimeField.x = 450;

            //gameTimeField = center;
            ////gameTimeField.stage = StageAlign.TOP;
            addChild(gameTimeField);
            gameStartTime = getTimer();
            gameTime = 0;
            addEventListener(Event.ENTER_FRAME,showTime);
        }

        // player clicked on a card
        public function clickCard(event:MouseEvent) {
            var thisCard:Card10 = (event.target as Card10); // what card?

            if (firstCard == null) { // first card in a pair
                firstCard = thisCard; // note it
                thisCard.startFlip(thisCard.cardface+2);
                playSound(theFirstCardSound);

            } else if (firstCard == thisCard) { // clicked first card again
                firstCard.startFlip(1);
                firstCard = null;
                playSound(theMissSound);

            } else if (secondCard == null) { // second card in a pair
                secondCard = thisCard; // note it
                thisCard.startFlip(thisCard.cardface+2);

                // compare two cards
                if (firstCard.cardface == secondCard.cardface) {
                    // remove a match
                    removeChild(firstCard);
                    removeChild(secondCard);
                    // reset selection
                    firstCard = null;
                    secondCard = null;
                    // add points
                    gameScore += pointsForMatch;
                    showGameScore();
                    playSound(theMatchSound);
                    // check for game over
                    cardsLeft -= 2; // 2 less cards
                    if (cardsLeft == 0) {
                        MovieClip(root).gameScore = gameScore;
                        MovieClip(root).gameTime = clockTime(gameTime);
                        MovieClip(root).gotoAndStop("gameover");
                    }
                } else {
                    gameScore += pointsForMiss;
                    showGameScore();
                    playSound(theMissSound);
                    flipBackTimer = new Timer(2000,1);
                    flipBackTimer.addEventListener(TimerEvent.TIMER_COMPLETE,returnCards);
                    flipBackTimer.start();
                }

            } else { // starting to pick another pair
                returnCards(null);
                playSound(theFirstCardSound);
                // select first card in next pair
                firstCard = thisCard;
                firstCard.startFlip(thisCard.cardface+2);
            }
        }

        // return cards to face-down
        public function returnCards(event:TimerEvent) {
            firstCard.startFlip(1);
            secondCard.startFlip(1);
            firstCard = null;
            secondCard = null;
            flipBackTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,returnCards);
        }

        public function showGameScore() {
            gameScoreField.text = "Score: "+String(gameScore);
        }

        public function showTime(event:Event) {
            gameTime = getTimer()-gameStartTime;
            gameTimeField.text = "Time: "+clockTime(gameTime);
        }

        public function clockTime(ms:int) {
            var seconds:int = Math.floor(ms/1000);
            var minutes:int = Math.floor(seconds/60);
            seconds -= minutes*60;
            var timeString:String = minutes+":"+String(seconds+100).substr(1,2);
            return timeString;
        }

        public function playSound(soundObject:Object) {
            var channel:SoundChannel = soundObject.play();
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ま柒月 2024-11-14 06:56:12

当您想要(水平)对齐 DisplayObject 与其父 DisplayObject 时,只需执行以下操作:

child.x = (parent.width - child.width) / 2; 

在您的代码示例中,这将是类似的内容

gameTimeField.x = (this.width - gameTimeField.width) / 2;

。当子 DisplayObject 完全绘制时,应插入此内容,因此其宽度已知。

When you want to (horizontal) align a DisplayObject in its parent DisplayObject, just do something like this:

child.x = (parent.width - child.width) / 2; 

In your code example this would be something like

gameTimeField.x = (this.width - gameTimeField.width) / 2;

This should be inserted, when the child DisplayObject is completely painted, so its width is known.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文