Skip to main content
Known Participant
September 2, 2013
Answered

how to make a touch movement system

  • September 2, 2013
  • 1 reply
  • 1979 views

hi

im working on a touch movment system where you have a button for left, a button for right and a jump i have it working with keyboard but how would i make it use buttons instead of keys. im using timeline coding for this

var leftPressed:Boolean = false;

var rightPressed:Boolean = false;

var upPressed:Boolean = false;

var downPressed:Boolean = false;

var leftBumping:Boolean = false;

var rightBumping:Boolean = false;

var upBumping:Boolean = false;

var downBumping:Boolean = false;

var leftBumpPoint:Point = new Point(-30, -55);

var rightBumpPoint:Point = new Point(30, -55);

var upBumpPoint:Point = new Point(0, -69);

var downBumpPoint:Point = new Point(0, 0);

var scrollX:Number = 0;

var scrollY:Number = 500;

var xSpeed:Number = 0;

var ySpeed:Number = 0;

var speedConstant:Number = 4;

var frictionConstant:Number = 0.9;

var gravityConstant:Number = 1.8;

var jumpConstant:Number = -35;

var maxSpeedConstant:Number = 18;

var doubleJumpReady:Boolean = false;

var upReleasedInAir:Boolean = false;

var keyCollected:Boolean = false;

var doorOpen:Boolean = false;

var currentLevel:int = 1;

stage.addEventListener(MouseEvent.KEY_DOWN, keyDownHandler);

stage.addEventListener(MouseEvent.KEY_UP, keyUpHandler);

stage.addEventListener(Event.ENTER_FRAME, loop);

function loop(e:Event):void{

if(back.collisions.hitTestPoint(player.x + leftBumpPoint.x, player.y + leftBumpPoint.y, true)){

//trace("leftBumping");

leftBumping = true;

} else {

leftBumping = false;

}

if(back.collisions.hitTestPoint(player.x + rightBumpPoint.x, player.y + rightBumpPoint.y, true)){

//trace("rightBumping");

rightBumping = true;

} else {

rightBumping = false;

}

if(back.collisions.hitTestPoint(player.x + upBumpPoint.x, player.y + upBumpPoint.y, true)){

//trace("upBumping");

upBumping = true;

} else {

upBumping = false;

}

if(back.collisions.hitTestPoint(player.x + downBumpPoint.x, player.y + downBumpPoint.y,

true)){

//trace("downBumping");

downBumping = true;

} else {

downBumping = false;

}

if(leftPressed){

xSpeed -= speedConstant;

player.scaleX = -1;

} else if(rightPressed){

xSpeed += speedConstant;

player.scaleX = 1;

}

/*if(upPressed){

ySpeed -= speedConstant;

} else if(downPressed){

ySpeed += speedConstant;

}*/

if(leftBumping){

if(xSpeed < 0){

xSpeed *= -0.5;

}

}

if(rightBumping){

if(xSpeed > 0){

xSpeed *= -0.5;

}

}

if(upBumping){

if(ySpeed < 0){

ySpeed *= -0.5;

}

}

if(downBumping){ //if we are touching the floor

if(ySpeed > 0){

ySpeed = 0; //set the y speed to zero

}

if(upPressed){ //and if the up arrow is pressed

ySpeed = jumpConstant; //set the y speed to the jump constant

}

//DOUBLE JUMP

if(upReleasedInAir == true){

upReleasedInAir = false;

}

if(doubleJumpReady == false){

doubleJumpReady = true;

}

} else { //if we are not touching the floor

ySpeed += gravityConstant; //accelerate downwards

//DOUBLE JUMP

if(upPressed == false && upReleasedInAir == false){

upReleasedInAir = true;

//trace("upReleasedInAir");

}

if(doubleJumpReady && upReleasedInAir){

if(upPressed){ //and if the up arrow is pressed

//trace("doubleJump!");

doubleJumpReady = false;

ySpeed = jumpConstant; //set the y speed to the jump constant

}

}

}

if(keyCollected == false){

if(player.hitTestObject(back.other.doorKey)){

back.other.doorKey.visible = false;

keyCollected = true;

trace("key collected");

}

}

if(doorOpen == false){

if(keyCollected == true){

if(player.hitTestObject(back.other.lockedDoor)){

back.other.lockedDoor.gotoAndStop(2);

doorOpen = true;

trace("door open");

}

}

}

if(xSpeed > maxSpeedConstant){ //moving right

xSpeed = maxSpeedConstant;

} else if(xSpeed < (maxSpeedConstant * -1)){ //moving left

xSpeed = (maxSpeedConstant * -1);

}

xSpeed *= frictionConstant;

ySpeed *= frictionConstant;

if(Math.abs(xSpeed) < 0.5){

xSpeed = 0;

}

scrollX -= xSpeed;

scrollY -= ySpeed;

back.x = scrollX;

back.y = scrollY;

sky.x = scrollX * 0.2;

sky.y = scrollY * 0.2;

}

function keyDownHandler(e:KeyboardEvent):void{

if(e.keyCode == Keyboard.LEFT){

leftPressed = true;

} else if(e.keyCode == Keyboard.RIGHT){

rightPressed = true;

} else if(e.keyCode == Keyboard.UP){

upPressed = true;

} else if(e.keyCode == Keyboard.DOWN){

downPressed = true;

if(doorOpen && player.hitTestObject(back.other.lockedDoor)){

//proceed to the next level if the player is touching an open door

nextLevel();

}

}

}

function keyUpHandler(e:KeyboardEvent):void{

if(e.keyCode == Keyboard.LEFT){

leftPressed = false;

} else if(e.keyCode == Keyboard.RIGHT){

rightPressed = false;

} else if(e.keyCode == Keyboard.UP){

upPressed = false;

} else if(e.keyCode == Keyboard.DOWN){

downPressed = false;

}

}

This topic has been closed for replies.
Correct answer kglad

use mousedown and mouseup eventlisteners and assign those booleans in the listener functions:

btnLeft.addEventListener(MouseEvent.MOUSE_DOWN,downF);

btnLeft.addEventListener(MouseEvent.MOUSE_UP,upF);

btnLeft.bool=leftPressed;  // if you use movieclip buttons

etc

function downF(e:MouseEvent):void{

e.currentTarget.bool=true;  // you might need to cast e.currentTarget as a MovieClip

}

function upF(e:MouseEvent):void{

e.currentTarget.bool=false;  // may need to cast

}

1 reply

kglad
kgladCorrect answer
Adobe Expert
September 2, 2013

use mousedown and mouseup eventlisteners and assign those booleans in the listener functions:

btnLeft.addEventListener(MouseEvent.MOUSE_DOWN,downF);

btnLeft.addEventListener(MouseEvent.MOUSE_UP,upF);

btnLeft.bool=leftPressed;  // if you use movieclip buttons

etc

function downF(e:MouseEvent):void{

e.currentTarget.bool=true;  // you might need to cast e.currentTarget as a MovieClip

}

function upF(e:MouseEvent):void{

e.currentTarget.bool=false;  // may need to cast

}

Known Participant
September 4, 2013

Thanks that helped!

So now I have three buttons one for left, right and jump but I can only press one at a time when I try it on a mobile device what would I have to put to make me able to move and jump at the same time.

kglad
Adobe Expert
September 8, 2013

yes if i put that what do i need to put in the functions because if i put rightPressed = true; leftPressed = true; upPressed = true; they all act at once but i only want them to act when i press the buttons

but if i put them in seperate functions i cant jump while im moving.


just copy the code in message 7 and paste it over your button listeners and listener functions.