0

我正在创建 Flash 游戏,Idea 是从天上掉下来的物体(在这种情况下是苹果),玩家需要点击苹果,点击苹果后它必须被删除并且分数更新 10 分。

这是我生成苹果的部分代码:

  public function startGame()
            {
                speed = C.PLAYER_SPEED;
                gravity = C.GRAVITY;
                score = C.PLAYER_START_SCORE;
                randomChance = C.APPLE_SPAWN_CHANCE;
                apples = new Array();

                mcGameStage.addEventListener(Event.ENTER_FRAME,update);

            }       

            private function update(evt:Event)
            {

                //Spawn new apples
                if (Math.random() < randomChance)
                {
                    var newApple = new Apple();
                    newApple.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;

                    newApple.y = C.APPLE_START_Y;
                    apples.push(newApple);

                    mcGameStage.addChildAt(newApple,0);
                }           
                //Move Apples
                for (var i = apples.length-1; i >= 0; i--)
                {
                    apples[i].y += gravity;

                    if (apples[i].y > C.APPLE_END_Y)
                    {
                        mcGameStage.removeChild(apples[i]);
                        apples.splice(i,1);
                    }
                }           


                //txtScore.text = String(score);
            }

        }

这里是应该通过鼠标点击删除苹果的代码。但它不起作用,我没有任何错误,只是没有删除苹果。请问你能帮帮我吗?

function onClick(evt:MouseEvent):void{
            var apples = evt.target;

                for (var iz = apples.length-1; iz >= 0; iz--)
                {
                        //Register hit
                        score += C.SCORE_PER_APPLE;                 
                        mcGameStage.removeChild(apples[iz]);                
                        apples.splice(iz,1);        
                }
            }

更新

所以我的代码现在看起来像:

        public function startGame()
        {
            speed = C.PLAYER_SPEED;
            gravity = C.GRAVITY;
            score = C.PLAYER_START_SCORE;
            randomChance = C.APPLE_SPAWN_CHANCE;
            apples = new Array();

            mcGameStage.addEventListener(Event.ENTER_FRAME,update);

        }       

        private function update(evt:Event)
        {

            //Spawn new apples
            if (Math.random() < randomChance)
            {
                var newApple = new Apple();
                newApple.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;

                newApple.y = C.APPLE_START_Y;
                apples.push(newApple);
                newApple.addEventListener(MouseEvent.CLICK, onClick);

                mcGameStage.addChildAt(newApple,0);
            }           
            //Move Apples
            for (var i = apples.length-1; i >= 0; i--)
            {
                apples[i].y += gravity;

                if (apples[i].y > C.APPLE_END_Y)
                {
                    mcGameStage.removeChild(apples[i]);

                    apples.splice(i,1);
                }
            }           


            txtScore.text = String(score);
        }
        function onClick(evt:MouseEvent):void{
            var apples = evt.target;
        apples.visible = false;
        //mcGameStage.removeChild(apples);      
        score += C.SCORE_PER_APPLE; 

        }
    }
}

我改用这个apples.visible = false;mcGameStage.removeChild(apples);没关系。只是我误解了为什么我的分数没有更新?总是显示0。有时我不能通过1点击设置苹果不可见如果点击苹果顶部没有任何反应,我需要点击苹果的中心来隐藏它,为什么我有这个问题?

4

3 回答 3

1

如果您认真对待游戏开发,则应该考虑代码膨胀问题。在您的情况下,您不需要任何apples数组 - 您的显示列表(由 Flash Player 提供)已经能够将苹果包含在排序的链表结构中 - 对于您打算使用的类型来说效率更高 - 删除和添加苹果. 如果您将容器精灵专门用于包含苹果,您将获得两个好处:1)您可以获得免费的苹果显示列表和 2)如果您为此精灵(仅包含苹果)注册鼠标单击事件侦听器,那么每次单击在苹果上,event.target会反映出来。

仔细检查修改后的代码:

public function startGame()
{
    speed = C.PLAYER_SPEED;
    gravity = C.GRAVITY;
    score = C.PLAYER_START_SCORE;
    randomChance = C.APPLE_SPAWN_CHANCE;

    mcApples = new Sprite();
    mcApples.addEventListener(MouseEvent.CLICK, onClickApple);

    mcGameStage.addChild(mcApples);

    mcGameStage.addEventListener(Event.ENTER_FRAME, update);
}

private function update(evt:Event)
{
    //Spawn new apple
    if(Math.random() < randomChance)
    {
        var newApple = new Apple();

        newApple.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
        newApple.y = C.APPLE_START_Y;

        mcApples.addChild(newApple);
    }

    //Move Apples
    for (var i = 0; i < mcApples.numChildren;)
    {
        var apple = mcApples.getChildAt(i);

        apple.y += gravity;

        if(apple.y > C.APPLE_END_Y)
        {
            apple.parent.removeChild(apple);
            continue;
        }

        i++;
    }
}

function onClickApple(event: MouseEvent):void
{
    var apple : Apple = event.target;

    apple.parent.removeChild(apple);

    score += C.SCORE_PER_APPLE;
}

因此,重要的变化及其影响如下:

  1. mcApples添加和删​​除苹果的专用精灵。
  2. 不需要一系列苹果,Flash Player 会跟踪mcApples容器中的苹果。
  3. 鼠标点击监听器设置保证任何点击都在苹果上。

因此,不需要拼接数组,也不需要在Apple类中显式引用函数或为每个新苹果添加事件监听器。

作为额外的建议,您应该小心地将逻辑绑定到enter frame事件 - 帧速率越快,调用频率越快 - 通常没问题,但有时不是您想要的。我会将update调用绑定到计时器事件;-)

于 2013-05-23T15:17:33.127 回答
0
        private function update(evt:Event)
        {

            //Spawn new apples
            if (Math.random() < randomChance)
            {
                var newApple = new Apple(appleClick);
                newApple.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;

                newApple.y = C.APPLE_START_Y;
                apples.push(newApple);
                mcGameStage.addChildAt(newApple,0);
            }           
            //Move Apples
            for (var i = apples.length-1; i >= 0; i--)
            {
                apples[i].y += gravity;

                if (apples[i].y > C.APPLE_END_Y)
                {
                    mcGameStage.removeChild(apples[i]);
                    apples[iz].destruct();
                    apples.splice(i,1);
                }
            }           


            //txtScore.text = String(score);
        }

        function appleClick(pApple:Apple):void{
            for (var iz = apples.length-1; iz >= 0; iz--)
            {
                    if (apples[iz] == pApple)
                    {
                        //Register hit
                        score += C.SCORE_PER_APPLE;                 
                        mcGameStage.removeChild(apples[iz]);
                        apples[iz].destruct();
                        apples.splice(iz,1);
                        break;
                    }
            }
        }

……

public class Apple extends Group or something
{
    private var m_fCallback:Function;

    public function Apple(pCallback:Function):void
    {
        addEventListener(MouseEvent.CLICK, onClick);
        m_fCallback = pCallback;
    }

    private function destruct():void
    {
        removeEventListener(MouseEvent.CLICK, onClick);
    }

    private function onClick(pEvent:MouseEvent):void
    {
        m_fCallback(this);
    }
}
于 2013-05-23T14:22:21.740 回答
0

我没有看到为onClick()添加了任何事件侦听器。请务必检查您的事件侦听器是否已添加,并在onClick()函数中添加跟踪或警报,以便您可以确认代码已实际到达。

于 2013-05-23T14:22:48.470 回答