Tomi Posted March 20, 2016 Share Posted March 20, 2016 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 More sharing options...
Yacob Posted March 21, 2016 Share Posted March 21, 2016 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 } } drhayes 1 Link to comment Share on other sites More sharing options...
Tomi Posted March 21, 2016 Author Share Posted March 21, 2016 Thanks, Yacob, I have tried your addition, but my program still not work. Maybe Phaser can't use my old PS/2 keyboard? By the way, I have copy out the code from here: https://www.youtube.com/watch?v=0Mu1yAkkEi8 but my program works with neither. What should I do? Link to comment Share on other sites More sharing options...
Tomi Posted March 23, 2016 Author Share Posted March 23, 2016 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! Link to comment Share on other sites More sharing options...
Recommended Posts