dcsimg
A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Multiple HitTests Help! Please!

  1. #1
    Senior Member
    Join Date
    Apr 2007
    Posts
    269

    Exclamation Multiple HitTests Help! Please!

    Hi there I've been reading up and looking at examples of how to handle multiple hittests. So far in my example I have failed.
    PHP Code:
    ///Variables
    var Xdist:Number 0;
    var 
    Ydist:Number 0;
    var 
    partAng:Number 0;
    var 
    bulletXvel:Number 0;
    var 
    bulletYvel:Number 0;
    var 
    bulletAcc:Number 2;
    var 
    bulletDec:Number 0.98;
    var 
    firePower:Number 12;
    ///Arrays
    //Stores Bullets
    var particleArray:Array = [ ];
    //Stores Atoms
    var atomArray:Array = [ ];
    //Stores the individual Atom's X'n'Y Position
    var atomPosX:Array = [600,200];
    var 
    atomPosY:Array = [150,150];

    //Creates the Atoms using MC from the library and X'n'Y Pos Arrays, then stores the created in an array.
    for(var i=0i<atomPosX.lengthi++){
        var 
    nuke = new atom();
        
        
    nuke.atomPosX[i];
        
    nuke.atomPosY[i];
        
        
    atomArray.push(nuke);
        
    addChild(nuke);
    }

    stage.addEventListener(Event.ENTER_FRAMEeachFrame);
    function 
    eachFrame(event:Event):void {
        
    //Trig for barrel position
        
    Xdist = (turret.mouseX);
        
    Ydist = (mouseY turret.y);
        
    turret.barrel.rotation = -((Math.atan2(Ydist,Xdist)) * (180/Math.PI));
        
    //Move Particles
        
    for(var i=0i<particleArray.lengthi++) {
            
    //Temporary name storing var
            
    var part particleArray[i];
            
    //Moves the bullet by its individual velocities
            
    part.+= part.xmov;
            
    part.+= part.ymov;
            
    //Removes bullets when they hit the bounds of the stage
            
    if(part.850 || part.< -50 || part.< -50 || part.650) {
                
    removeChild(part);
                
    particleArray.splice(i1);
            }
        }
    }
    stage.addEventListener(MouseEvent.MOUSE_DOWNfireParticle);
    function 
    fireParticle(event:MouseEvent):void {
        var 
    part = new electron();
        
        
    partAng turret.barrel.rotation;
        
    //Intial X'n'Y Position of the bullet
        
    part.turret.x+(70Math.cos((turret.barrel.rotation+180)* Math.PI/180));
        
    part.turret.y+(70Math.sin((turret.barrel.rotation+180)* Math.PI/180));
        
    //Initial bullet velocities.
        
    part.xmov = -Math.cos(partAng*Math.PI/180)* firePower;
        
    part.ymov = -Math.sin(partAng*Math.PI/180)* firePower;
        
    //Storing bullet in the array;
        
    particleArray.push(part);
        
    addChild(part);

    Basically I want to be able to check for a hitTest against all the bullets on stage against all atoms created. I attempted to do this with two for loops and it didn't work can anyone help me with this specific problem. Project folder attached.

    Thanks in advance.
    Attached Files Attached Files

  2. #2
    a.k.a gltovar deadlock32's Avatar
    Join Date
    May 2001
    Location
    Naperville,IL
    Posts
    489
    one problem I see off the bat is here:

    PHP Code:
      for(var i=0i<particleArray.lengthi++) {
            
    //Temporary name storing var
            
    var part particleArray[i];
            
    //Moves the bullet by its individual velocities
            
    part.+= part.xmov;
            
    part.+= part.ymov;
            
    //Removes bullets when they hit the bounds of the stage
            
    if(part.850 || part.< -50 || part.< -50 || part.650) {
                
    removeChild(part);
                
    particleArray.splice(i1);  // <<<<<< RIGHT HERE
            
    }
        } 
    your for loop is going though the length of the particleArray, but if you splice off part of the array it gets shorter, so it skips the next bullet

    there are a few ways to get around this. here is one way:
    PHP Code:
      for(var i=0i<particleArray.lengthi++) {
            
    //Temporary name storing var
            
    var part particleArray[i];
            
    //Moves the bullet by its individual velocities
            
    part.+= part.xmov;
            
    part.+= part.ymov;
            
    //Removes bullets when they hit the bounds of the stage
            
    if(part.850 || part.< -50 || part.< -50 || part.650) {
                
    removeChild(part);
                
    particleArray.splice(i1);
                
    i--;  // we subtract on from i, to keep from skipping an element.
            
    }
        } 
    let us know if there are other problems after doing this, since its the first thing I found. = D

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