Copy link to clipboard
Copied
Hello all
i have made a road crossing game with keyboard controls to move the objects. the game was actually developed for desktop. now, i have published a .apk version and it works well on android devices, except for the fact that i am not able to use the Keyboard Keys to make the objects move. Can anybody please tell me if there is any way i can overcome this problem. Is there any way that i can make the Up, Down, Left and Right keys (virtual keys) appear on the game screen and use them as if like i was using the keyboard keys.
thanks in advance
Copy link to clipboard
Copied
are you using a keyboardevent listener for the stage?
and what's the problem using virtual keys (actually interactiveobjects)?
Copy link to clipboard
Copied
@kglad
Sir, thank you for your willingness to help
For my Desk Top version of the game i used a Key Board Event Listener, but not for the stage.
For my Android version of the game i used a Mouse Event Listener, since i do not have access to a keyboard anymore.
This is the code that i used for the Desk Top version. (Letters A to Z are movie clips icons that we have to make them cross from one side of the road to the other without being hit by the vehicles).
letterA.addEventListener(KeyboardEvent.KEY_DOWN, moveLetA);
function moveLetA(e:KeyboardEvent):void{
if (e.keyCode == Keyboard.RIGHT){ letterA.x +=4;}
if (e.keyCode == Keyboard.LEFT){ letterA.x -=4;}
if (e.keyCode == Keyboard.UP){ letterA.y -=4;}
if (e.keyCode == Keyboard.DOWN){ letterA.y +=4;}
}
letterA.addEventListener(KeyboardEvent.KEY_UP, stopLetA);
function stopLetA(e:KeyboardEvent):void{
letterA.stopDrag();
}
For the android version of the game since i could not use the Keyboard Keys anymore, i created four arrow key movie clips (up, down, right and left and gave them the instance names of rightK, leftK, upK, downK) thinking that if i continued to press down on each of these keys it would move the target object "continuously" in that direction. But when i attempt this, it moves the object by only 4 pixels for each press. So i have to press (tap) repeatedly to make the object move continuously, 4 pixels for each tap.
This is the code that i tried for the android version of the game:
stage.addEventListener(MouseEvent.MOUSE_DOWN, movLetA);
function movLetA(e:MouseEvent):void{
if (e.target == rightK){letterA.x +=4;}
if (e.target == leftK){letterA.x -=4;}
if (e.target == upK){letterA.y -=4;}
if (e.target == downK){letterA.y +=4;}
}
In case my question was not properly phrased, i would like to kindly mention it once again. What i would like to know is , "if there is any way that i can make four
directional movie clips (eg., Up, Down, Right & Left) and write the code in such a way that, when i press and hold down on any of these movie clips, it would simulate the function of a Keyboard Event and continue to move the target object in the respective direction".
with best regards
shams
Copy link to clipboard
Copied
use your mousedown event to start an enterframe loop that moves your movieclip and use a mouseup event to stop the enterframe loop.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now