-
OffTheHorseCEO
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
-
Senior Member
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
-
OffTheHorseCEO
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?
-
Senior Member
That script moves the tank around.
-
OffTheHorseCEO
thanx im home now imma try it
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|