I am making a game where the player has to dodge attacks, these attacks come in waves and in the second wave I want an object to fall from the top of the screen and when it hits the ground to break apart into two separate pieces that go left and right.
Currently in the second wave I have the object fall but I cant get it to split and go left and right.
var gameTimer:Timer = new Timer(85000, 1); // game lasts the length of the song
var thesong:SoundChannel = new SoundChannel();
var songsnd:song = new song();
var nextObject:Timer;
var count:int = 0;
var objects:Array = new Array();
var wave:int = 1; // attack.
stage.addEventListener (Event.ENTER_FRAME, game);
function game(e:Event) {
stage.removeEventListener (Event.ENTER_FRAME, game);
thesong = songsnd.play();
setGameTimer();
setNextObject();
addEventListener(Event.ENTER_FRAME, moveObject);
}
function setGameTimer():void {
trace("GAME TIMER STARTED");
gameTimer.addEventListener(TimerEvent.TIMER_COMPLETE, nextattack);
gameTimer.start();
}
function setNextObject():void { // depending on which attack the speed is different.
if(wave == 1) {
nextObject = new Timer(1000, 1);
} else if(wave == 2) {
nextObject = new Timer(1500, 1);
} else {
nextObject = new Timer(250, 1)
}
nextObject.addEventListener(TimerEvent.TIMER_COMPLETE, newObject);
nextObject.start();
}
function newObject(e:TimerEvent) {
//create next object
var r2:int = Math.random() * 2 + 1;
if(wave == 1) {
var classRef:Class= getDefinitionByName("ATCK01") as Class;
} else if (wave == 2) {
var classRef:Class= getDefinitionByName("ATCK02") as Class;
// var classRef:Class= getDefinitionByName("Split") as Class;
} else {
var classRef:Class= getDefinitionByName("ERR") as Class;
}
var newObject:MovieClip = new classRef();
if (wave == 1) {
if(r2 == 1) {
newObject.typestr = "slideL";
newObject.x = Rwall.x; // right wall
newObject.y = Player.y;
addChild(newObject);
objects.push(newObject);
} else if(r2 == 2) {
newObject.typestr = "slideR";
newObject.x = Lwall.x; // left wall
newObject.y = Player.y;
addChild(newObject);
objects.push(newObject);
}
} else if (wave == 2) {
newObject.typestr = "fallD";
newObject.x = Player.x;
newObject.y = Twall.y;
addChild(newObject);
objects.push(newObject);
}
count++; // this section is what changes the current attack wave
if (count == 48) { wave = 2; }
if (count == 55) { wave = 1; }
setNextObject();
}
function moveObject(e:Event) { // speed at which each attack goes and the pattern
for (var i:int=objects.length-1; i>=0; i--) {
//move objects down based on speed
if (objects[i].typestr == "fallD") {
objects[i].y +=5;//5 is speed
if (objects[i].y > 357) { // this is where the floor is
removeChild(objects[i]);
objects.splice(i, 1);
// here is where I try to add the split and change the sprite to 'split'
// where the typestr should be slideL and slideR
}
} else if (objects[i].typestr == "slideL") {
objects[i].x -=5;
if (objects[i].x < 100){
removeChild(objects[i]);
objects.splice(i, 1);
}
} else if (objects[i].typestr == "slideR") {
objects[i].x +=5;
if (objects[i].x > (Rwall.x+50)) {
removeChild(objects[i]);
objects.splice(i, 1);
}
}
}
}
I think my problem is in moveObject, I'm not quite sure how to do the split my current attempts have ended in error
In moveObject where I have commented in fallD I was just missing the newObject in my attempts:
var classRef1:Class = getDefinitionByName("Split") as Class;
var newObject1:MovieClip = new classRef1();
newObject1.typestr = "slideL";
newObject1.x = xLoc; // x location before the split
newObject1.y = Bwall.y;
addChild(newObject1);
objects.push(newObject1);
var classRef2:Class = getDefinitionByName("Split") as Class;
var newObject2:MovieClip = new classRef2();
newObject2.typestr = "slideR";
newObject2.x = xLoc;
newObject2.y = Bwall.y;
addChild(newObject2);
objects.push(newObject2);