PDA

View Full Version : Vertical Shooter Bullets



ZakMcCracken
November 30th, 2007, 04:26 AM
Hey so I've made a vertical shooter in space where you fire at falling enemies with the ability for multiple bullets to be on the screen at a time.

Everything was running smooth up to the hitTest on the enemies detecting a hit from a laser. Not all of the lasers fired would unload the enemies and I've come to realize that only the most recent fired shot can actually work.

Code I've used for the enemy is:

function enemy_clover(){
attachMovie("clover","clover"+enemyDepth,enemyDepth)
clover = eval("clover"+enemyDepth)
enemyDepth++
clover._x = Math.floor(Math.random()*(25-425))+425;
clover._y = -50
clover.enemySpeed = Math.floor(Math.random()*(2-4))+4;
clover.enemyRotation = Math.floor(Math.random()*(-15-15))+15;
clover.onEnterFrame = function(){
this._y+=this.enemySpeed;
this._rotation += this.enemyRotation
if(enemyDepth>4000){
enemyDepth = 3000;
}
if (this.hitTest(_root.laserShot)) {
this.removeMovieClip();
_root.laserShot.removeMovieClip();
score+= 5;
}
}
}.....and the code for the laser:

_root.attachMovie("shot","shot"+laserDepth,laserDepth);
laserShot = eval("shot"+laserDepth);
laserDepth++;
.....I realize that the "laserShot" being tested for in the hitTest on the enemy is only the most recent and highest "laserDepth".

Is there another way I can call the lasers? I would appreciate any help.
Thanks :rocker:

Charleh
November 30th, 2007, 06:12 AM
I've written an entire tutorial on a vertical space shooter/game engine - part 2 is due up soon, but in part 1 I do collision testing and stuff so you might want to have a look

http://www.kirupa.com/forum/showthread.php?t=281507

acebuddy29
November 30th, 2007, 07:08 AM
Try This.


var laserArray:Array = new Array ();
var lasersOnScreen:Number;
var j:Number;
enemyDepth = 1

function enemy_clover ()
{
attachMovie ("clover","clover" + enemyDepth,enemyDepth);
clover = eval ("clover" + enemyDepth);
enemyDepth++;
clover._x = Math.floor (Math.random () * (25 - 425)) + 425;
clover._y = -50;
clover.enemySpeed = Math.floor (Math.random () * (2 - 4)) + 4;
clover.enemyRotation = Math.floor (Math.random () * (-15 - 15)) + 15;
clover.onEnterFrame = function ()
{
this._y += this.enemySpeed;
this._rotation += this.enemyRotation;
if (enemyDepth > 4000)
{
enemyDepth = 3000;
}
for (j = 0; j <= lasersOnScreen; j++)
{
if (this.hitTest (laserArray[j]))
{
this.removeMovieClip ();
_root.laserArray[j].removeMovieClip ();
if(j >= lasersOnScreen)
{
j=0
}
}

}
/*
if (this.hitTest(_root.laserShot)) {
this.removeMovieClip();
_root.laserShot.removeMovieClip();
score+= 5;
}*/
};
}


function shootLaser (howMany)
{
lasersOnScreen = howMany;
for (var i = 0; i <= howMany; i++)
{
var laserShot = _root.attachMovie ("shot", "shot" + i, i);
laserArray.push (laserShot);
trace(laserArray)
}
}
shootLaser (5);
P.S:Might Get a bit CPU intensive(hittest is usually cpu intersive), will try refining it ....if i get time. Hope it helps

ZakMcCracken
November 30th, 2007, 01:27 PM
Thanks a lot guys. I've never really learned much about arrays, but nows seems to be the perfect time.

ZakMcCracken
December 5th, 2007, 11:01 AM
I guess I assumed that when you hitTest an array it checks for a hit against anything in that array.

I attempted at:

function shoot() {
_root.attachMovie("shot","shot_"+j,j+1000);
laserShot = eval("shot_"+j);
if(j == 10){
j = 0;
}
_root.laserArray[j] = "_level0.shot_"+j;
trace (laserArray)
j++;
...

clover.onEnterFrame = function(){
if (this.hitTest(_root.laserArray)) {
this.removeMovieClip();
_root.laserShot.removeMovieClip();
score+= 5;
}
}
...
..but the hitTest only works for the first shot when the laserArray is only 1x1 and is only "_level0.shot_0"

I've got the items adding to the array fine and after 9 shots the bullet names recycle, but I guess the hitTest is where I'm lost.

Maybe I didn't fully understand what you were suggesting acebuddy29, but I'm pretty that every time I fire, the laserArray fires 5 bullets stacked on top of each other and then adds 5 to that every shot after that, and also adding the array to itself. Maybe I'm just executing it wrong though.

And Charleh, I've been looking over your tutorial and it is good, just a little over my head at the moment. I'm trying to work my way up to there.

:pa:

Charleh
December 5th, 2007, 11:08 AM
You dont hitTest an array, you loop through the objects in an array and hitTest each one individually -

Try these



function shoot() {
var newLaser:MovieClip = _root.attachMovie("shot","shot_" + _root.getNextHighestDepth(), _root.getNextHighestDepth());
laserArray.push(newLaser);
}

clover.onEnterFrame = function() {
for (var i = 0; i < _root.laserArray.length; i++) {
if (this.hitTest(_root.laserArray[i])) {
_root.laserArray[i].removeMovieClip();
score+= 5;
}
}

ZakMcCracken
December 5th, 2007, 06:04 PM
Awesome! That makes sense and it works. I appreciate it :krazy: