Jump to content

Problem with user input


Tomi
 Share

Recommended Posts

Hello everybody!

I'm still new here and as a Phaser game developer. I try to make a very simple game, in which the player can control a ball. When I running the program through "Brackets" code editor, it shows the ball correctly, but not react to the keypresses. Here is the code (some varaibles are in Hungarian, e.g.: jatek=game, labdakepe=sprite of ball and nyilgombok=arrow keys in English):

var jatek = new Phaser.Game(
	800, 600, Phaser.AUTO, '',
	{ preload: preload, create: create, update: update }
);

function preload() {
	jatek.load.image('labda', 'labda.png');
}

var labdakepe;
var nyilgombok;

function create() {
	labdakepe = jatek.add.sprite(jatek.world.centerX, 0, 'labda');
	//labdakepe.acceleration.y = 100; //Gravitáció y irányba pozitív értékkel, azaz lefelé.
	//labdakepe.body.collideWorldBounds = true; //megállás a játéktér szélénél.
    nyilgombok = jatek.input.keyboard.createCursorKeys();
}

function update() {
	if (nyilgombok.left.isDown) {
	labdakepe.velocity.x = -50;
	//labdakepe.scale.x = 1; //Tükrözés balra
	} else if (nyilgombok.right.isDown) {
	labdakepe.velocity.x = 50;
	//labdakepe.scale.x = -1; //Tükrözés jobbra
	}
}

Somebody can help me? Please don't let that an error unknown by me discourage me from porogramming!

Link to comment
Share on other sites

The velocity properties are apart of Phaser's Arcade Physics engine. You don't invoke velocity.x directly from the sprite object. You need to use "mySprite.body.velocity.x". But before you can do that, you need to enable the sprite's built-in Arcade Physics body.

Try the code below:

function create() {
	labdakepe = jatek.add.sprite(jatek.world.centerX, 0, 'labda');
    labdakepe.enableBody = true;
	//labdakepe.acceleration.y = 100; //Gravitáció y irányba pozitív értékkel, azaz lefelé.
	//labdakepe.body.collideWorldBounds = true; //megállás a játéktér szélénél.
    nyilgombok = jatek.input.keyboard.createCursorKeys();
}

function update() {
	if (nyilgombok.left.isDown) {
	labdakepe.body.velocity.x = -50;
	//labdakepe.scale.x = 1; //Tükrözés balra
	} else if (nyilgombok.right.isDown) {
	labdakepe.body.velocity.x = 50;
	//labdakepe.scale.x = -1; //Tükrözés jobbra
	}
}

 

Link to comment
Share on other sites

Huhhh, finally I found out that what is missed from my code, because I look at the "Shooting bullets" example.
So, these lines are missed from my game to enable the body and game physics:

jatek.physics.startSystem(Phaser.Physics.ARCADE);
labdakepe.enableBody = true;
jatek.physics.enable(labdakepe, Phaser.Physics.ARCADE);

But I wondering, how can move sprites other Phaser sample programs without this; e.g. that example in the YouTube video?
And now, let's go coding!:lol:

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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