Search code examples
flashactionscript-3parent-childcollision-detection

how to access a different movieclip within the flash in AS3


I've been trying to learn Action Script 3 the past few weeks, making tiny interactive games to learn the basics. I stumble upon a problem every now and then but most of the times google helps me out.

But this problem has got me stuck so please help:

The main stage contains two objects(movieclips), the player and a wall. The player has got his own code so when I drag in the player object I don't have to write any code into the main stage to be able to move the player.

This all worked pretty well and I now wanted to add the wall so the player actually has something to bounce into.

Now here is the problem, I want to check if the player touches the wall, I've done this before but that was when I used the main stage as my coding playground instead of putting the code in movieclips. How can I check if the player hits the wall within the movement code of the player object?


Solution

  • There are many ways to access your objects, here is one possible solution:

    var wall:MovieClip = this.parent.getChildByName("wall") as MovieClip;
    

    I presume that you have a player movieClip, added to stage directly (no containers) and the code goes inside this object. Wall object must have the instance name of "wall".

    EDIT: about the collision you can do it using hitTestObject, example:

    var wall:MovieClip = this.parent.getChildByName("wall") as MovieClip;
    trace("check collision: "+this.hitTestObject(wall));