A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Hittest any other object

  1. #1
    Member
    Join Date
    Apr 2009
    Posts
    37

    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

  2. #2
    newb of many sorts Ralgoth's Avatar
    Join Date
    Apr 2002
    Posts
    466
    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.

  3. #3
    Member
    Join Date
    Apr 2009
    Posts
    37
    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....

  4. #4
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    I need for my character to stop moving if it hits any instance of the Object class. That means everything.
    even strings and numbers?
    who is this? a word of friendly advice: FFS stop using AS2

  5. #5
    newb of many sorts Ralgoth's Avatar
    Join Date
    Apr 2002
    Posts
    466
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center