Hi,
I’m getting this error
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at Main/groupFishEnterFrame()
in this code
package {
import flash.display.Sprite;
import fl.controls.Button;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.Font;
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import flash.events.MouseEvent;
import flash.media.SoundChannel;
import flash.events.ProgressEvent;
public class Main extends MovieClip{
//for managing questions
private var quizQuestions:Array;
private var currentQuestion:QuizQuestion;
private var currentIndex:int=0;
//the buttons
private var nextButton:nextBtn;
private var continueButton:continueBtn;
private var startQuizButton:startQuizBtn;
private var backMainButton:backMainBtn;
private var instructionsButton:instructionsBtn;
private var startQuizFirstButton:startQuizFirstBtn;
private var startGameButton:startGameBtn;
//the backgrounds
private var quizBackground:quizBkgd;
private var instructionsBackground:instructionsBkgd;
private var answerBackground:answerBkgd;
private var errorField:errorFieldMC;
//scrollbar
//public var my_scrollbar:MakeScrollBar;
//Images For Animation
private var newBubble:bubbleMC= new bubbleMC();
private var bubbleInstance:bubbleMC;
private var vy:Number=-5;
private var groupFish:groupFishMC= new groupFishMC();
private var groupFishLeft:groupFishLeftMC= new groupFishLeftMC()
private var yellowFish:yellowFishMC = new yellowFishMC();
private var yellowFishRight:yellowFishRightMC = new yellowFishRightMC();
private var blueFishLeft:blueFishLeftMC = new blueFishLeftMC();
private var blueFishRight:blueFishRightMC = new blueFishRightMC();
//scoring messages
private var score:int=0;
private var statusBox:TextField;
public var scoreNumber:TextField = new TextField();
public var scoreTime:TextField = new TextField();
//Questions Asked
private var questionsAsked:int=0;
private var questionsAnswered: Array = new Array();
//Game
public var health:Health = new Health;
public var meter:Meter = new Meter;
public var playScreen:Game;
public var secondCounter:SecondCounter;
public var gameOverScreen:GameOverScreen;
public static var instance:Main;
public var time:int;
public var overallTime:Number = 10;
public static var overalltime:Number = 0;
public var enemy:Enemy;
public var avatar:Avatar;
public var food:Food;
public var reminderInst: reminder;
public var game:Game;
public var backgroundMusic:BackgroundMusic;
public var bgmSoundChannel:SoundChannel;//bgm for BackGround Music
public function Main() {
quizQuestions = new Array();
addMainBackgrounds();
addBubbles();
addFish();
addYellowFish();
addBlueFish();
addQuizBackgrounds();
createQuestions();
createButtons();
createStatusBox();
addAllQuestions();
hideAllQuestions();
firstQuestion();
}
private function createQuestions() {
quizQuestions.push(new QuizQuestion("Which animal would be lowest in a food chain?",
4,
"Krill",
"Jellyfish",
"Turtle",
"Plankton"));
quizQuestions.push(new QuizQuestion("What part of a fish keeps them upright?",
1,
"Fins",
"Tail",
"Gills",
"Mouth"));
quizQuestions.push(new QuizQuestion("What statement below best describes a food chain?",
3,
"Where a living thing lives",
"Multiple sets or organisms showing what eats what",
"A single set of living organisms showing what eats what",
"The conditions a living thing lives"));
quizQuestions.push(new QuizQuestion("Which one of the following food chains is incorrect?",
1,
"Plankton -> Starfish -> Scallop -> Flat Fish",
"Plankton -> Krill -> Jellyfish -> Turtle",
"Phytoplankton -> Plankton -> Auklet -> Gull",
"Squid -> Fish -> Seal -> Whale"));
quizQuestions.push(new QuizQuestion("Which of these animals would be highest in a food chain?",
3,
"Squid",
"Seal",
"Shark",
"Krill"));
quizQuestions.push(new QuizQuestion("Which statement best describes a plankton's movement?",
2,
"Smims using five pairs of legs",
"Floats",
"Strong and agile swimmer",
"Claps it's shell together, squirting water so it moves"));
quizQuestions.push(new QuizQuestion("Where would primary producers be in a food chain?",
4,
"Close to the bottom",
"Middle",
"Top",
"Bottom"));
}
private function addMainBackgrounds() {
quizBackground = new quizBkgd();
addChild(quizBackground);
}
private function addQuizBackgrounds() {
instructionsBackground = new instructionsBkgd();
addChild(instructionsBackground);
instructionsBackground.y = stage.stageHeight / 2;
instructionsBackground.x = stage.stageWidth / 2;
instructionsBackground.visible=false;
answerBackground = new answerBkgd();
addChild(answerBackground);
answerBackground.y=stage.stageHeight / 2;
answerBackground.x=stage.stageWidth / 2;
answerBackground.visible=false;
errorField = new errorFieldMC()
addChild(errorField);
errorField.y= 250;
errorField.x=stage.stageWidth/2 - 10 ;
errorField.visible = false;
reminderInst = new reminder;
addChild(reminderInst);
reminderInst.x =20;
reminderInst.y = 50;
reminderInst.visible = false;
backgroundMusic=new BackgroundMusic ;
bgmSoundChannel=backgroundMusic.play();
addEventListener(Event.SOUND_COMPLETE,onBackgroundMusicFinished);
}
public function onBackgroundMusicFinished(event:Event):void {
bgmSoundChannel=backgroundMusic.play();
bgmSoundChannel.addEventListener(Event.SOUND_COMPLETE,onBackgroundMusicFinished);
}
private var bubbleList:Array;
private function addBubbles():void {
bubbleList= new Array();
for (var i:int=0; i<20; i++) {
var newBubble:bubbleMC = new bubbleMC();
addChild(newBubble);
bubbleList.push(newBubble);
newBubble.x=Math.random()*stage.stageWidth;
newBubble.y=Math.random()*stage.stageHeight;
}
// Add only one listener
this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function addFish():void{
for (var j:int=0; j<1; j++){
addChild(groupFish);
groupFish.x=Math.random()*stage.stageWidth;
groupFish.y=Math.random()*stage.stageHeight;
}
this.addEventListener(Event.ENTER_FRAME, groupFishEnterFrame);
}
private function addYellowFish():void{
for (var j:int=0; j<1; j++){
addChild(yellowFish);
yellowFish.x=Math.random()*stage.stageWidth;
yellowFish.y=Math.random()*stage.stageHeight;
}
this.addEventListener(Event.ENTER_FRAME, yellowFishEnterFrame);
}
private function addBlueFish():void{
for (var j:int=0; j<1; j++){
addChild(blueFishRight);
blueFishRight.x=Math.random()*stage.stageWidth;
blueFishRight.y=Math.random()*stage.stageHeight;
}
this.addEventListener(Event.ENTER_FRAME, blueFishRightEnterFrame);
}
private function bringToFront():void{
//setChildIndex(startGameButton, numChildren -1);
setChildIndex(startQuizFirstButton, numChildren -1);
setChildIndex(instructionsButton, numChildren -1);
setChildIndex(instructionsBackground, numChildren -1);
setChildIndex(startQuizButton, numChildren -1);
setChildIndex(backMainButton, numChildren -1);
setChildIndex(answerBackground, numChildren -1);
setChildIndex(startQuizButton, numChildren -1);
setChildIndex(startGameButton, numChildren -1);
setChildIndex(reminderInst, numChildren -1);
setChildIndex(nextButton, numChildren -1);
setChildIndex(continueButton, numChildren -1);
setChildIndex(currentQuestion, numChildren -1);
setChildIndex(errorField, numChildren -1);
}
private function onEnterFrame(event:Event):void {
var objectHalfWidth:uint=newBubble.width/2;
var objectHalfHeight:uint=newBubble.height/2;
// Loop through all instances and change y
for (var i = 0; i < bubbleList.length; i++) {
bubbleInstance=bubbleList*;
bubbleInstance.y+=vy;
//Screen wrapping but random x axis
if (bubbleInstance.y+objectHalfHeight<0) {
bubbleInstance.y=stage.stageHeight+objectHalfHeight;
bubbleInstance.x=Math.random()*stage.stageWidth;
}
}
}
private function groupFishEnterFrame(event:Event):void {
var fishVX:Number = 13;
groupFish.x+=fishVX;
if (groupFish.x > 700) {
for(var a:int=0; a<1; a++){
removeChild(groupFish);
addChild(groupFishLeft);
groupFishLeft.x=750;
groupFishLeft.y=Math.random()*stage.stageHeight;
bringToFront();
}
addEventListener(Event.ENTER_FRAME, groupFishLeftEnterFrame);
removeEventListener(Event.ENTER_FRAME, groupFishEnterFrame);
}
}
private function groupFishLeftEnterFrame(event:Event):void {
var fishLeftVX:Number = -13;
groupFishLeft.x+=fishLeftVX;
if (groupFishLeft.x < -100){
for (var a:int=0; a<1; a++){
removeChild(groupFishLeft);
addChild(groupFish);
groupFish.x=-100;
groupFish.y=Math.random()*stage.stageHeight;
bringToFront();
}
addEventListener(Event.ENTER_FRAME, groupFishEnterFrame);
removeEventListener(Event.ENTER_FRAME, groupFishLeftEnterFrame);
}
}
private function yellowFishEnterFrame(event:Event):void {
var yellowFishVXLeft:Number = -10;
yellowFish.x+=yellowFishVXLeft;
if(yellowFish.x < -100){
for (var j:int=0; j<1; j++){
removeChild(yellowFish);
addChild(yellowFishRight);
yellowFishRight.x=-300;
yellowFishRight.y=Math.random()*stage.stageHeight;
bringToFront();
}
removeEventListener(Event.ENTER_FRAME, yellowFishEnterFrame);
this.addEventListener(Event.ENTER_FRAME, yellowFishRightEnterFrame);
}
}
private function yellowFishRightEnterFrame(event:Event):void {
var yellowFishVXRight:Number = 10;
yellowFishRight.x+=yellowFishVXRight;
if (yellowFishRight.x > 700){
for (var j:int=0; j<1; j++){
removeChild(yellowFishRight);
addChild(yellowFish);
yellowFish.x=700;
yellowFish.y=Math.random()*stage.stageHeight;
bringToFront();
}
removeEventListener(Event.ENTER_FRAME, yellowFishRightEnterFrame);
this.addEventListener(Event.ENTER_FRAME, yellowFishEnterFrame);
}
}
private function blueFishRightEnterFrame(event:Event):void {
var blueFishVXRight:Number = 10;
blueFishRight.x+=blueFishVXRight;
if(blueFishRight.x > 700){
for (var j:int=0; j<1; j++){
removeChild(blueFishRight);
addChild(blueFishLeft);
blueFishLeft.x= 700;
blueFishLeft.y=Math.random()*stage.stageHeight;
bringToFront();
}
removeEventListener(Event.ENTER_FRAME, blueFishRightEnterFrame);
this.addEventListener(Event.ENTER_FRAME, blueFishLeftEnterFrame);
}
}
private function blueFishLeftEnterFrame(event:Event):void {
var blueFishVXLeft:Number = -13;
blueFishLeft.x+=blueFishVXLeft;
if(blueFishLeft.x < -100){
for (var j:int=0; j<1; j++){
removeChild(blueFishLeft);
addChild(blueFishRight);
blueFishRight.x=-100;
blueFishRight.y=Math.random()*stage.stageHeight;
bringToFront();
}
removeEventListener(Event.ENTER_FRAME, blueFishLeftEnterFrame);
this.addEventListener(Event.ENTER_FRAME, blueFishRightEnterFrame);
}
}
public function createButtons() {
instructionsButton = new instructionsBtn();
instructionsButton.y=stage.stageHeight/2;
instructionsButton.x=(stage.stageWidth/2) + 90;
instructionsButton.addEventListener(MouseEvent.CLICK, instructionsHandler);
addChild(instructionsButton);
startQuizFirstButton = new startQuizFirstBtn();
startQuizFirstButton.y=stage.stageHeight/2;
startQuizFirstButton.x=(stage.stageWidth/2) - 90;
startQuizFirstButton.addEventListener(MouseEvent.CLICK, startQuizFirstHandler);
addChild(startQuizFirstButton);
nextButton = new nextBtn();
nextButton.y=255;
nextButton.x=505;
nextButton.addEventListener(MouseEvent.CLICK, nextHandler);
addChild(nextButton);
nextButton.visible=false;
continueButton = new continueBtn();
continueButton.y=255;
continueButton.x=505;
continueButton.addEventListener(MouseEvent.CLICK, continueHandler);
addChild(continueButton);
continueButton.visible=false;
backMainButton = new backMainBtn();
backMainButton.y=353;
backMainButton.x=435;
backMainButton.addEventListener(MouseEvent.CLICK, backMainHandler);
addChild(backMainButton);
backMainButton.visible=false;
startQuizButton = new startQuizBtn();
startQuizButton.y=350;
startQuizButton.x=495;
startQuizButton.addEventListener(MouseEvent.CLICK, startQuizHandler);
addChild(startQuizButton);
startQuizButton.visible=false;
startGameButton = new startGameBtn();
startGameButton.y=245;
startGameButton.x=520;
startGameButton.addEventListener(MouseEvent.CLICK, startGameHandler);
addChild(startGameButton);
startGameButton.visible=false;
}
private function createStatusBox() {
statusBox = new TextField();
statusBox.autoSize=TextFieldAutoSize.LEFT;
statusBox.y=stage.stageHeight-80;
addChild(statusBox);
}
private function showMessage(theMessage:String) {
statusBox.text=theMessage;
statusBox.x = (stage.stageWidth / 2 - (statusBox.width / 2));
}
private function addAllQuestions() {
for (var i:int = 0; i < quizQuestions.length; i++) {
addChild(quizQuestions*);
}
}
private function hideAllQuestions() {
for (var i:int = 0; i < quizQuestions.length; i++) {
quizQuestions*.visible=false;
}
}
function firstQuestion() {
currentIndex=Math.random()*quizQuestions.length;
currentQuestion=quizQuestions[Math.floor(currentIndex)];
currentQuestion.visible=false;
questionsAnswered.push(currentIndex);
}
private function nextHandler(event:MouseEvent) {
if (currentQuestion.userAnswer==0) {
errorField.visible = true;
return;
}
if (questionsAsked<4) {
questionsAsked++;
currentQuestion.visible=false;
currentIndex=Math.floor(Math.random()*quizQuestions.length);
while (questionsAnswered.indexOf(currentIndex) != -1) {
currentIndex=Math.floor(Math.random()*quizQuestions.length);
bringToFront();
}
currentQuestion=quizQuestions[currentIndex];
currentQuestion.visible=true;
questionsAnswered.push(currentIndex);
trace(questionsAnswered);
bringToFront();
if(errorField){
errorField.visible = false;
}
} else {
nextButton.visible=false;
errorField.visible = false;
continueButton.visible=true;
hideAllQuestions();
bringToFront();
computeScore();
}
}
private function instructionsHandler(event:MouseEvent) {
instructionsBackground.visible=true;
backMainButton.visible=true;
startQuizButton.visible=true;
instructionsButton.visible=false;
startQuizFirstButton.visible=false;
}
private function startQuizFirstHandler(event:MouseEvent) {
answerBackground.visible=true;
startQuizFirstButton.visible=false;
instructionsButton.visible=false;
currentQuestion.visible=true;
nextButton.visible=true;
}
private function backMainHandler(event:MouseEvent) {
instructionsBackground.visible=false;
backMainButton.visible=false;
startQuizButton.visible=false;
instructionsButton.visible=true;
startQuizFirstButton.visible=true;
}
private function startQuizHandler(event:MouseEvent) {
instructionsBackground.visible=false;
backMainButton.visible=false;
startQuizButton.visible=false;
answerBackground.visible=true;
nextButton.visible=true;
currentQuestion.visible=true;
}
private function computeScore() {
for (var i:int = 0; i < quizQuestions.length; i++) {
if (quizQuestions*.userAnswer==quizQuestions*.correctAnswer) {
score++;
time=score * 10;
if(time==0){
overallTime=10;
trace(overallTime);
}
else{
overallTime=time+10;
}
}
}
var scoreFormat:TextFormat = new TextFormat();
scoreFormat.size=25;
scoreFormat.font="Chalkboard";
scoreFormat.color=0x000000;
scoreNumber.defaultTextFormat=scoreFormat;
scoreNumber.text="You have answered "+score+" out of 5 questions correct.";
scoreNumber.autoSize=TextFieldAutoSize.CENTER;
scoreNumber.x=25;
scoreNumber.y=100;
scoreTime.defaultTextFormat=scoreFormat;
scoreTime.text="This will give you " +overallTime+ " seconds in the game.";
scoreTime.autoSize=TextFieldAutoSize.CENTER;
scoreTime.x=stage.stageWidth/9;
scoreTime.y=150;
addChild(scoreNumber);
addChild(scoreTime);
scoreNumber.visible = true;
scoreTime.visible = true;
addEventListener(Event.ENTER_FRAME, convertScore);
}
public function convertScore(e:Event){
time=score * 10;
overalltime= time + 10;
//var maxtime:SecondCounter = new SecondCounter( overalltime );
//addChild( maxtime );
}
public function onAvatarDeath( avatarEvent:AvatarEvent ):void
{
var finalScore:Number = playScreen.getFinalScore();
removeChild(playScreen);
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
flash.media.SoundMixer.stopAll()
gameOverScreen = new GameOverScreen();
gameOverScreen.addEventListener( NavigationEvent.RESTART, onRequestRestart );
gameOverScreen.x = 0;
gameOverScreen.y = 0;
gameOverScreen.setFinalScore( finalScore );
addChild( gameOverScreen );
playScreen = null;
}
public function startGameHandler(event:MouseEvent):void
{
if(stage.contains(blueFishRight)){
removeChild(blueFishRight);
}
else{
removeChild(blueFishLeft);
}
if(stage.contains(groupFish)){
removeChild(groupFish);
}
else{
removeChild(groupFishLeft);
}
if(stage.contains(yellowFish)){
removeChild(yellowFish);
}
else{
removeChild(yellowFishRight);
}
removeChild(answerBackground);
removeChild(quizBackground);
startGameButton.visible=false;
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
removeEventListener(Event.ENTER_FRAME, groupFishEnterFrame);
removeEventListener(Event.ENTER_FRAME, groupFishLeftEnterFrame);
removeEventListener(Event.ENTER_FRAME, yellowFishEnterFrame);
removeEventListener(Event.ENTER_FRAME, yellowFishRightEnterFrame);
removeEventListener(Event.ENTER_FRAME, blueFishRightEnterFrame);
removeEventListener(Event.ENTER_FRAME, blueFishLeftEnterFrame);
/*bgmSoundChannel.stop()
removeEventListener(Event.SOUND_COMPLETE,onBackgroundMusicFinished);*/
playScreen = new Game();
secondCounter = new SecondCounter();
playScreen.addEventListener( AvatarEvent.DEAD, onAvatarDeath );
secondCounter.addEventListener( AvatarEvent.DEAD, onAvatarDeath );
//playScreen.addEventListener(TimerEvent.TIMER, timerHandler);
playScreen.x = 0;
playScreen.y = 0;
addChild( playScreen );
addBubbles();
//menuScreen = null;
}
public function onRequestRestart( navigationEvent:NavigationEvent ):void
{
restartGame();
}
public function restartGame():void
{
currentIndex = 0;
questionsAsked = 0;
questionsAnswered.splice(0,questionsAnswered.length);
removeEventListener(Event.ENTER_FRAME, convertScore);
time=0;
overalltime = 0;
overallTime = 10;
score = 0;
health.meter.width=89;
quizQuestions = new Array();
addMainBackgrounds();
addBubbles();
addFish();
addYellowFish();
addBlueFish();
addQuizBackgrounds();
createQuestions();
createButtons();
createStatusBox();
addAllQuestions();
hideAllQuestions();
firstQuestion();
gameOverScreen = null;
}
private function continueHandler(event:MouseEvent) {
removeChild(nextButton);
removeChild(currentQuestion);
removeChild(continueButton);
scoreNumber.visible = false;
scoreTime.visible = false;
errorField.visible = false;
gotoAndStop(3);
reminderInst.visible = true;
setChildIndex(reminderInst, numChildren -1);
startGameButton.visible=true;
setChildIndex(startGameButton, numChildren -1);
}
}
}
But I don’ t understand why i’m getting the error.
Any ideas?
Thanks in advance