Copy link to clipboard
Copied
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;
}
}
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
}
Copy link to clipboard
Copied
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
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
for each of your 3 movieclip buttons:
btnLeft.bool=leftPressed;
btnRight.bool=rightPressed;
btnJump.bool=downPresse;
Copy link to clipboard
Copied
That helped a bit but what im really looking for is where you can move then jump while still moving to the right or left and jump that way not have to press a different button each time
Copy link to clipboard
Copied
the code i suggested should work that way: press and hold the left button, you should move left. while holding down the left button intermittantly press the jump button and, while you continue to move left, you should intermittantly jump.
Copy link to clipboard
Copied
mabye i have didnt put the code correctly here is my code:
btnLeft.bool=leftPressed;
btnRight.bool=rightPressed;
btnUp.bool=downPressed;
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(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 nextLevel():void{
currentLevel++;
trace("Next Level: " + currentLevel);
if(currentLevel == 2){
gotoLevel2();
}
// can be extended...
// else if(currentLevel == 3) { gotoLevel3(); } // etc, etc.
}
function gotoLevel2():void{
back.other.gotoAndStop(2);
back.visuals.gotoAndStop(2);
back.collisions.gotoAndStop(2);
scrollX = 0;
scrollY = 500;
keyCollected = false;
back.other.doorKey.visible = true;
doorOpen = false;
back.other.lockedDoor.gotoAndStop(1);
}
function keyDownHandler(e:MouseEvent):void{
leftPressed = 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:MouseEvent):void{
leftPressed = false;
}
function keyUpHandler2(e:MouseEvent):void{
rightPressed = false;
}
function keyDownHandler2(e:MouseEvent):void{
rightPressed = true;
}
function keyUpHandler3(e:MouseEvent):void{
upPressed = false;
}
function keyDownHandler3(e:MouseEvent):void{
upPressed = true;
}
btnLeft.addEventListener(MouseEvent.MOUSE_DOWN,keyDownHandler);
btnLeft.addEventListener(MouseEvent.MOUSE_UP,keyUpHandler);
btnRight.addEventListener(MouseEvent.MOUSE_DOWN,keyDownHandler2);
btnRight.addEventListener(MouseEvent.MOUSE_UP,keyUpHandler2);
btnUp.addEventListener(MouseEvent.MOUSE_DOWN,keyDownHandler3);
btnUp.addEventListener(MouseEvent.MOUSE_UP,keyUpHandler3);
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
}
Copy link to clipboard
Copied
again, use this:
btnLeft.addEventListener(MouseEvent.MOUSE_DOWN,downF);
btnLeft.addEventListener(MouseEvent.MOUSE_UP,upF);
btnLeft.bool=leftPressed; // if you use movieclip buttons
btnRight.addEventListener(MouseEvent.MOUSE_DOWN,downF);
btnRight.addEventListener(MouseEvent.MOUSE_UP,upF);
btnRight.bool=rightPressed; // if you use movieclip buttons
btnUp.addEventListener(MouseEvent.MOUSE_DOWN,downF);
btnUp.addEventListener(MouseEvent.MOUSE_UP,upF);
btnUp.bool=upPressed; // if you use movieclip buttons
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
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
just copy the code in message 7 and paste it over your button listeners and listener functions.
Copy link to clipboard
Copied
if i do that when i press the buttons i now dont move at all
Copy link to clipboard
Copied
my error. that should be:
btnLeft.addEventListener(MouseEvent.MOUSE_DOWN,downF);
btnLeft.addEventListener(MouseEvent.MOUSE_UP,upF);
btnLeft.bool="leftPressed"; // if you use movieclip buttons
btnRight.addEventListener(MouseEvent.MOUSE_DOWN,downF);
btnRight.addEventListener(MouseEvent.MOUSE_UP,upF);
btnRight.bool="rightPressed"; // if you use movieclip buttons
btnUp.addEventListener(MouseEvent.MOUSE_DOWN,downF);
btnUp.addEventListener(MouseEvent.MOUSE_UP,upF);
btnUp.bool="upPressed"; // if you use movieclip buttons
function downF(e:MouseEvent):void{
this[e.currentTarget.bool]=true; // you might need to cast e.currentTarget as a MovieClip
}
function upF(e:MouseEvent):void{
this[e.currentTarget.bool]=false; // may need to cast
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now