Sign in to follow this  
LaaW

my sprite can't move

Recommended Posts

Hi, I started making simple pong like game in phaser but i don't really want to use any tutorial step by step so i started making game, when i try to make movement for paddle i saw first problem.

My paddle don't move and completely i have no idea why. Anybody can show me what's i'm doing wrong?

There is a main file with js


//this game will have only 1 state
var GameState = {
    


//load the game assets before the game starts
  preload: function() {
    //adding sprites
    this.load.image('ball','assets/images/ball.png');
    this.load.image('paddle','assets/images/paddle.png');
  },
  //executed after everything is loaded
  create: function() {    
    //adding background
    game.stage.backgroundColor = '#d3de33';
    //adding ball
    ball = this.game.add.sprite(game.world.centerX,game.world.centerY,'ball');
    ball.scale.setTo(0.1);
    //adding paddles
    paddleLeft = this.game.add.sprite(5, game.world.centerY, 'paddle');
    paddleLeft.anchor.setTo(0.5,0.5);
    paddleRight = this.game.add.sprite(795, game.world.centerY, 'paddle');
    paddleRight.anchor.setTo(0.5,0.5);
    //paddle movemment
    if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
    {
        paddleLeft.x -= 50;
    }    else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
    {
         paddleLeft.y += 50;
    };
  },
  update: function() {
},


  render(){
        //render paddles
        game.debug.spriteInfo(paddleLeft, 20, 32);

  }
};

//initiate the Phaser framework
var game = new Phaser.Game(800, 600, Phaser.AUTO);

game.state.add('GameState', GameState);
game.state.start('GameState');

 

pong.rar

Share this post


Link to post
Share on other sites

Hi LaaW,
you are checking for keyboard inputs in the create function, so just once when the game starts.

Use the update functions, instead, and it works fine, like this:
http://jsfiddle.net/vq6ubnfo/

As shown in the Phaser keyboard example, too:
https://phaser.io/examples/v2/input/keyboard

Phaser examples are a good start too, if you don't want to follow step by step tutorials.

Cheers! :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.