Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Adobe Animate - How do I programme two players in a top down view game

Community Beginner ,
Aug 07, 2018 Aug 07, 2018

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!!!

316
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Aug 08, 2018 Aug 08, 2018

Hi.

It's a typo problem. In the player 1 code, you check for the WASD keys, but you actually set the arrows keys.

For example here:

case Keyboard.W:

{

    upPressed = true;

    break;

}

Anyway, here is a suggestion for you. Notice that you don't need to add 6 listeners. Only add 3 to the stage and you're good to go.

var WPressed:Boolean = false;

var SPressed:Boolean = false;

var APressed:Boolean = false;

var DPressed:Boolean = false;

var upPressed:Boolean = false;

var downPressed:Boolean = false;

var leftPress

...
Translate
Community Expert ,
Aug 08, 2018 Aug 08, 2018

Hi.

It's a typo problem. In the player 1 code, you check for the WASD keys, but you actually set the arrows keys.

For example here:

case Keyboard.W:

{

    upPressed = true;

    break;

}

Anyway, here is a suggestion for you. Notice that you don't need to add 6 listeners. Only add 3 to the stage and you're good to go.

var WPressed:Boolean = false;

var SPressed:Boolean = false;

var APressed:Boolean = false;

var DPressed:Boolean = false;

var upPressed:Boolean = false;

var downPressed:Boolean = false;

var leftPressed:Boolean = false;

var rightPressed:Boolean = false;

function fl_SetKeyPressed_2(event:KeyboardEvent):void

{

     switch (event.keyCode)

     {

          case Keyboard.W:

          {

               WPressed = true;

               break;

          }

          case Keyboard.S:

          {

               SPressed = true;

               break;

          }

          case Keyboard.A:

          {

               APressed = true;

               break;

          }

          case Keyboard.D:

          {

               DPressed = true;

               break;

          }

          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_2(event:KeyboardEvent):void

{

     switch (event.keyCode)

     {

          case Keyboard.W:

          {

              WPressed = false;

              break;

          }

          case Keyboard.S:

          {

               SPressed = false;

               break;

          }

          case Keyboard.A:

          {

               APressed = false;

               break;

          }

          case Keyboard.D:

          {

               DPressed = false;

               break;

          }

          case Keyboard.UP:

          {

               upPressed = false;

               break;

          }

          case Keyboard.DOWN:

          {

              downPressed = false;

               break;

          }

          case Keyboard.LEFT:

          {

               leftPressed = false;

               break;

          }

          case Keyboard.RIGHT:

          {

               rightPressed = false;

               break;

          }

     }

}

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;

     }

     if (upPressed)

     {

          Quadcopter_Blue.y -= 5;

     }

     if (downPressed)

     {

          Quadcopter_Blue.y += 5;

     }

     if (leftPressed)

     {

          Quadcopter_Blue.x -= 5;

     }

     if (rightPressed)

     {

          Quadcopter_Blue.x += 5;

     }

}

stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed_2);

stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed_2);

stage.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey_2);

I hope this helps.

Regards,

JC

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 08, 2018 Aug 08, 2018

Hi JC

I just wanted to thank you for the help with my coding. It's all working perfectly now. Thank you!

Kind Regards

Austin

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 09, 2018 Aug 09, 2018
LATEST

You're welcome!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines