A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Trouble hitTesting an array.

  1. #1
    Member
    Join Date
    Apr 2009
    Posts
    62

    Trouble hitTesting an array.

    Hey everyone,
    I am having trouble hitTesting items in an array...here is my full code:
    PHP Code:
    stop();

    var 
    gameStarted:Boolean false;
    var 
    gamePaused:Boolean false;
    var 
    generateSpeed:Number 7;
    var 
    speed:Number 0;
    var 
    score:Number 0;
    var 
    health:Number 100;
    var 
    spiderArray:Array = [];

    hud.pauseBar._visible false;

    menuSound.stop();
    inGameSound.start();

    this.createEmptyMovieClip('spiders',this.getNextHighestDepth());

    this.onEnterFrame = function() {
        if (!
    gamePaused && gameStarted) {
            
    score++;
        }

        if (
    score == 1000 || score == 2000 || score == 3000 || score == 4000 || score == 5000) {
            
    generateSpeed += 3.5;
        }

        if (
    score == 6000) {
            
    generateSpeed 28;
        }

        if (
    generateSpeed>28) {
            
    generateSpeed 28;
        }

        if (
    health<0) {
            
    health 0;
        }

        if (
    health == 0) {
            
    delete this.onEnterFrame;
            
    gameStarted false;
            
    inGameSound.stop();
            
    gameOverSound.start();
        }
    };

    hud.onEnterFrame = function() {
        if (
    gameStarted && !gamePaused) {
            
    this.swapDepths(this.getNextHighestDepth());
        }
    };

    car.onEnterFrame = function() {
        if (
    gameStarted && !gamePaused) {
            if (
    Key.isDown(Key.UP)) {
                
    this._y -= speed/2;
            }

            if (
    Key.isDown(Key.DOWN)) {
                
    this._y += speed;
            }

            if (
    Key.isDown(Key.LEFT)) {
                
    this._x -= speed;
            }

            if (
    Key.isDown(Key.RIGHT)) {
                
    this._x += speed;
            }
            
    speed += 0.3;

            if (
    speed>=15) {
                
    speed 15;
            }

            if (
    this._x<=37.35) {
                
    this._x 37.35;
            }

            if (
    this._x>=762.65) {
                
    this._x 762.65;
            }

            if (
    this._y<=42.65) {
                
    this._y 42.65;
            }

            if (
    this._y>=507.40) {
                
    this._y 507.40;
            }
            
            for(
    x in spiderArray){
                if (
    this.hitTest(_root[spiderArray[x]])) {
                    
    _root[spiderArray[x]].removeMovieClip();
                    
    this.removeMovieClip();
                    
    spiderArray.splice(x1);
                }
            }
        }
    };

    grass.onEnterFrame = function() {
        if (
    gameStarted && !gamePaused) {
            
    this._y += speed;
        }

        if (
    this._y>0) {
            
    this._y = -200;
        }
    };

    spiderGenerator.onEnterFrame = function() {
        if (!
    gamePaused && gameStarted) {
            
    this._y -= generateSpeed;
        }

        if (
    this._y<0) {
            
    growSpider();
            
    this._y 600;
        }
    };

    function 
    growSpider() {
        if (!
    gamePaused && gameStarted) {
            
    spiderCount 0;
            while (
    spiderCount<Math.floor(Math.random()*(9-7))+7) {
                
    spiderCount++;
                
    randomX Math.floor(Math.random()*(745-55))+55;
                
    spider spiders.attachMovie('Spider''Spider'+spiders.getNextHighestDepth(), spiders.getNextHighestDepth(), {_x:randomX_y:-32});
                
    spiderArray.push(spider._name);

                
    spider.onEnterFrame = function() {
                    if (!
    gamePaused) {
                        
    this._y += speed/2;
                        
    this.play();
                    }

                    if (
    this._y>=632) {
                        
    this.removeMovieClip();
                    }
                };
            }
        }
    }

    function 
    transitionToNextFrame() {
        
    nextFrame();

    Any ideas why it's not working?
    Check out my firefox theme "PacStrata" here

  2. #2
    Senior Member
    Join Date
    Jun 2007
    Location
    Nottingham, England
    Posts
    203
    Only thing that immediately sticks out is that wherever you have 'spiders' it should be:

    "spiders"

    Give that a try.

  3. #3
    Member
    Join Date
    Apr 2009
    Posts
    62
    Tried it, but still no go...
    Check out my firefox theme "PacStrata" here

  4. #4
    Senior Member
    Join Date
    Jun 2007
    Location
    Nottingham, England
    Posts
    203
    Hard to tell then really mate without having the FLA. Hit testing with loops and such is always a bit of a funny one.

    Unfortunately the only way you can find out what's going wrong is just by tracing stuff all over the place to see if it outputs what it should. Try tracing every 'x in spiderArray' for every iteration of your loop to see what it returns.

    Also, trace when you push your new spiders into the array. The depths don't look quite right to me but I could be wrong. Just as a temporary sort of 'test', replace your attachMovie line with this:
    PHP Code:
    spiders.attachMovie("Spider""Spider"+spiderCountspiderCount, {_x:randomX_y:-32}); 
    If that works you might not end up with the depths how you wanted but you'll have to play around with it. I've had a lot of problems before with getNetHighestDepth()+this+that etc. when attaching movies. Let me know how you get on.

  5. #5
    Member
    Join Date
    Apr 2009
    Posts
    62
    I got it working eventually...it had to be:
    PHP Code:
     for(x in spiderArray){
                if (
    this.hitTest(_root.spiders[spiderArray[x]])) {
                    
    _root.spiders[spiderArray[x]].removeMovieClip();
                    
    spiderArray.splice(x1);
                }
            } 
    Such a simple mistake...
    Check out my firefox theme "PacStrata" here

  6. #6
    Senior Member
    Join Date
    Jun 2007
    Location
    Nottingham, England
    Posts
    203
    Aha nice one. Yeah they always are!!

    Good feeling when you finally overcome something like that and you can move on. Until something else goes wrong!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center