Sigh, my script is so close to working… can anyone help me out here?
First of all, here’s the whole script:
// creating target
createTarget = function (x, y) {
var targ = this.attachMovie("targetlink", "Target", _root.depth++);
// variables for rapid code execution
var mr = Math.random, mf = Math.floor;
// put target at specific x and y on stage
targ._x = mf(mr()*400);
targ._y = mf(mr()*400);
// set target's rotation
targ._rotation = mf(mr()*360)-180;
return targ;
};
// Array creation
ba = new Array();
howmany = 0;
counter = 20;
rate = 4.0;
_root.depth = 0;
// Make those targets!
this.onEnterFrame = function() {
if (counter>=20) {
howmany++;
ba[howmany] = createTarget();
ba[howmany].rate = random(3)+1;
ba[howmany]._xscale = random(90)+10;
ba[howmany]._yscale = ba[howmany]._xscale;
counter = 0;
} else {
counter++;
}
for (i=0; i<=howmany; i++) {
ba*._x += ba*.rate;
if ((ba*.hitTest(_root._xmouse, _root._ymouse, true))) {
myColor = new Color(_root.crosshairs);
myColor.setRGB(0xFF0000);
} else {
myColor.setRGB(0x000000);
}
}
};
// For target to be shot
this.onMouseDown = function() {
for (i in ba) {
var targ = ba*;
if ((ba*.hitTest(_root._xmouse, _root._ymouse, true))) {
ba.splice(i, 1);
void (targ.removeMovieClip());
return;
}
}
};
Everything works great, the random target generation, the onMouseDown shooting, etc.
There is just one small problem: the targets can only be shot in the order they appear.
I.E.: If you click each target AS it appears, and BEFORE another appears, it just disappears and all is well. BUT, if one or more targets appear in addition to the first one to appear, then clicking on the new targets does NOTHING. Clicking on the first target to appear makes all the targets disappear, in order, until finally the one you’re clicking on disappears.
This is really weird, and I’d REALLY appreciate any help on debugging this… I feel so close to finishing my game
the problem is that your clips are all named the same thing. append _root.depth to the names to differentiate them.
however it took me ages to spot that and i’d built another method of managing this before i realised what was wrong.
to make me feel less like i wasted my time, may i recommend a different approach?
rather than storing your objects in an array and looping that array for hitTests etc. why not write the actions you wish to accomplish as methods of the MovieClip object and have each target execute them when necessary?
here’s an approach which works:
// creating target
createTarget = function (x, y) {
var targ = this.attachMovie("targetlink", "t"+_root.depth, (_root.depth++)%99);
// variables for rapid code execution
var mr = Math.random, mf = Math.floor;
// put target at specific x and y on stage
targ._x = mf(mr()*400);
targ._y = mf(mr()*400);
// set target's rotation
targ._rotation = mf(mr()*360)-180;
targ.rate = random(3)+1;
targ._xscale = targ._yscale = random(90)+10;
targ.useHandCursor = 0;
targ.onEnterFrame = targ.move;
targ.onRollOver = targ.cursorColourOn;
tart.onRollOut = targ.onDragOut = targ.cursorColourOff;
targ.onPress = targ.getShot;
return targ;
};
MovieClip.prototype.move = function(){
this._x += this.rate;
}
MovieClip.prototype.cursorColourOn = function(){
_root.myColor.setRGB(0xFF0000);
}
MovieClip.prototype.cursorColourOff = function(){
_root.myColor.setRGB(0x000000);
}
MovieClip.prototype.getShot = function() {
this.cursorColourOff();
removeMovieClip(this);
};
_root.myColor = new Color(_root.crosshairs);
_root.crosshairs.swapDepths(100);
_root.crosshairs.onMouseMove = function(){
this._x = _root._xmouse;
this._y = _root._ymouse;
updateAfterEvent();
}
Mouse.hide();
// Make those targets!
_root.depth = 0;
this.onEnterFrame = function() {
if (Math.random()<.05) createTarget();
}
i moduloed the depths by 99 because i put the crosshairs in 100 and don’t want it to be overwritten by target. there shouldn’t be more than 99 targets at any given time so we should be alright there.
does this approach make sense? it’s more like you define behaviours, then assign them.
sbeener, this is GREAT! Thank you SO much for saving the day. I’m sorry my silly mistake made you waste so much time :-\
But again, thanks a TON for helping me with this… I know it was a pretty annoying issue and I was afraid my thread was becoming the black sheep of the help boards
Thank you for spending time on this like that, I hope I can help you out somehow someday :beam: