[FONT=Verdana]I am trying to create a game where insects coming from the Y direction are racing to go down the screen. The point of this game is that the user has to click on the insects for them to die, if the insects go off the stage then the player’s lives decrease. I have created the on click function inside the insect, everything works perfect. I have also made a score board for the player for when he/she clicks on the insect. The score updates perfect. The problem I am facing now is reducing the amount of lives every time the insects go off the stage. The lives visual is a text field. The player has 3 lives by default.
[/FONT]
if (tempEnemy.y > stage.stageHeight) {
removeEnemy(i);
lives--;
lives_txt.text = String(lives);
}
}
}
function removeEnemy(idx:int)
{
removeChild(enemies[idx]);
enemies.splice(idx,1);
}
[FONT=Verdana]this is the function that I have created. The insects are coming off the stage in the Y direction. It works perfect when I test run it, but then when one insect comes off and decreases the lives to 2, the game slows down and an error message shows saying: [/FONT]
[FONT=Verdana]ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.[/FONT]
[FONT=Verdana]at flash.display:isplayObjectContainer/removeChild()[/FONT]
[FONT=Verdana]I don’t understand what it’s saying because the game works perfect, I don’t see no error![/FONT]
[FONT=Verdana]The code on the main timeline is just the button codes for the intro and levels screen. The code on the “roach level” is in the main timeline, but in a different layer. [/FONT]
[FONT=Verdana]This is the whole code for reference:
[/FONT]
import flash.events.MouseEvent;
var STATE_INIT_GAME:String = "STATE_INIT_GAME";
var STATE_PLAY_GAME:String = "STATE_PLAY_GAME";
var gameState:String;
var enemies:Array;
var level:Number;
var score:Number;
var lives:Number;
var tempEnemy:MovieClip;
score = 0;
score_txt.text = String(score);
function gameLoop(e:Event):void
{
//Keep track of the gameState
switch (gameState)
{
case STATE_INIT_GAME :
initGame();
break;
case STATE_PLAY_GAME :
playGame();
break;
}
}
function initGame():void
{
level = 1;
level_txt.text = String(level);
lives = 3;
lives_txt.text = String(lives);
//Create an enemies array (a big box that can hold many things)
enemies = new Array();
gameState = STATE_PLAY_GAME;
trace(gameState);
}
function playGame():void
{
//Set up the enemies, explosions etc.
makeEnemies();
moveEnemies();
testForEnd();
}
function makeEnemies():void
{
var chance:Number = Math.floor(Math.random() * 150);
if (chance <= + level)
{
//Make sure a Library item linkage is set to Enemy...
tempEnemy = new Enemy();
tempEnemy.speed = 2
//Math.random(); gets a random number from 0.0-1.0
tempEnemy.x = Math.round(Math.random() * 800);
addChild(tempEnemy);
enemies.push(tempEnemy);
if (level == 2)
{
tempEnemy.speed = 3
}
if (level == 3)
{
tempEnemy.speed = 4
}
if (level == 4)
{
tempEnemy.speed = 5
}
if (level == 5)
{
tempEnemy.speed = 6
}
if (level == 6)
{
tempEnemy.speed = 7
}
if (level == 7)
{
tempEnemy.speed = 8
}
}
}
function moveEnemies():void
{
var tempEnemy:MovieClip;
for (var i:int =enemies.length-1; i>=0; i--)
{
tempEnemy = enemies*;
//rotate the enemy between 10-5 degrees
tempEnemy.rotation += (Math.round(Math.random()*10-5));
//Find the rotation and move the x position that direction
tempEnemy.x -= (Math.sin((Math.PI/180)*tempEnemy.rotation))*tempEnemy.speed;
tempEnemy.y += (Math.cos((Math.PI/180)*tempEnemy.rotation))*tempEnemy.speed;
if (tempEnemy.x < 0)
{
tempEnemy.x = 0;
}
if (tempEnemy.x > stage.stageWidth)
{
tempEnemy.x = stage.stageWidth;
}
if (tempEnemy.y > stage.stageHeight)
{
removeEnemy(i);
lives--;
lives_txt.text = String(lives);
}
}
}
function testForEnd():void
{
//
if (score > level * 10)
{
level++;
level_txt.text = String(level);
}
function removeEnemy(idx:int)
{
removeChild(enemies[idx]);
enemies.splice(idx,1);
}
}
[FONT=Verdana]
[/FONT]