I’ve got a game where the user drags and drops object to places on the screen, then clicks a button to check their positions. The main code calls a second class in to check the answers, place an object on a the screen in a holder, and then return the number of correct answers. The main code then places a button to remove the objects on a click (after user reviews) by calling the second class again . The problem is that I can’t remove these objects with the click.
The code:
Code of the main body: public class SigTransScripts; this calls the Scene1Scripts class to check answers of the position of “epi” and “bar” and returns the number of correct
To check the answers of the game, after a button click:
function checkAnswers (e:MouseEvent):void
{
scene = new Scene1Scripts();
addChild(scene);
numCorrect = scene.init(epi.x, epi.y, bar.x, bar.y);
numWrong = 2 - numCorrect;
score = score + (numWrong * -5) + (numCorrect * 10);
removeChild (scoreText);
scoreText.text = score;
scoreText.setTextFormat (format);
addChild (scoreText);
RemoveChkBtn.x = 30;
RemoveChkBtn.y = 340;
addChild (RemoveChkBtn);
RemoveChkBtn.buttonMode = true;
RemoveChkBtn.addEventListener (MouseEvent.CLICK, removeChk);
}
Code in the public class Scene1Scripts to check answers, place an object for correct answers (correctChk1 or 2) in a movie clip holder (chkHolder), and then return the number correct answers:
public function init (epiX:int, epiY:int, barX:int, barY:int,):int
{
addChild (chkHolder);
var numCorrect:int = 0;
if ((epiX > 200) && (epiY >15) && (epiY <30) && (epiX <220))
{ correctChk1.x = 100;
correctChk1.y = 100;
chkHolder.addChild(correctChk1);
trace (chkHolder.numChildren); // this returns 1
numCorrect++; }
if ((barX > 210) && (barX < 235) && (barY > 55) && (barY < 70) )
{ correctChk2.x = 100;
correctChk2.y = 150;
chkHolder.addChild(correctChk2);
trace (chkHolder.numChildren); // this returns 2
numCorrect++}
return numCorrect;
}
Back to SigTransScripts - click a button to remove the objects (correctChk1 and correctChk2) from the screen by emptying chkHolder:
function removeChk (e:MouseEvent):void
{ scene = new Scene1Scripts();
addChild(scene);
scene.chk();
}
Code in the public class Scene1Scripts to remove the correct Chk objects:
function chk ():void
{ trace ("chk = ",chkHolder.numChildren); // this returns 0
while (chkHolder.numChildren) chkHolder.removeChildAt(0);
}
This fails to remove the objects because, as the trace shows, chkHolder is empty. If this is true, then how did it get empty, where are the correctChk1 and 2 and how do remove them? Help!