-
hitTest and classes
Trying to figure out the best way to perform a hit test on some randomly moving objects...
I've got a Main class that creates 2 squares and 2 circles and adds them to the stage:
Code:
package {
import flash.display.MovieClip;
public class Main extends MovieClip
{
public function Main() {
Populate();
}
private function Populate() {
var circle1:Circle = new Circle;
this.addChild(circle1);
circle1.x = 5;
circle1.y = 5;
var circle2:NewCat = new Circle;
this.addChild(circle2);
circle2.x = 25;
circle2.y = 25;
var square1:Square= new Square;
this.addChild(square1);
square1.x = 15;
square1.y = 15;
var square2:Square= new Square;
this.addChild(square2);
square2.x = 50;
square2.y = 50;
}
}
}
Within the Circle and Square classes, I've got random movement but I need to be able to figure out when a circle touches another circle and when it touches a square and can't figure out how to get that into my other classes...
I've tried using hitTestObject to test the class of the objects using getClassObj(this) but it doesn't work. I tried looping through all the children, but it hits the Main container and doesn't detect any children collisions.
Any help would be greatly appreciated!!
:)
-
This will give you the class name of the object:
var c:Circle = new Circle();
addChild(c);
trace(getQualifiedClassName(c));
-
Thanks...I'm trying to figure out how to work a hitTest into the Circle.as class and don't know what to put on the other side of the argument...
I tried this in Circle.as:
Code:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
public class Circle extends MovieClip
{
public function Circle () {
this.addEventListener(Event.ENTER_FRAME, MoveCircle);
}
public function getClassObj(obj:*):*
{
var objClass:Class = Class(getDefinitionByName(getQualifiedClassName(obj)));
return new objClass();
}
private function MoveCircle(e:Event) {
//I've got code here to randomly move the circles and I have the same sort of thing on the squares
//I tried this for collision detection, but it doesn't work...not sure how to check to see if the circle is hitting another circle or a square
if (this.hitTestObject(getClassObj(this))) {
trace("name1 " + this.name);
}
//I tried this also to see if I could tell if it was hitting anything, but it just returns that it's hitting Main every time
if(stage){
var i:int = 0;
for(i; i < stage.numChildren; ++i){
if (this.hitTestObject(stage.getChildAt(i) && stage.getChildAt(i)) != this) {
trace("hit "+stage.getChildAt(i));
}
}
}
}
}
}
-
You are not looking for the instance name but the class id.
trace("name1 " + getQualifiedClassName(this);
-
Hmm...so, how would I work that into a hitTarget statement? :)
I guess I'm not sure how to check if an object created with one class hits an object created with another class. I'm considering re-writing the whole script to put everything in one class, but that doesn't seem very OOP "proper" and I'm trying to work on intelligently separating things out for re-usability.
Been doing LOTS of reading and going through sample code, but nothing really fits this and so much of what's out there doesn't seem very OOP centric.
Thanks for your help. :)
-
Alright...got it figured out...
I ended up building an array in the Main class to keep track of circles and squares and then put a function in that class as well to handle the collision detection.
Thanks!