Jump to content

Better way to approch button to action (move player)


Learning
 Share

Recommended Posts

Hey so i moved away from the gameControler.js

 

So instead i tried this.

lftButton = game.add.button(0, 300, 'button', actionMoveLeft, this, 2, 1, 0);
rhtButton = game.add.button(200, 300, 'button', actionMoveRight, this, 2, 1, 0);

 

//but i know that it has to keep update alive cause when you only use this you have to press the button like crazy to make the player move.

tried too add an extra pointer, and then check for active input and so on but still no good came from it.

 

So please any better solutions too this would be awesome thanks!

function actionMoveLeft() {    player.body.velocity.x -= 900;    player.animations.play('left');}function actionMoveRight() {   player.body.velocity.x += 900;   player.animations.play('left');}

-Learning

Link to comment
Share on other sites

tried it like this

rhtButton.onTouchStart = function(event){player.body.velocity.x += 900;player.animations.play('right');}
and then just the pixi way but no dicebutton.touchstart = function(touchData){player.body.velocity.x -= 900;player.animations.play('left');}

the doc's for this is very little too none. so i was hopin too get some more ideas on this

Thanks!

Link to comment
Share on other sites

button = game.add.button(0, 300, 'button');

rhtButton = game.add.button(200, 300, 'btn');

button.inputEnabled = true;

button.input.pointerOver.id = 1;

rhtButton.inputEnabled = true;

rhtButton.input.pointerOver.id = 1;

rhtButton.input.useHandCursor = true;

button.input.useHandCursor = true;

game.input.onHold.add(actionMoveLeft, this);

game.input.onHold.add(actionMoveRight, this);

button.events.onInputDown.add(actionMoveLeft, this);

rhtButton.events.onInputDown.add(actionMoveRight, this);

}

function actionMoveLeft() {

player.body.velocity.x -= 900;

player.animations.play('left');

}

function actionMoveRight() {

player.body.velocity.x += 900;

player.animations.play('right');

}

Link to comment
Share on other sites

//have it as so but still have not found a solution to keep walking if it's onHold the button...

//please guide me!!!

game.input.onHold.add(actionMoveLeft, true, this);

game.input.onHold.add(actionMoveRight, true, this);

button.events.onInputDown.add(actionMoveLeft, this);

rhtButton.events.onInputDown.add(actionMoveRight, this);

}

function actionMoveLeft() {

if (game.input.onDown && actionMoveLeft && checkActiveDown) {

checkActiveDown = true;

player.body.velocity.x -= 300 * 2;

player.body.x -= 2;

player.animations.play('left');

} else if (game.input.onUp) {

player.body.velocity.x -= 100 * 4;

player.body.x -= 4;

player.animations.play('left');

checkActiveDown = false;

}

}

function actionMoveRight() {

if (game.input.onDown && actionMoveLeft && checkActiveDown) {

player.body.velocity.x += 300 * 2;

player.body.x += 2;

player.animations.play('right');

} else if (game.input.onUp) {

player.body.velocity.x += 100 * 4;

player.body.x += 4;

player.animations.play('right');

checkActiveDown = false;

}

}

Link to comment
Share on other sites

this is my solution - since i moved away from gamecontroller.js too i made my own gamecontroller with phaser only..

 

 http://www.html5gamedevs.com/topic/1813-the-monthly-phaser-examples-contest/?p=27173

 

the mentioned workaround (the one line in the update loop where i check for current pointers) to make buttons NOT stick is not needed anymore in 2.0.2

Link to comment
Share on other sites

@valueerror, i've fixed left and right but the jump button is not working as it should.

i use 2.0 of phaser so first of all i have to make use of velocity to get the player to jump

and not moveUp so yeah it's not reacting that fast to the jump don't get why.

 

i removed the delay and i dont have it inside a function..

game.physics.startSystem(Phaser.Physics.ARCADE);game.physics.arcade.gravity.y = 250;var bjump=false;jump = game.add.button(100, 280, 'btnjump', null, this, 2, 1);jump.events.onInputOver.add(function(){bjump=true;});jump.events.onInputOut.add(function(){bjump=false;});jump.events.onInputDown.add(function(){bjump=true;});jump.events.onInputUp.add(function(){bjump=false;});   if (bjump) {                             player.body.velocity.y = -200;                       }
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...