PDA

View Full Version : hitTestObject


The Little Guy
11-02-2009, 11:16 PM
I would like to do a hit test, so I am making a class that can handle hit tests.

This class will (so far):
- Hold an array of all the items to test the current item against
- check for hit test

I have a method that adds the item to the array, and it looks like this:

public function add_hit(hit){
solids.push(hit);
}

Next I use this to check for hit tests:

public function enterFrame(e:Event){
for(var i=0;i<this.solids.length;i++){
if(this.hitTestObject(solids[i])){
trace("collision");
}
}
}

The question I have, can I do hit tests on an object instead of an instance? So lets say you have a player that is a circle, and you have 100 wall objects that are squares, and you would like to do a hit test, can I do a hit test to check if player hits the wall object, and not the instance wall?

Let me know if that is confusing!

Thanks

henke37
11-03-2009, 02:58 PM
Uhm object is a synonym for instance.

The Little Guy
11-03-2009, 05:26 PM
are you sure?

Isn't Object the main class, and instance a duplicate of the class?

If you ever have used the software "Game Maker" you create an object which is in your list (similar to the library in flash), and a room (similar to stage in flash). When you create your object, you give that object properties from a list, like bounce off other objects, move in a certain direction, jump to new location, etc. You then add that object to your room (stage), and all those objects then have the same properties as the original, you don't need to write separate properties for each object, you just write it for one and it applies to them all.

Is there anyway do do this?

So for my example above I wan't to do a hitTest on an Object and not instance, that way when ever obect1 hits instance2 or instance3 something happens no matter what, even if I didn't specify a hit test on instance2 or instance3, but since I did on object2 (a.k.a. instance2 and instance3) it will take affect on both.

Does that make sense (I think the last paragraph may be confusing :cool:))?

omgnoseat
11-06-2009, 01:04 PM
I too believe that there is no difference between an object and an instance in flash.

I have a question about hittests too.
when 2 items hittest eachother I want to trace the name of the hittested targetobject.

Something like this
[as]
if(chooser.hitTestObject(item)){
trace(hittest.target.name);
//trace the name of the item
}

Any help would be appreciated.

Mazoonist
11-06-2009, 01:30 PM
Rather than making the Player class do a hitTest on the array of wall objects, another approach would be to have the Wall class have an enter frame event that checks for a hitTest against the player instance. Since there is (presumably) only one player, this is simpler--no more need for an array of wall objects. Each instance of Wall would continually check for a hitTest on the player instance.

Of course you would have to devise some way that each Wall object would get a reference to the player.

omgnoseat
11-09-2009, 10:36 AM
Rather than making the Player class do a hitTest on the array of wall objects, another approach would be to have the Wall class have an enter frame event that checks for a hitTest against the player instance. Since there is (presumably) only one player, this is simpler--no more need for an array of wall objects. Each instance of Wall would continually check for a hitTest on the player instance.

Of course you would have to devise some way that each Wall object would get a reference to the player.

The problem is that when you have multiple shapes in moveclip, the hittest area is the space between all the shapes.
Kind of hard to explain, but imagine that you have 2 rectangles on x:0 and the other one on x: 100. When you place those 2 rectangles in a movieclip to hittest those rectangles, the whole area between x:0 and x: 100 will be seen as a hittest area for the movieclip.

Any idea for my question aswell?

when 2 items hittest eachother I want to trace the name of the hittested targetobject.

Something like this
[as]
if(chooser.hitTestObject(item)){
trace(hittest.target.name);
//trace the name of the item
}