Reputation: 1761
I'm currently playing around with some AS3 platform gaming, and trying to go with a cleaner object-based approach for platforms. In short, I'm trying to detect if the player object is colliding with platform - ala:
if (!player.hitTestObject(this)) {
//player falls
}
However, the problem is in actually referencing the player object - the player is at the following location (from the stage); manager.player - wheras the platforms are at manager.level.foreground.
Is there any way to actually reference the player object from within the foreground object as listed above, without passing in a constructor from the player to each and every instance of the platform?
Upvotes: 1
Views: 107
Reputation: 1891
You can get a static reference to the place that the player object is created in. In that class, you basically return a reference to the player.
Upvotes: 1
Reputation: 518
you could do
if (!this.parent.parent.player.hitTestObject(this)) {
//player falls
}
but i strongly advise against this as it is extremely inflexible to changes, and cause a possible debugging nightmare:)
would it not be neater to let the manager handle the collisions between it's children?
Upvotes: 2