Jump to content

Character move problem


brunotrocoli
 Share

Recommended Posts

Hello Friends! I'm a noob phaser dev.

I have a problem with character move... anybody can help me with this issue?

The problem: how to add  

cursors.up.isDown

to function

function actionOnClick () {
   player.body.moveRight(3000);
}

SOURCE CODE
 

var game = new Phaser.Game("100%", "100%", Phaser.CANVAS, 'game', { preload: preload, create: create, update: update, render: render });
var crosta;
var player;
var cursors;
var button;
function preload() {
   game.load.image('player','res/assets/btn.png');
   game.load.image('arte-crosta','res/assets/terrain.png');
   game.load.spritesheet('button', 'res/assets/btn.png', 193, 71);
}
function create() {
   crosta = game.add.tileSprite(0, 0, 3840, 2160, 'arte-crosta');
   game.world.resize(3840, 2160);
   game.world.setBounds(0, 0, 3840, 2160);
   // game.add.text(20, 100, "- phaser -", { font: "32px Arial", fill: "#330088", align: "center" });
   
   player = game.add.sprite(game.world.centerX, game.world.centerY, 'player');
   button = game.add.button(game.world.centerX - 95, 400, 'button', actionOnClick, this, 2, 1, 0);

   game.physics.startSystem(Phaser.Physics.P2JS);
   game.physics.p2.enable(player);
   game.camera.follow(player);
   cursors = game.input.keyboard.createCursorKeys();
}
function update() {
   player.body.setZeroVelocity();
   if(cursors.up.isDown){ player.body.moveUp(3000) }
   else if (cursors.down.isDown){ player.body.moveDown(3000); }
   if (cursors.left.isDown){ player.body.velocity.x = -3000; }
   else if (cursors.right.isDown){ player.body.moveRight(3000); }
}
function render() {}
function actionOnClick () {
   player.body.moveRight(3000);
}

 

Link to comment
Share on other sites

player.body.velocity.x = 0;
player.body.velocity.y = 0;

if (cursors.up.isDown)
{
  personaggio.body.velocity.y = -3000;
}
else if (cursors.down.isDown)
{
  player.body.velocity.y = 3000;
}if (cursors.left.isDown)
{
  personaggio.body.velocity.x = -3000;
}
else if (cursors.right.isDown)
{
  player.body.velocity.x = 3000;
}

 

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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