dcsimg
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: collision testing

Hybrid View

  1. #1
    OffTheHorseCEO RegalKnight's Avatar
    Join Date
    Jun 2002
    Location
    Who said fallin off of a horse couldn't be an inspiration
    Posts
    138

    collision testing

    HELP my freakin hittest wont work. i went under flash help and searched a bunch of places lokin for it to work but it wont. basically this is it: i have two MC's "tank" and "terrain" tank is of course the tank im making and terrain is the walls, ground, rocks, trees, etc. well ive made the walls trees and rocks solid fills and the clip "terrain" is in the layer under the one "tank" is in. what kind of actions should i assign to which clip or frame or should i move them to the same layer or what?????
    thanx for any help in advance
    Martin
    BLING!

  2. #2
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    Draw out your terrain and make sure it is turned into a movie clip, leave the areas that the tank can drive on blank. Give this clip the instance name terrain.
    On the tank clip attach a script a little like this,

    Code:
    onClipEvent(load) {
    	speed = 0;
            maxSpeed = 10;
    }
    onClipEvent(enterFrame) {
    	if (Key.isDown(Key.UP)) {
    		if (speed < maxSpeed) {
    			speed++;
    		}
    	} else if (Key.isDown(Key.DOWN)) {
    		if (speed > -maxSpeed) {
    			speed--;
    		}
    	} else { // slowly stop the tank
    		if (speed < 0) {
    			speed++;
    		} else if (speed > 0) {
    			speed--;
    		}
    	}
    	if (Key.isDown(Key.LEFT)) {
    		this._rotation -= 2;
    	} else if (Key.isDown(Key.RIGHT)) {
    		this._rotation += 2;
    	}
    	_root.move(this, speed);
    }
    Now on the main timeline add the function move

    Code:
    function move(clip, speed) {
    	var angle = (clip._rotation / 360) * 2 * Math.PI;
    	var xStep = speed * Math.sin(angle);
    	var yStep = -speed * Math.cos(angle);
    	if (!_root.terrain.hitTest(clip._x + xStep, clip._y + yStep, true)) {
     		clip._x += xStep;
    		clip._y += yStep;
    	}
    }
    this calculates the distance the clip will move in the x and y directions based on its speed and rotation (which is first converted from degrees to radians), it then checks that this move would not make it hit any of the terrain, if this is the case the clip is moved to the new position. Hope this helps

  3. #3
    OffTheHorseCEO RegalKnight's Avatar
    Join Date
    Jun 2002
    Location
    Who said fallin off of a horse couldn't be an inspiration
    Posts
    138
    hey thanx, im at school right now and even though ive got flash mx at schol i dont have the fla file so i dont kno if it'd work yet, one question though: with this script will the tank move around freely or stay in the same place while the terrain moves?
    BLING!

  4. #4
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    That script moves the tank around.

  5. #5
    OffTheHorseCEO RegalKnight's Avatar
    Join Date
    Jun 2002
    Location
    Who said fallin off of a horse couldn't be an inspiration
    Posts
    138
    thanx im home now imma try it
    BLING!

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