Skip to main content
ogeezus
Known Participant
March 22, 2015
Question

TouchInput & keydownhandler

  • March 22, 2015
  • 1 reply
  • 428 views

Hi, I'm making a top-to-bottom shooter where you control a ship and dodge asteroids.

I've really been struggling to add touch support to move the ship up and down. I've also done a lot of searching around for youtube tutorials, but this is the best I could do.

  1.                 function keyDownHandler(keyEvent: KeyboardEvent): void {
  2.                         if (keyEvent.keyCode == 32 || keyEvent.keyCode == 39) { //fires gun
  3.                                 shootBullet();
  4.                         } else if (keyEvent.keyCode == 38 && player.y > 50) {
  5.                                 player.y -= playerMoveSpeed; //moves player down
  6.                         } else if (keyEvent.keyCode == 40 && player.y < stage.stageHeight - 50) {
  7.                                 player.y += playerMoveSpeed; //moves player up
  8.                         }
  9.                 }

Here's my touch code, which it seems to work, but I need it to match my keydownhandler

  1.                 function onTouchBegin (e:TouchEvent):void{
  2.                         player.y = e.stageY;
  3.                         trace(e.touchPointID);
  4.                 }                             
  5.                 function onTouchMove (e:TouchEvent):void{
  6.                         player.y = e.stageY;
  7.                 }                     
  8.                 function onTouchFinish (e:TouchEvent):void{
  9.                         player.y = e.stageY;
  10.                 }

So the problem here is:

a) playerMoveSpeed = 15, so keyboard players can only move at a limited speed however touch users can move with no limits

b) keydownhandler, prevents the player going off the screen while I dont think the touch code does

c) I cant seem to figure out a way to make touch users shoot, by tapping?


Thanks in advance for your help, looking forward to your reply.

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
March 22, 2015

all your problems (except b) require design decisions by you.  we're not going to be able to make those decisions for you.

for b, use a conditional just like you do your for keyboard users.

ogeezus
ogeezusAuthor
Known Participant
March 23, 2015

I'm not to fussed about the design decisions, I would be happy enough if things just worked.

So far I have managed to do b)

  1.                 function onTouchBegin(e: TouchEvent): void {
  2.                         player.y = e.stageY;
  3.                         trace(e.touchPointID);
  4.                 }
  5.                 function onTouchMove(e: TouchEvent): void {
  6.                         if (player.y > 50) {
  7.                                 player.y -= playerMoveSpeed;
  8.                                 player.y = e.stageY;
  9.                               
  10.                         } else if (player.y < stage.stageHeight - 50) {
  11.                                 player.y += playerMoveSpeed; //moves player up
  12.                                 player.y = e.stageY ;
  13.                         }
  14.                 }
  15.                 function onTouchFinish(e: TouchEvent): void {
  16.                         player.y = e.stageY;
  17.                 }


I can move the player using the touch and restrict him to the stage, however he's not limited to the playermovespeed for some reason but im not to fussed since it works.


all I want is to make the player shoot by tapping the screen, and then it would be complete, however I just dont know how to accomplish that.




kglad
Community Expert
Community Expert
March 23, 2015

you can use:

stage.addEventListener(MouseEvent.MOUSE_DOWN,downF);

function downF(e:MouseEvent):void{

shootBullet();

}