ForumsProgramming Forumcollision detection

7 3440
rino
offline
rino
27 posts
Nomad

I have found this code in as2 and I am trying to make it have collision detection, with objects of different shapes but have been unable to do so. Does anyone know how to do this.

onClipEvent (load) {
theta = 0;
thrust = 0;
angle_inc = 5;
vx = 0;
vy = 0;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.UP)) {
thrust = .1;
} else {
thrust = 0;
}
if (Key.isDown(Key.LEFT)) {
theta = theta-angle_inc;
} else if (Key.isDown(Key.RIGHT)) {
theta = theta+angle_inc;
}
if (Key.isDown(Key.DOWN)) {
thrust -= .1;

}
vx += thrust*Math.cos(theta/180*Math.PI);
vy += thrust*Math.sin(theta/180*Math.PI);
this._x += vx;
this._y += vy;

this._rotation = theta;
}

  • 7 Replies
plasmafish
offline
plasmafish
252 posts
Nomad

onClipEvent (enterFrame) {
if (this.hitTest(_root.item)) {
CODE GOES HERE
}
}

Do you understand how to use this simple code?

rino
offline
rino
27 posts
Nomad

yes and i have tried this, but it does not serve the purpose of what is trying to be done.

do you understand what is trying to be done?

plasmafish
offline
plasmafish
252 posts
Nomad

Yep, looks like Asteroid code to me.

plasmafish
offline
plasmafish
252 posts
Nomad

Maybe you need to break apart the objects and refine the edges.

ZeroComp
offline
ZeroComp
383 posts
Nomad

I have tried it and it does seem to work but the one plasmafish wrote didn't work

plasmafish
offline
plasmafish
252 posts
Nomad

The one I wrote was taken directly from a movie I had made. You have to apply it to the movie clip that you want to interact with the moving character/object. _root.item would be changed to something like bullet or collideobject, _root is not needed in all scenarios, I just used it in my case.

Where it says CODE GOES HERE, you would pass variables and do whatever you need the collision to do (applying to the first set of code also if needed).

JustScott
offline
JustScott
96 posts
Nomad

it never works for me :<

Showing 1-7 of 7