Hittest object from a class

Hi

this is what i am trying to achieve , i have a bike and two boxes on stage , i want the boxes to disappear when the bike touches the boxes, i can achieve this while doing coding from frame by simply doing this

////////////
box1.hitTestObject(bike);
////////////////////

but when i try and do the same from class its not working , here is code from my main class

////////////////////
package
{
import wck.WCK;
import flash.display.*;
import flash.events.*;
public class Main extends WCK
{

var hit1:MyRectabgle=new MyRectabgle();
var hit2:MyRectabgle=new MyRectabgle();
var hero:Vehicle=new Vehicle();
var myArray=new Array(hit1,hit2);

public function Main()
{
hero.addEventListener(Event.ENTER_FRAME, EnterFrame1);
// hero.addEventListener(Event.ENTER_FRAME, Collision);
}

/////////////////////////
function EnterFrame1(event:Event):void {
for (var i:Number = 0; i < myArray.length; i++){
if (hero.hitTestObject(myArray[i])){
//trace("done");
removeChild(myArray[i]);
}
/////////////////////
}
}
}
}
//////////////////////

Its giving error at removechild , also i am trying call boxes here by giving them instance name hit1 and hit2, any help will be appreciated ....

Regards
Jin
any help pls .... also my main class extends WCK instead of MovieClip .. could that be the reason i need to do things differently ....I mean the problem is can any one help me to do hittest inside the physics world ....and on hittest remove object that got hit from the stage, is there any manual regarding this

Jin
Well, what is the exact error & stack trace you are getting?

I've never seen anyone use hitTestObject with Box2D... you'll definitely want to use contact events for better collision detection.
@mayo- thanks can i find some reference on contact listeners at wck ...

Jin
This webpage has a few good examples why hitTestObject only works in some cases: http://sierakowski.eu/list-of-tips/39-c ... tives.html

There are a few solutions there but box2d already has this built in. In other word, why reinvent the wheel when mayobutter has already created the Circle class 😉

In the WCK demo file, you can find 'BoxMan.as' the code to detect collisions.

This sets up the collision check:
Code:
public var contacts:ContactList;
      
      public override function create():void {
         reportBeginContact = true;
         reportEndContact = true;
         fixedRotation = true;
         super.create();
         listenWhileVisible(world, StepEvent.STEP, parseInput, false, 10);
         listenWhileVisible(this, ContactEvent.BEGIN_CONTACT, handleContact);
         contacts = new ContactList();
         contacts.listenTo(this);
      }

and this part is where collision has occured, do something:
Code:
public function handleContact(e:ContactEvent):void {
         var p:Pellet = e.other.m_userData as Pellet;
         if(p) {
            Util.addChildAtPosOf(world, new FX1(), p); 
            p.remove();
         }
      }

If you want to detect a collision with a box in your game:
Quote:
var v:MyBox= e.other.m_userData as MyBox;
Hi istavn- this is what i have done i have set up a world with bike inside , like below

Image

Now if i were to give the class name vehicle to the bike and create separate front wheel and backwheel and body part as in demo given the vehicle moves, as you know that vehicle class extends scroller while boxman class extends box, in this case the collision is not getting detected now if i were to remove the parts and create body of bike as one and say extend it by BoxMan like below ...

Image

I was first getting FX1 undefiend error then i converted it to FX, then i got no error but the body just does not move properly ....and also collision is still not getting detected , i changed some code like this...

//////////////////////
var p:BoxMan = e.other.m_userData as BoxMan;
if(p) {
Util.addChildAtPosOf(world, new FX(), p);
p.remove();
}
///////////////////////

But still not working properly, any suggestions as to where i might be going wrong ....

Thanks again

Jin
For now you can ignore Util.addChildAtPosOf(world, new FX(), p); . That code is used to create the visual flash when the Boxman picks up a pellet.

Maybe there's a better way, as I've only done a few tests, but creating separate classes for each part that you want to check collisions for (i.e. Wheel, Body) worked for me.

To set up the proper linkage, look at the Vehicle in Flash: VehicleBody and Circle20.

Original thread: https://box2d.org/forum/viewtopic.php?t=8838