Skip to main content
Known Participant
September 16, 2009
Answered

attachmovie, hitTest & multidimentional array question...

  • September 16, 2009
  • 1 reply
  • 996 views

Hi all

I'm trying to make a game (my first) and I'm having trouble making my dynamically attached bullets and enemies collide. I've tried so many variations, tutorials, forum searches... to no avail.

Here's my code:

bulletArr = new Array();
var bulletCount = 0;

badArr = new Array();
var badCount = 0;
var badAmount = 0;

onEnterFrame = function() {
    if (Key.isDown(Key.SPACE)) {
        bulletCount ++;
        var bulletName = bulletArr.push("bullet" + bulletCount);
        attachMovie("bullet", bulletName, bulletCount)
        this[bulletName]._y = random(Stage.height);
    }
    badAmount ++;
    if (badAmount > 50) {
        badCount ++;
        var badName = badArr.push("bad" + badCount);
        attachMovie("bad", badName, badCount);
        this[badName]._x = random(Stage.width);
        this[badName]._y = random(Stage.height);
        badAmount = 0;
    }
    this[bulletName].onEnterFrame = function() {
        this._x += 10;
        if (this._x > 560) {
            this.removeMovieClip();
        }

        for (i=0; i<badArr.length; i++) {
           if (this.hitTest(badArr)) {
                badArr._alpha -= 50;
            }

        }
    }

}

Any help is appreciated.

Thanks

This topic has been closed for replies.
Correct answer kglad

badArr is an array of strings.  you need to help flash resolve those strings to movieclips using array notation  or, even easier just add the movieclip references to your arrays:


bulletArr = new Array();
var bulletCount = 0;

badArr = new Array();
var badCount = 0;
var badAmount = 0;

onEnterFrame = function() {
    if (Key.isDown(Key.SPACE)) {
        bulletCount ++; 
        var bulletName = "bullet" + bulletCount;
        var bmc:MovieClip = attachMovie("bullet", bulletName, bulletCount)
        bulletArr.push(mc);
        bmc._y = random(Stage.height);     }     badAmount ++;     if (badAmount > 50) {         badCount ++;         var badName = "bad" + badCount;         var mc:MovieClip = attachMovie("bad", badName, badCount);         badArr.push(mc);
        mc._x = random(Stage.width);         mc._y = random(Stage.height);         badAmount = 0;     }     bmc.onEnterFrame = function() {         this._x += 10;         if (this._x > 560) {             this.removeMovieClip();         }         for (i=0; i<badArr.length; i++) {            if (this.hitTest(badArr)) {                 badArr._alpha -= 50;                 if(badArr._alpha<=0){
                   var mc:MovieClip = badArr;
                   badArr.splice(i,1);
                   mc.removeMovieClip();
                }
            }         }     } }

Any help is appreciated.

Thanks

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
September 16, 2009

badArr is an array of strings.  you need to help flash resolve those strings to movieclips using array notation  or, even easier just add the movieclip references to your arrays:


bulletArr = new Array();
var bulletCount = 0;

badArr = new Array();
var badCount = 0;
var badAmount = 0;

onEnterFrame = function() {
    if (Key.isDown(Key.SPACE)) {
        bulletCount ++; 
        var bulletName = "bullet" + bulletCount;
        var bmc:MovieClip = attachMovie("bullet", bulletName, bulletCount)
        bulletArr.push(mc);
        bmc._y = random(Stage.height);     }     badAmount ++;     if (badAmount > 50) {         badCount ++;         var badName = "bad" + badCount;         var mc:MovieClip = attachMovie("bad", badName, badCount);         badArr.push(mc);
        mc._x = random(Stage.width);         mc._y = random(Stage.height);         badAmount = 0;     }     bmc.onEnterFrame = function() {         this._x += 10;         if (this._x > 560) {             this.removeMovieClip();         }         for (i=0; i<badArr.length; i++) {            if (this.hitTest(badArr)) {                 badArr._alpha -= 50;                 if(badArr._alpha<=0){
                   var mc:MovieClip = badArr;
                   badArr.splice(i,1);
                   mc.removeMovieClip();
                }
            }         }     } }

Any help is appreciated.

Thanks

Known Participant
September 16, 2009

Thank you soooooo much, I was pulling out my hair...

I'm really curious, do you help people here for the joy of teaching, or as a job? I don't mean to be rude/forward. Whatever the reason, it is very much appreciated.

Thanks

kglad
Community Expert
Community Expert
September 17, 2009

you're welcome.

no, i'm not paid for helping people.  but i do get hired occassionally by people i help.