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