0

I am trying to update my score only when hitting on one specific object, out of 4 types of different object spawned. Code that I have made updateScore when all objects are hit. I just cant figure out on how to update score only when it hit 1 type only

heres the code:

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();
            }
        }
    }
}

Heres the external class of the different objects spawned: Note that the each different object are on each different frame

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;
    }
  • This **currentBullet2.hitTestObject(currentRandom == (nRandom == 1))** is wrong, it bears no sense. Please explain what did you try to do here? – Organis Oct 13 '21 at 08:32
  • **(1)** Do you actually have a `destroyRandom()` function in your `mcRandom` class? If yes, then you can see that if possible to access functions inside the class itself that means you can also access its variables. So make a variable (in class mcRandom) like example `public var typeRandom : int = ???` and read it in main class `if ( currentRandom.typeRandom == someNumber ){ updateScoreHere }` ... **(2)** If this was my problem I would make `nRandom` a **public var** and also move it out of a function (to make it global). The `init` function should just _update_ not _create_ the variable. – VC.One Oct 13 '21 at 08:33
  • PS... `.hitTestObject` will return **true** or **false**. Try it as this logic: `if( ( myThing.hitTestObject ( someThing) ) == true) { doExpectedResult }` because your code like `hitTestObject(currentRandom == (nRandom == 1))` is likely not doing what you think it should... – VC.One Oct 13 '21 at 20:24
  • In short you have there a link to the enemy that got hit, you can check its type with whatever things you use to determine the characteristics. And if checks pass, you update score, else you can do something other. – Vesper Oct 14 '21 at 07:50
  • @VC.One thank you so much! Finally I managed to get it working – Mursyid Nasir Oct 16 '21 at 13:45

0 Answers0