Be a Supporter!

AS3 Hit testing different mcs

  • 186 Views
  • 4 Replies
New Topic Respond to this Topic
Liran
Liran
  • Member since: May. 8, 2005
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
AS3 Hit testing different mcs 2014-02-10 13:53:24 Reply

Hi

I am building a game and want to check for hit testing of the player mc with other mcs on stage. All mcs are placed on the frame in the timeline.
I have different types of mcs (walls, doors, etc..). for each type of object the player hits i want to perform a different action.

I have an enter frame function on my main timeline.
In it I use this code to list all mcs on stage:

for(var i:int = 0; i<numChildren; i++)
{
    var e:DisplayObject = getChildAt(i);
    if(e is MovieClip)
    {
	
    }
}

So for each object "e" I want to hittest and based on the type of object (wall, door) i want to use a different action.
How can I distinguish different objects?

I tried to put a variable in each mc like this:

var mctype="wall"

and then use this code to check which type of mc it is:

for(var i:int = 0; i<numChildren; i++)
{
    var e:DisplayObject = getChildAt(i);
    if(e is MovieClip)
    {
        if (e.mctype=="wall") {trace("wall");}
    }
}

but it gives me an error for "a possibly undefined property mctype.

Thanks

Etherblood
Etherblood
  • Member since: Apr. 14, 2013
  • Offline.
Forum Stats
Member
Level 12
Game Developer
Response to AS3 Hit testing different mcs 2014-02-10 14:44:09 Reply

At 2/10/14 01:53 PM, Liran wrote: Hi

I am building a game and want to check for hit testing of the player mc with other mcs on stage. All mcs are placed on the frame in the timeline.
I have different types of mcs (walls, doors, etc..). for each type of object the player hits i want to perform a different action.

I have an enter frame function on my main timeline.
In it I use this code to list all mcs on stage:

for(var i:int = 0; i<numChildren; i++)
{
var e:DisplayObject = getChildAt(i);
if(e is MovieClip)
{

}
}

So for each object "e" I want to hittest and based on the type of object (wall, door) i want to use a different action.
How can I distinguish different objects?

I tried to put a variable in each mc like this:

var mctype="wall"

and then use this code to check which type of mc it is:

for(var i:int = 0; i<numChildren; i++)
{
var e:DisplayObject = getChildAt(i);
if(e is MovieClip)
{
if (e.mctype=="wall") {trace("wall");}
}
}

but it gives me an error for "a possibly undefined property mctype.

Thanks

The easiest "fix" to this concrete problem would be to change your code to sth. like this:

for(var i:int = 0; i<numChildren; i++)
{
    var e:DisplayObject = getChildAt(i);
    if(e is MovieClip && "mctype" in e) // checks if e is a MovieClip and if it contains a var or function named "mctype"
    {
        if (e["mctype"]=="wall") // checks if e's var or function named "mctype" == "wall"
            {trace("wall");}
    }
}

I strongly advise against this method though.

You should use different classes for different objects, so your code could look more like this:

for(var i:int = 0; i<numChildren; i++)
{
    var e:DisplayObject = getChildAt(i);
    if(e is Wall)
    {
        trace("wall");
    }
    else if(e is Door)
    {
        trace("door");
    } // etc
}

There's still a lot potential for improvement in regards to coding practices, but I'll leave it at that in this answer.

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to AS3 Hit testing different mcs 2014-02-10 15:58:20 Reply

First of all, getChildAt is kind of expensive.
It's a lot easier to put things into an array.

To enable different behaviours but same treatment, put the behaviours into each class.

// some pseudo code:

public class Wall
{
    public function gotHit():void
    {
        trace("wall");
    }
}

public class Door
{
    public function gotHit():void
    {
        trace("door");
    }
}

In your main loop you can now do something like:

for (element in array)
{
    if ( hit)
    {
        element.gotHit();
    }
}
Liran
Liran
  • Member since: May. 8, 2005
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to AS3 Hit testing different mcs 2014-02-11 03:36:54 Reply

@EtherBlood:
Thanks, using your code (second one) works.

@milchreis:
Should I use getChild() once to get them all into an array and then use the array on each enterframe? (Since they are placed on the timeline).
Also, If I use a gotHit() function for the Walls/Doors, in order to effect the player mc too (from inside the function), I'd probably need to send a reference to the stage like this:

gotHit(this);

function gotHit (mainStage: DisplayObjectContainer) {
 trace ("got hit");
}

Is that right?

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to AS3 Hit testing different mcs 2014-02-11 04:39:24 Reply

At 2/11/14 03:36 AM, Liran wrote: Should I use getChild() once to get them all into an array and then use the array on each enterframe? (Since they are placed on the timeline).
Also, If I use a gotHit() function for the Walls/Doors, in order to effect the player mc too (from inside the function), I'd probably need to send a reference to the stage like this:

No. If you need to affect the player, pass the player.
Why would you send the stage if that's not what you need?

Is that right?

It depends on what you want to do with the player.