Hello, I’ve only started using Actionscript 3 recently and am having difficulty removing a number of childs from my flash game (a variant of frogger),
My aim is when the player’s character (WWalk) touches the EndGameZone on the other side of the stage, the generated Lorries should disappear, so i can put an ending animation on scene two.
I can get it to load Scene 2 but cannot get rid of the lorries - They just stay still and don’t disappear. If there is a simple enough solution, can anyone help? Many thanks!
My code is below:
stage.focus = stage;
//adding keyboard control over the character to control direction
stage.addEventListener (KeyboardEvent.KEY_DOWN, CWWalk);
function CWWalk (e:KeyboardEvent) {
switch (e.keyCode) {
case Keyboard.UP:
WWalk.y-=10;
break;
case Keyboard.DOWN:
WWalk.y+=10;
break;
case Keyboard.LEFT:
WWalk.x-=10;
break;
case Keyboard.RIGHT:
WWalk.gotoAndPlay("Walk");
WWalk.x+=10;
break;
}
}
var totalNumberOfLorries =8;
var startx = 75;
var lorrySeparation = 44;
var lorryIncrement=-5;
//function to create all the lorries
function createLorries (totalNumber) {
for (var lorryNo = 1; lorryNo<=totalNumber; lorryNo++) {
createLorry(lorryNo);
}
};
//function to create each lorry in turn
function createLorry (lorryNo) {
root["lorry"+lorryNo] = addChild(new Lorry());
var lorry = root["lorry"+lorryNo];
//setting size of lorries
lorry.width = 20;
lorry.height = 50+50*Math.random();
//setting position of lorries
lorry.x = startx + lorryNo * lorrySeparation;
lorry.y = 50+200*Math.random();
lorry.increment=lorryIncrement*lorryNo;
//changing direction of lorries in second set of lanes
if (lorryNo>totalNumberOfLorries/2) {
lorry.rotation=180;
lorry.increment=lorryIncrement*(lorryNo-1-totalNumberOfLorries);
}
}
//calling initial function createLorries
createLorries(totalNumberOfLorries);
stop();
addEventListener(Event.ENTER_FRAME , moveLorries);
function moveLorries (e: Event) {
for (var lorryNo = 1; lorryNo<=totalNumberOfLorries; lorryNo++) {
root["lorry"+lorryNo].y += root["lorry"+lorryNo].increment;
if (root["lorry"+lorryNo].y<=-50) {
root["lorry"+lorryNo].y=400;
}
if (root["lorry"+lorryNo].y>=405) {
root["lorry"+lorryNo].y=-45;
}
}
}
addEventListener(Event.ENTER_FRAME , testForHit);
function testForHit (e: Event ) {
for (var lorryNo = 1; lorryNo<=totalNumberOfLorries; lorryNo++) {
if (WWalk.hitTestObject(root["lorry"+lorryNo])) {
var squish=stage.addChild(new Squish());
squish.x=WWalk.x;
squish.y=WWalk.y;
squish.height = (WWalk.height/1.5);
squish.width=(WWalk.width/1.5);
WWalk.x = 25;
}
}
}
addEventListener(Event.ENTER_FRAME , testForFinish);
function testForFinish (e: Event ) {
if (WWalk.hitTestObject(EndGameZone)) {
stage.removeEventListener (KeyboardEvent.KEY_DOWN, CWWalk);
removeEventListener(Event.ENTER_FRAME , testForHit);
removeEventListener(Event.ENTER_FRAME , moveLorries);
removeEventListener(Event.ENTER_FRAME , testForFinish);
gotoAndPlay(1, "Scene 2");
}
}
Any help would be greatly appreciated!!