Actionscript3:鸭子的倒计时数

时间:2012-04-26 19:39:48

标签: actionscript-3

  

云,   鸭,   得分了   显示   和   波浪   应该   每   有   一个   类   至   治理   其   运动   和   行为。

     

当   鸭子   是   点击   上   他们   是   “射击”   和   该   鸭   是   去除   从   该   排列   如   好   如   从   该   阶段   (使用   arrayName.splice()   对于   这个)。   该   得分了   显示   应该   计数   下   如   这个   发生。

     

在   数   的   鸭子   剩下   应该   是   一个   属性   内   该   得分了   显示的   类   和   调整   通过   主要   什么时候   该   鸭子   是   镜头。

     

当   所有   该   鸭子   是   “射击”   该   游戏   应该   活跃   该   “您   赢得”   信息。   这个   能够   是   DONE   通过   加入   和   去除   事件   听众   那   关联   一个   输入   帧   事件   同   一个   动画   功能。   (这个   是   价值   只要,   所以   离开   它   对于   最后一个)。

     

当   该   鸭子   是   “射击”   该   波浪   和   云   应该   也   是   去除   从   视图   和   从   其   各自   阵列。

     

游戏   应该   重启   后   播放机   具有   韩元   要么   丢失   许多   倍。   (不   只是   一次)

我完成了大部分工作,我只是在记分牌上遇到了麻烦。任何有关如何重置所有内容以及编写胜利标志的提示都会有所帮助。

import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;

[SWF(width="800", height="600", backgroundColor="#E6FCFF")]

public class Main extends Sprite
{
    private var _sittingDucks:Array = []; //always set your arrays with [] at the top
    public var _scoreDisplay:TextField


    public function Main()
    {
        //adding the background, and positioning it
        var background:Background = new Background();
        this.addChild(background);
        background.x = 30;
        background.y = 100;

        for(var i:uint = 0; i < 5; i++)
        {
            //adding the first cloud, and positioning it
            var clouds:Clouds = new Clouds();
            this.addChild(clouds);
            clouds.x = 130 + Math.random() * 600; //130 to 730
            clouds.y = 230;
            clouds.speedX = Math.random() * 3;
            clouds.width = clouds.height = 200 * Math.random()//randomly changes the clouds demensions
        }   

        var waves:Waves = new Waves();
        this.addChild(waves);
        waves.x = 0;
        waves.y = 510;
        waves.speedX = Math.random() * 3;


        for(var j:uint = 0; j < 8; j++)
        {
            var ducks:Ducks = new Ducks();
            this.addChild(ducks);
            ducks.x = 100 + j * 100;
            ducks.y = 475;
            _sittingDucks.push(ducks);
            ducks.addEventListener(MouseEvent.CLICK, ducksDestroy);
        }

        var waves2:Waves = new Waves();
        this.addChild(waves2);
        waves2.x = 0;
        waves2.y = 520;
        waves2.speedX = Math.random() * 3;

        var setting:ForeGround = new ForeGround();
        this.addChild(setting);
        setting.x = 0;
        setting.y = 50;
        setting.width = 920;

        var board:ScoreDisplay = new ScoreDisplay();
        this.addChild(board);
        board.x = 570;
        board.y = 35;

    }
    private function ducksDestroy(event:MouseEvent):void
    {
        //store the crow we clicked on in a new array
        var clickedDuck:Ducks = Ducks(event.currentTarget);

        //remove it from the crows array
        //find the address of the crow we are removing
        var index:uint = _sittingDucks.indexOf(clickedDuck);

        //remove it from the array with splice
        _sittingDucks.splice(index, 1);

        //remove it from my document's display list
        this.removeChild(clickedDuck);
    }
}


import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import ScoreDisplayBase; // always import the classes you are using

public class ScoreDisplay extends ScoreDisplayBase
{
    private var txt:TextField; // where is it initialized?
    private var score:uint = 0;

    public function ScoreDisplay()
    {
        super(); // do you init txt here?
    }

    public function scoreUpdate():void
    {
        score += 10; // ok, so I suppose that your score does not represent the remaining ducks as you said, just only a score
        txt.text = score.toString();
    }
}

1 个答案:

答案 0 :(得分:2)

Aaaalrighty:

  1. 您确实想在ScoreDisplay的构造函数中创建TextField txt。实例化它,将其文本设置为初始分数(0)和addChild(txt)

  2. 为了稍后设置分数,我们需要一种方法来引用显示。

    //you want a reference to the ScoreDisplay, not this
    public var _scoreDisplay:TextField //no
    public var _scoreDisplay:ScoreDisplay //yes
    

    当你在Main构造函数中创建它时,我们需要保留一个引用。

    _scoreDisplay = :ScoreDisplay = new ScoreDisplay();
    this.addChild(_scoreDisplay );
    _scoreDisplay .x = 570;
    _scoreDisplay .y = 35;
    
  3. 如果你想重置游戏,我建议把鸭子创建并放在Main类构造函数之外的方法中。您还应创建一个“重置”功能,在ScoreDisplay中将分数(和显示)设置为0.

    private function spawnDucks() {
        for(var j:uint = 0; j < 8; j++)
        {
            var ducks:Ducks = new Ducks();
            this.addChild(ducks);
            ducks.x = 100 + j * 100;
            ducks.y = 475;
            _sittingDucks.push(ducks);
            ducks.addEventListener(MouseEvent.CLICK, ducksDestroy);
        }
    }
    

    然后你在构造函数中调用它,并且当你需要重置游戏时可以再次调用它。

  4. ducksDestroy(event:MouseEvent)将会重新计算得分,检查您是否赢了,显示消息并重置游戏。您需要显示某种弹出窗口,如果您不知道从哪里开始,那么here是一个不错的选择。

    private function ducksDestroy(event:MouseEvent):void
    {
        //store the crow we clicked on in a new array
        var clickedDuck:Ducks = Ducks(event.currentTarget);
    
        //remove it from the crows array
        //find the address of the crow we are removing
        var index:uint = _sittingDucks.indexOf(clickedDuck);
    
        //remove it from the array with splice
        _sittingDucks.splice(index, 1);
    
        //remove it from my document's display list
        this.removeChild(clickedDuck);
    
        //update the score
        _scoreDisplay.scoreUpdate();
    
        //Check if all the ducks are gone
        if (_sittingDucks.length == 0) {
            //All the ducks are dead, we've won the game!
    
            //create some kind of popup to display.
            //add it to the screen, have some form
            //of button (or a timer) take it away
    
            //whatever takes the popup away, have it call 'reset'
    
        }
    }
    
    private function reset():void
    {
        //write a reset method to clear the score
        _scoreDisplay.reset(); 
    
        //create some ducks and you're ready to go!
        spawnDucks();
    }