首页 > 解决方案 > 子弹击中特定对象时更新分数

问题描述

我试图只在击中一个特定对象时更新我的​​分数,在产生的 4 种不同类型的对象中。当所有对象都被击中时,我制作了 updateScore 的代码。我只是不知道如何仅在仅达到 1 种类型时才更新分数

继承人的代码:

function checkBulletHitsRandom():void
{
    //loop all current bullets
    for(var i:int = 0; i < bulletArray2.length; i++)
    {
        //Get current bullet in the loop
        var currentBullet2:mcBullet = bulletArray2[i];
        
        //loop all current enemies spawned
        for(var j:int = 0;  j < randomArray.length; j++)
        {
            //get current enemy in the loop
            var currentRandom:mcRandom = randomArray[j];
            
            //Test if current bullet hit current enemy
            if(currentBullet2.hitTestObject(currentRandom))
            {
                //Create explosion
                var newExplosion2:mcExplosion = new mcExplosion();
                
                //Add explosion to the stage
                stage.addChild(newExplosion2);
                
                //Position the explosion on hit
                newExplosion2.x = currentRandom.x;
                newExplosion2.y = currentRandom.y;
                
                
                //Remove the bullet from stage
                currentBullet2.destroyBullet();
                
                //Remove the bullet from  array
                bulletArray2.splice(i, 1);
                
                //Remove enemy from stage
                currentRandom.destroyRandom();
                
                //Remove enemy from array
                randomArray.splice(j, 1);
                
                //Update scores
                nScore2 += 2;
                updateScore2();
            }
        }
    }
}

下面是生成的不同对象的外部类:请注意,每个不同的对象都在每个不同的帧上

package  {

import flash.display.MovieClip;
import flash.events.Event;


public class mcRandom extends MovieClip {
    
    public var sDirection:String;
    private var nSpeed:Number;
    
    public function mcRandom()
    {
        addEventListener(Event.ADDED_TO_STAGE, onAdd);
    }
    
    private function onAdd(e:Event):void
    {
        removeEventListener(Event.ADDED_TO_STAGE, onAdd);
        init();
    }
    
    private function init():void
    {
        var nRandomCells:Number = 3;
        
        //Pick random number btw 1 and the number of enemies
        var nRandom:Number = randomNumber(1, nRandomCells);
        
        //Setup playhead of enemy clip to the random number
        this.gotoAndStop(nRandom);
        
        //Setup enemies start position
        setupStartPosition();
    }
    
    private function setupStartPosition():void
    {
        // Pick a random speed for the enemy
        nSpeed  = randomNumber(17, 22);
        // Pick a random number from left/right position
        var nLeftOrRight:Number = randomNumber(1, 2);
        
        // make 1 to start from left
        if(nLeftOrRight == 1)
        {
            //Start enemy moving from left side
            this.x = 0 - this.width / 2;
            sDirection = "R";
        }
    
        else 
        {
            //Start enemy moving from right side
            this.x = stage.stageWidth + (this.width / 2);
            sDirection  = "L";
        }
        // Set random altitude for enemy
        
        // Set min and max altitude for the enemy to spawn
        var nMinAltitude:Number = stage.stageHeight / 2;
        var nMaxAltitude:Number = this.height / 2;
        
        this.y = randomNumber(nMinAltitude, nMaxAltitude);
        
        // Move enemy
        startMoving();
    }
    
    private function startMoving():void
    {
        addEventListener(Event.ENTER_FRAME, randomLoop);
    }
    
    private function randomLoop(e:Event):void
    {
        if(sDirection == "R")
        {
            //Move enemy to the right
            this.x += nSpeed;
        }
        
        else
        {
            //Move enemy to the left
            this.x -= nSpeed; 
        }
    }

function randomNumber(low:Number=0, high:Number=1):Number
    {
        return Math.floor(Math.random() * (1+high-low)) + low;
    }

标签: actionscript-3adobe-animate

解决方案


推荐阅读