View Full Version : [AS3] Collision HitBox
Ciubhran
03-04-2009, 12:10 PM
I need to make a collision hitbox for one of my movieclips, because some of its minor parts get stuck easily.
How would you advice one would make a hitbox like this, and how would you access it for the HitTestObject-call?
aaron_da_killa
03-05-2009, 03:18 AM
I don't know what you mean buddy.
Ciubhran
03-05-2009, 09:03 AM
The shape of my current MovieClip is too large, and has "edges" that bump into things that I want them to ignore.
So I want to make a new "entity" in the MovieClip which the collision detection is made against, that has no edges.
aaron_da_killa
03-05-2009, 09:44 AM
The shape of my current MovieClip is too large, and has "edges" that bump into things that I want them to ignore.
So I want to make a new "entity" in the MovieClip which the collision detection is made against, that has no edges.
Cool, so you have per pixel collision detection or something?
Anyway, there are several ways you can do this. Instead of just saying like:
if movieclip.y = whatever etc
you can say
if movieclip.y - 20 = whatever
You could also put another movieclip inside your movie clip that you currently have and use that embedded movieclip instead for collison detection.
Ciubhran
03-05-2009, 10:05 AM
Cool, so you have per pixel collision detection or something?
Anyway, there are several ways you can do this. Instead of just saying like:
if movieclip.y = whatever etc
you can say
if movieclip.y - 20 = whatever
You could also put another movieclip inside your movie clip that you currently have and use that embedded movieclip instead for collison detection.
It's exactly the latter of your examples that I am looking for. Do you have a AS3 code example? Or instructions?
szataniol
03-05-2009, 10:12 AM
Get familiar with Box2DFlashAs3
aaron_da_killa
03-05-2009, 10:18 AM
If you have Adobe Flash or Macromedia Mx and such, what you simply do is doubleclick the existing movieclip than create the new movieclip inside that and give the new movieclip an instance name.
you call the new movieclip like this:
oldMovieClip.newMovieClip.y = 10;
However, I must warn you that now that you have a movieclip inside a movieclip, you need to use localToGlobal if you want to use newMovieClip's position in regards to the stage.
oldMovieClip.y = the number of pixels the top left of oldMovieClip is from the top of the stage.
oldMovieClip.newMovieClip.y = the number of pixels the top left of newMovieClip is from the top of OldMovieClip.
If you want to know newMovieClips position in regards to the stage than you do this:
var newMovieClipPoint:Point = new Point(oldMovieClip.newMovieClip.x, oldMovieClip.newMovieClip.y);
newMovieClipPoint.y = oldMovieClip.localToGlobal(platformPoint).y;
newMovieClipPoint.x = oldMovieClip.localToGlobal(platformPoint).x;
So now if you want to use the newMovieClip from the stage you use newMovieClipPoint instead. Example:
newMovieClipPoint.y = etc etc
because:
Ciubhran
03-05-2009, 03:12 PM
A slight problem. When I change the instance name of the new MovieClip, it changes the instance name of the old MovieClip, and vice versa.
(Might not be correct. In the library it says they have the same instance name, but when I double-click the old one and check the new one there, then it has the correct instance name.)
Hoever, I get a "null reference" error when trying to do thing with the new movie clip.
function CheckCollision():Boolean {
for(var g:Number = 0; g < mineral_vector.length; g++) {
if(Hero.HitBox.hitTestObject(mineral_vector[g])) {
return true;
}
}
return false;
}
Ciubhran
03-05-2009, 03:36 PM
I found out what screws it up.
The MovieClip I added it to, has many different animation frames. And then new MovieClip only exists in the first one. How do I get it to appear in all the frames?
Ciubhran
03-05-2009, 07:19 PM
Fixed it by adding a Sprite to the Hero Class, called HitBox. Works like a charm.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.