i'm kinda new for as3 and i'm trying to make simple shooting game for my education. First, i know there are other similar questions like mine but i couldn't figure out what's wrong with mine! So if anyone can help i'll be happy a lot :)) thanks in advance
My ship moving as fixed left and right and shoots bullets when i click to big button. Also enemy ships (dusman/dusmanlar) comes down. So problem is that i couldn't figure out some bullets (mermi/mermiler) hit's and destroys some enemy ships with that very bullet itself but most of the time bullets just passing through the enemy ships and nothing happens. And of course animate gives the error Parameter hitTestObject must be non-null on this project!
What i'm doing wrong here?
var mermiler: Array = new Array(); //mermiler means bullets
var dusmanlar: Array = new Array(); // dusmanlar means enemy ships
var game_speed: int = 20;
var game_timer: int = 0;
var oyunHiz: int = 8; //bullet speed
var skor: Number = 0;
btn.addEventListener(MouseEvent.CLICK, ates);
function ates(event: MouseEvent): void {
//whenever the mouse is pressed add the Bullet from the library to the screen
var mermi: Mermi = new Mermi();
mermi.x = gemi.x;
mermi.y = gemi.y - (gemi.height / 2);
addChild(mermi);
//add it to the bullet array so we can loop through to move and hit test
mermiler.push(mermi);
}
addEventListener(Event.ENTER_FRAME, basla);
function basla(event: Event): void {
game_timer++;
if (game_timer >= game_speed) {
var dusman: Dusman = new Dusman();
dusman.x = Math.random() * 640 + 10;
dusman.y = Math.random() * 100 - 120;
dusman.xspeed = (Math.random() * 15) - 10;
dusman.yspeed = (Math.random() * 5) + 10;
addChild(dusman);
dusmanlar.push(dusman);
game_timer = 0;
}
yaz.text = String(skor);
gemi.x = mouseX;
gemi.y = 750;
//move all enemy ships down the screen
for (i = 0; i < dusmanlar.length; i++) {
//move enemy ships
dusmanlar[i].y += dusmanlar[i].yspeed;
dusmanlar[i].x += dusmanlar[i].xspeed;
//bounce off sides
if (dusmanlar[i].x >= 640 || dusmanlar[i].x <= 0) {
dusmanlar[i].xspeed = -dusmanlar[i].xspeed;
}
//if reach bottom remove ship
if (dusmanlar[i].y >= 860) {
removeChild(dusmanlar[i]);
dusmanlar[i] = null;
dusmanlar.splice(i, 1);
break;
}
}
//move all the bullets on the screen up
for (var i: int = 0; i < mermiler.length; i++) {
//move bullets
mermiler[i].y -= 20;
//if bullets got of screen
if (mermiler[i].y <= 0) {
removeChild(mermiler[i]);
mermiler[i] = null;
mermiler.splice(i, 1);
break;
}
}
//test if any bullets have hit any ships
for (var j: int = 0; j < dusmanlar.length; j++) {
if (mermiler != null && dusmanlar[j].hitTestObject(mermiler[i])) {
if (mermiler.length != 0 && dusmanlar[j] != null && mermiler[i].hitTestObject(dusmanlar[j])) {
removeChild(dusmanlar[j]);
dusmanlar[j] = null;
dusmanlar.splice(j, 1);
removeChild(mermiler[i]);
mermiler[i] = null;
mermiler.splice(i, 1);
skor++
break;
}
}
}
}