-
Hittest any other object
I'm making a game, and I need for my character to stop moving if it hits any instance of the Object class. That means everything. I don't want to have to write a hittest check for every single object on my stage, but everything in general.
In pseudocodeish syntax, this would be represented as
if (hitTestObject(AnyOtherObjectInGame))
{
stopMoving();
}
I need to know what i'd put in for 'AnyOtherObjectInGame' so that I wouldn't have to go
if (hitTestObject(tree1))
{
stopMoving();
}
if (hitTestObject(tree2))
{
stopMoving();
}
if (hitTestObject(randomobject1))
{
stopMoving();
}
etc
Thanks
-
newb of many sorts
You'll need to do a loop...
For this example, I have 4 trees on the stage, and a character called "bob"
Code:
var myArrayOfThings:Array = new Array();
myArrayOfThings.push(tree1);
myArrayOfThings.push(tree2);
myArrayOfThings.push(tree3);
myArrayOfThings.push(tree4);
for(var s in myArrayOfThings){
var o:DisplayObject = myArrayOfThings[s];
if(o != bob){
trace(bob.hitTestObject(o));
}
}
All you need to do is stick your objects into the array and then run the loop. In this case, you would want to slap it into a function though...
Code:
function bobHit(a:Array):Boolean
{
for(var s in a){
var o:DisplayObject = a[s];
if(o != bob){
if(bob.hitTestObject(o)){
return true;
}
}
}
return false;
}
so now you could call the function to see if its a hit or not...
Code:
if(bobHit(myArrayOfThings)){
stopMoving();
}
Search first, asked questions later.
-
Thanks, I was thinking of doing it through an array like that, but is there a way to just push all object on the stage into the array? There's going to be a LOT of objects and I don't want to have to push every single one....
-
Senior Member
I need for my character to stop moving if it hits any instance of the Object class. That means everything.
even strings and numbers?
-
newb of many sorts
assuming that they are truly on the stage...
Code:
for(var i:uint=0; i<stage.numChildren; i++){
myArrayOfThings.push(stage.getChildAt(i));
}
If they're not directly children of the stage, then replace "stage" with the tree's parent container.
Search first, asked questions later.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|