Adobe Animate - How do I programme two players in a top down view game
Hey guys and girls
I have been struggling for the past while as a beginner at Animate and was wondering if anyone could help. I am creating a game much like soccer/football only 2D and from above. My trouble is that I want to make the game two player using the same keyboard, where one player uses WASD and the other uses the arrow keys. Using this code I have managed to make one of the players (Quadcopter_Red) move using the arrow keys and have tried to do the same with the other player (Quadcopter_Blue) but when I test it, it only moves one of the players with both keys (WASD and arrows). can anyone tell me if I am doing something wrong and if so correct me.
PLAYER 1
var WPressed:Boolean = false;
var SPressed:Boolean = false;
var APressed:Boolean = false;
var DPressed:Boolean = false;
Quadcopter_Red.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey_2);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed_2);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed_2);
function fl_MoveInDirectionOfKey_2(event:Event)
{
if (WPressed)
{
Quadcopter_Red.y -= 5;
}
if (SPressed)
{
Quadcopter_Red.y += 5;
}
if (APressed)
{
Quadcopter_Red.x -= 5;
}
if (DPressed)
{
Quadcopter_Red.x += 5;
}
}
function fl_SetKeyPressed_2(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.W:
{
upPressed = true;
break;
}
case Keyboard.S:
{
downPressed = true;
break;
}
case Keyboard.A:
{
leftPressed = true;
break;
}
case Keyboard.D:
{
rightPressed = true;
break;
}
}
}
function fl_UnsetKeyPressed_2(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.W:
{
upPressed = false;
break;
}
case Keyboard.S:
{
downPressed = false;
break;
}
case Keyboard.A:
{
leftPressed = false;
break;
}
case Keyboard.D:
{
rightPressed = false;
break;
}
}
}
Player 2
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
Quadcopter_Blue.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey_1);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed_1);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed_1);
function fl_MoveInDirectionOfKey_1(event:Event)
{
if (upPressed)
{
Quadcopter_Blue.y -= 5;
}
if (downPressed)
{
Quadcopter_Blue.y += 5;
}
if (leftPressed)
{
Quadcopter_Blue.x -= 5;
}
if (rightPressed)
{
Quadcopter_Blue.x += 5;
}
}
function fl_SetKeyPressed_1(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = true;
break;
}
case Keyboard.DOWN:
{
downPressed = true;
break;
}
case Keyboard.LEFT:
{
leftPressed = true;
break;
}
case Keyboard.RIGHT:
{
rightPressed = true;
break;
}
}
}
function fl_UnsetKeyPressed_1(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = false;
break;
}
case Keyboard.DOWN:
{
downPressed = false;
break;
}
case Keyboard.LEFT:
{
leftPressed = false;
break;
}
case Keyboard.RIGHT:
{
rightPressed = false;
break;
}
}
}
Thank you!!!