A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Slow HitTest

  1. #1
    Junior Member
    Join Date
    Jan 2006
    Posts
    28

    Slow HitTest

    The hunt continues. It took me several hours to understand how the hitTest should work. When I finally got it I found out that it didn't always work like it supposed to. The collision between the "defender" and the "attacker" work flawless, but when the bullets hit there's a delay. Not always though, only when you fire several shots at a time. Try it out here:

    http://www.reachground.se/shugoshin.html

    Controls: W-A-S-D -> Fire with ENTER.

    When the defender and the attacker collides the defender is supposed to move to Y-300 and X-150. When the bullets hit the attacker the defender should move to Y-300 and X-375 (this is only for testpurposes). I tried to trace it too and it only reacted to some bullets but not all.

    Code:
    var allowfire:Number = 1;
    
    arrow_mc.onEnterFrame = function() {
            if (Key.isDown(68)) {
                this._x += 10;
    			if(this._x >537) {
    				this._x=537;
    				}
    			}
            if (Key.isDown(65)) {
    			this._x -= 10;
    			if(this._x <13) {
    				this._x=13;
    				}
            	}
            if (Key.isDown(87)) {
                 this._y -= 10;
    			 if(this._y <15) {
    				this._y=15;
    			 	}
    			}
            if (Key.isDown(83)) {
                    this._y += 10;
    				if(this._y >387) {
    					this._y=387;
    				}
    			}
    		if(this.hitTest(ball)) {
    			arrow_mc._y = 300;
    			arrow_mc._x = 150;
    	}
    	arrowX = this._x;
    	arrowY = this._y;
    	
    	if (Key.isDown(Key.ENTER)) {
    		fireBullets();
    	} else {
    		allowFire = 1;
    	}
    }
    
    var i:Number = 1;
    
    function fireBullets() {
    	if(allowFire == 1) {
    		i += 1;
    		var newname = "bullet"+i;
    		_root.attachMovie("bullet", newname, i);
    //		trace("bullet"+newname+i);
    		_root[newname]._y = _root.arrow_mc._y-20;
    		_root[newname]._x = _root.arrow_mc._x+0;
    		allowFire = 0;
    		_root[newname].onEnterFrame = function() {
    			var bullet_speed = 10;
    			this._y -= bullet_speed;
    			if (this._y<0) {
    				this.removeMovieClip();
    			}
    		}
    	}
    }
    
    var angle:Number = 0;
    var centerX:Number = randRange();
    var range:Number = 20;
    var xspeed:Number = 5;
    var yspeed:Number = .6;
    
    ball.onEnterFrame = function() {
    
    	ball._y += xspeed;
    	ball._x = centerX + Math.sin(angle) * range;
    	angle += yspeed;
    	if(ball._y > 400) {
    		ball._y = -25;
    		centerX = randRange();
    		
    		}else{
    		if(this.hitTest(_root["bullet"+i])) {
    			arrow_mc._y = 300;
    			arrow_mc._x = 375;
    		}
    	}
    }
    
    function controlFire(){
    	allowFire = 1;
    }
    
    var myListener:Object = new Object();
    fireInt = setInterval(controlFire,500);

  2. #2
    Style Through Simplicity alillm's Avatar
    Join Date
    Mar 2004
    Location
    Wales
    Posts
    1,988
    The ball is only checking for colision with one bullet (the last one fired)
    Code:
    if(this.hitTest(_root["bullet"+i])) {
    "i" will just be the last value of i, and so you're only checking the last bullet.

    To fix this, you either need to do a loop in your ball code to go through all the bullets
    Code:
    for (var j=0; j<i; j++) {
    if(this.hitTest(_root["bullet"+j])) {
    //im dead
    }
    }
    or you could just put code on each bullet checking for collision with the ball instead.

    Ali

  3. #3
    Junior Member
    Join Date
    Jan 2006
    Posts
    28
    Cheers. It worked just like you said

  4. #4
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    Also, using the built-in flash hitTest functions are kind of slow. I suggest you try checking distance if you can.
    .

  5. #5
    Junior Member
    Join Date
    Jan 2006
    Posts
    28
    Hey Swak, could you tell me more about that...in a 6-year old type of manner?

  6. #6
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    Pretend that there are two movieclips mc1 and mc2.
    Code:
    var distance:Number = 0;
    onEnterFrame = function(){
    distance = Math.sqrt(Math.pow(mc2._x-mc1._x,2)+Math.pow(mc2._y-mc1._y,2));
    if(distance < 50){
    //hit
    }
    }
    this is off the top of my head the "distance =" is the main part of it. it finds out how far the two movieclips are.
    .

  7. #7
    Junior Member
    Join Date
    Jan 2006
    Posts
    28
    Thanks. Too tired to think atm. I'll get a good look at it in the morning...afternoon.

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