A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: AttachMovie HitTests

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    4

    AttachMovie HitTests

    Hi everyone, so I wasn't sure if I were to put this in the Actionscript forum or in here, so I put it in here. I'm making a small test for a game so I made two attached movieclips to the stage. I got them to move at each other, but when they hit, it doesn't detect it. Is there anyway i can make them hit eachother?
    Heres the code --
    Thanks in advance.(By the way, I'm using as2)

    Code:
    var hnumber:Number		= 0;
    var enumber:Number		= 1;
    var timer:Boolean		= false;
    onLoad = function(){
    	newHero2();
    	newEnemy2();
    }
    function newHero2(){
    	hnumber++
    	var newHero = "hero"+hnumber;
    	_root.attachMovie("hero", newHero, hnumber);
    	_root[newHero]._x = 65;
    	_root[newHero]._y = 197.3;
    	_root[newHero].onEnterFrame = function(){
    		_root[newHero]._x+=4;
    	}
    }
    function newEnemy2(){
    	enumber++
    	var newEnemy = "enemy"+enumber;
    	_root.attachMovie("enemy", newEnemy, enumber);
    	_root[newEnemy]._x = 476;
    	_root[newEnemy]._y = 197.3;
    	_root[newEnemy].onEnterFrame = function(){
    		_root[newEnemy]._x-=4;
    		if(_root[newEnemy].hitTest(_root[newHero])){
    			trace("HIT");
    		}
    	}
    }

  2. #2
    Senior Member Robb@exo's Avatar
    Join Date
    Jun 2003
    Location
    Canada
    Posts
    1,540
    You may be complicating things... try this,
    PHP Code:
    onLoad = function(){
        
    newHero2();
        
    newEnemy2();
    }
    function 
    newHero2(){
        
    this.attachMovie("hero""newHero"this.getNextHighestDepth());
        
    this.newHero._x 65;
        
    this.newHero._y 197.3;
        
    this.newHero.onEnterFrame = function(){
            
    this._x += 4;
        }
    }
    function 
    newEnemy2(){
        
    this.attachMovie("enemy""newEnemy"this.getNextHighestDepth());
        
    this.newEnemy._x 476;
        
    this.newEnemy._y 197.3;
        
    this.newEnemy.onEnterFrame = function(){
            
    this._x -= 4;
            if(
    newHero.hitTest(newEnemy)) {
                
    trace("HIT");
                
    delete newEnemy.onEnterFrame;
            }
        }

    Wile E. Coyote - "Clear as mud?"

  3. #3
    Junior Member
    Join Date
    Aug 2009
    Posts
    4
    Works great. Thanks!

  4. #4
    Senior Member Robb@exo's Avatar
    Join Date
    Jun 2003
    Location
    Canada
    Posts
    1,540
    Actually you may want to add this line of code in to remove the onEnterFrame for the hero function, unless of course you need it. EnterFrame events are resource hogs, so when they're not needed, delete them.
    PHP Code:
     if(newHero.hitTest(newEnemy)) {
                
    trace("HIT");
                
    delete newHero.onEnterFrame// add this line
                
    delete newEnemy.onEnterFrame;
            } 
    Wile E. Coyote - "Clear as mud?"

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