I'm working on a small flash game and am currently trying to implement multiplayer for up to 4 players. While the collision detection for 1 player works perfectly, when more players are added only the last player can collide with other objects.
Using trace statements I discovered that calling the x and y coordinates of the problematic players from the main class returns the initial x and y positions and not the current coordinates (trace(players[0].x + "/" + players[0].y);), while calling them from within the player class (trace(this.x + "/" + this.y);) always gives the correct values.
The last player will always return the correct coordinates in both classes. Below is a skeleton of the main class.
public class main extends MovieClip {
public var collisionObject: Array = new Array(14);
public var players: Array = new Array();
private var noOfPlayers = 2;
public function main() {
for (var i = 0; i < noOfPlayers; i++) {
setupPlayer(i);
stage.addChild(players[i]);
}
setupCollisionObject();
stage.addEventListener(Event.ENTER_FRAME, checkForCollision);
}
private function setupCollisionObject() {
/* Determines positions of and spawns objects */
}
private function setupPlayer(playerNo) {
switch (playerNo) {
case 3:
players[3] = new player(1000, 576, 180, 104, 100, 102);
case 2:
players[2] = new player(24, 576, 0, 73, 74, 76);
case 1:
players[1] = new player(1000, 384, 180, 38, 37, 39);
case 0:
players[0] = new player(24, 384, 0, 87, 65, 68);
}
}
public function checkForCollision(e: Event) {
trace("x: "+players[0].x+" y: "+players[0].y);
trace("x: "+players[1].x+" y: "+players[1].y);
for (var i in players) {
for (var j in collisionObject) {
if (players[i].hitTestObject(collisionObject[j])) {
//damage player
}
}
}
}
}
I'm at a loss of why this is happening.