PDA

View Full Version : game hit test?


JL1
11-29-2007, 04:07 PM
I am trying to make a game where a ship will shoot at objects coming towards it. I am trying to get it so that after a certin amount of the enemy comes onto the stage, it goes to the next level. I was thinking of havng a invisable movie clip and have a hitTest to count how many ships cross it and then have it move on to the next level. I don't know how to go about doing this. I'm still new at actionscript and most of the code I have is from tutorials so if anyone has a better suggestion please let me know.



//stars
var stars = 50;
var maxSpeed = 18;
var minSpeed = 4;

for (var i = 0; i<stars; i++) {
var mc = this.attachMovie("star", "star"+i, i);
mc._x = random(Stage.width);
mc._y = random(Stage.height);
mc.speed = random(maxSpeed-minSpeed)+minSpeed;
var size = random(2)+0.6*(random(4));
mc._width = size;
mc._height = size;
}

this.onEnterFrame = function() {
moveObject();
moveStars();
moveShip();
moveBullets();
for (var j = 0; j<stars; j++) {

var mc = this ["star"+j];


if(mc._y<600) {
mc._y += mc.speed;
} else {
mc._y -= 600;
mc.speed = random (maxSpeed-minSpeed)+minSpeed
mc._x = random (Stage.width);
}
}
}
//end stars



ship_mc.onEnterFrame = function() {
if (_root.ship_mc.hitTest(_root.enemy)){
_root.ship_mc.play();
_root.lives -= 1;
if (_root.lives <= 0){
_root.attachMovie("gameOver", "gameOver", 100)
_root.gameOver._x = 10
_root.gameOver._y = 200
ship_mc.swapDepths(300);
ship_mc.removeMovieClip();
}
reset();
}

}



var bulletSpeed = 9;
var bulletReady = true;
var bulletDelay = 150;
var bulletArray = [];
var bulletCount = 0;


function createBullets() {
var bullet_mc = this.attachMovie("bullet", "bullet"+bulletCount, 1000+bulletCount);
bulletCount++;
bullet_mc._x = ship_mc._x+(ship_mc._width/2)-(bullet_mc._width/2);
bullet_mc._y = ship_mc._y+bullet_mc._height;
bulletArray.push(bullet_mc);
}

function moveBullets() {
if (bulletReady && Key.isDown(Key.SPACE)) {
bulletReady = false;
currentTime = getTimer();
createBullets();
} else {
if (currentTime+bulletDelay<=getTimer()) {
bulletReady = true;
}
}
for(var i = 0; i<bulletArray.length; i++) {
bulletArray[i]._y -= bulletSpeed;

var b = bulletArray[i];
if (b._y>20) {
b._y -= bulletSpeed;
} else {
removeMovieClip (b);
bulletArray.splice(i, 1);
}

if (!hit) {
if (b.hitTest(ob)) {
hit = true;
ob.play();
_root.score += 10;
removeMovieClip (b);
bulletArray.splice(i, 1);
}
}
}
}


var hit = false;
var obSpeed = 5;
function createObject(o) {
ob = this.attachMovie(o, o, 100);
ob._x = random(530)+20;
}


function resetObject() {
ob._x = random(530)+20;
ob._y = -70;
ob.gotoAndStop (1);
ob.restart = false;
hit = false
}

function moveObject() {
if (ob._y<550) {
ob._y += obSpeed;
} else {
resetObject();
}
if (ob.restart) {
resetObject();
}
}


createObject("enemy");


var shipspeed = 10;



var ship_mc = this.attachMovie ("ship", "ship", 10000);
ship_mc._y = Stage.height-(ship_mc._height+10);

ship_mc._x = Stage.width/2 - (ship_mc._width/2);
function moveShip() {
if (Key.isDown(Key.UP)) {
ship_mc._y -= shipspeed;
}
if (Key.isDown(Key.DOWN)) {
ship_mc._y += shipspeed;
}
if (Key.isDown(Key.LEFT)) {
ship_mc._x -= shipspeed;
}
if (Key.isDown(Key.RIGHT)) {
ship_mc._x += shipspeed;
}
}

var score = 0;