Be a Supporter!

[AS3] best concept for hittests.

  • 410 Views
  • 6 Replies
New Topic Respond to this Topic
Barzona
Barzona
  • Member since: Jan. 4, 2009
  • Offline.
Forum Stats
Member
Level 13
Game Developer
[AS3] best concept for hittests. May. 16th, 2012 @ 07:47 PM Reply

Now that I have a decent understanding of how to create and control bullets, I seem to be having trouble having them interact with enemies that are loaded onto an array. The bullets are also loaded onto an array, so is it best to create a function that uses an array to check for hittests?

When I trace my bullet move clip when I fire it, it shows [object Bullet]. Is that a normal/good thing? It's tracing the name of the actual movie clip instead of the name I gave it. Should I, then, reference the name of the movie clip rather than the custom name?

Now that I've moved from AS2 to AS3 I am beginning to value efficiency and dynamic control a lot more, so I just want to be sure I'm doing this right.

Sandremss128
Sandremss128
  • Member since: Aug. 22, 2009
  • Offline.
Forum Stats
Supporter
Level 11
Programmer
Response to [AS3] best concept for hittests. May. 16th, 2012 @ 08:02 PM Reply

If you create or override the function toString():String on an object and you do trace object you can see more detailed information than just the class name.

public class Main extends Sprite
{
	public function Main()
	{
		trace(new Name("HI")); //will trace : "[Name] HI!"
	}
}
public class Name {
	private var _message : String;
	public function Name(message : String) {
		this._message = message;
	}
	public function toString() : String {
		return "[Name] " + _message + "!";
	}
}

Nice for debugging purposes. Also for the hittest thing just check each bullet with each enemy.

stretch1one
stretch1one
  • Member since: Jul. 9, 2004
  • Offline.
Forum Stats
Member
Level 08
Artist
Response to [AS3] best concept for hittests. May. 22nd, 2012 @ 08:02 AM Reply

I just recently figured out the problem you find yourself with.

if((zombies.length > 0) && (mybullets.length > 0))
	{
		for(g = 0; g < mybullets.length; g++)
		{
			for(h = 0; h < zombies.length; h++)
			{
			if(this.zombies[h].hitTestObject(this.mybullets[g]))
			{
				container.removeChild(this.mybullets[g]);
				mybullets.splice(g,1);
                                container.removeChild(this.zombies[h]);
			        zombies.splice(h,1);
			}
			}
		}
	}

if statement to make sure theres something in the array to be checked, and if you depend on the array of enemies and a time limit to be over to end a round of something like that like im doing you'll want to remove the objects from the array too. Something like that should work for you.


BBS Signature
stretch1one
stretch1one
  • Member since: Jul. 9, 2004
  • Offline.
Forum Stats
Member
Level 08
Artist
Response to [AS3] best concept for hittests. May. 22nd, 2012 @ 08:04 AM Reply

I worded that wrong, I meant to say if you depend on the enemy array to be empty as well as the time being over, anyway, good luck with your programming


BBS Signature
lobsterhat
lobsterhat
  • Member since: May. 4, 2012
  • Offline.
Forum Stats
Member
Level 05
Programmer
Response to [AS3] best concept for hittests. May. 22nd, 2012 @ 11:56 AM Reply

At 5/22/12 08:02 AM, stretch1one wrote: I just recently figured out the problem you find yourself with.

if((zombies.length > 0) && (mybullets.length > 0))
{
for(g = 0; g < mybullets.length; g++)
{
for(h = 0; h < zombies.length; h++)
{
if(this.zombies[h].hitTestObject(this.mybullets[g]))
{
container.removeChild(this.mybullets[g]);
mybullets.splice(g,1);
container.removeChild(this.zombies[h]);
zombies.splice(h,1);
}
}
}
}

if statement to make sure theres something in the array to be checked, and if you depend on the array of enemies and a time limit to be over to end a round of something like that like im doing you'll want to remove the objects from the array too. Something like that should work for you.

Your loop is going to skip an element each time your hittest is ttrue, I think. You are removing an element and shifting the remaining elements in the array to the left. Your iterator is now pointing at the element that was following the one you just removed. This element will not be checked as the iterator will increment before the next collision check.

I think you'll be better off if you go through your loops backward:

for(g = mybullets.length; g >= 0; g--)
{
    for(h = zombies.length; h >= 0;  h--)
    {
         if(this.zombies[h].hitTestObject(this.mybullets[g]))
         {
              //do stuff
         }
    }
}
lobsterhat
lobsterhat
  • Member since: May. 4, 2012
  • Offline.
Forum Stats
Member
Level 05
Programmer
Response to [AS3] best concept for hittests. May. 22nd, 2012 @ 12:05 PM Reply

It that should be length - 1 not length.

And also, you should break out of your zombie loop when hit test is true.

stretch1one
stretch1one
  • Member since: Jul. 9, 2004
  • Offline.
Forum Stats
Member
Level 08
Artist
Response to [AS3] best concept for hittests. May. 22nd, 2012 @ 08:18 PM Reply

At 5/22/12 12:05 PM, lobsterhat wrote: It that should be length - 1 not length.

And also, you should break out of your zombie loop when hit test is true.

well, I have it set up differently in my game, since i gave them a health them being removed is independent of the hitTest, sorry if I pointed him in the wrong direction.


BBS Signature