Jump to content

Creating a circle for the player?


DbD
 Share

Recommended Posts

Hi i'm trying to make a circle that is then used as the player, but for some reason i'm having an issue with the movement code. NOTE: I've used this code on regular sprites and it works fine, just won't work for the circle for some reason

var game = new Phaser.Game(800, 600, Phaser.AUTO, 'game test', { preload: preload, create: create, update: update });function preload() {    game.load.image('background', 'assets/defaultBckgrnd.png');        game.input.maxPointers = 1;    }function create() {    game.add.sprite(0, 0, 'background');        var graphics = game.add.graphics(0, 0);    // graphics.lineStyle(2, 0xffd900, 1);    graphics.beginFill(0xFF0000, 1);    player = graphics.drawCircle(300, 300, 100);        player.anchor.setTo(0.5, 0.5);    game.physics.arcade.enable(player);        player.body.collideWorldBounds = true;    }function update() {    upW = game.input.keyboard.addKey(Phaser.Keyboard.W);    downS = game.input.keyboard.addKey(Phaser.Keyboard.S);    leftA = game.input.keyboard.addKey(Phaser.Keyboard.A);    rightD = game.input.keyboard.addKey(Phaser.Keyboard.D);        //  Reset the players velocity (movement)    player.velocity.x = 0;    player.velocity.y = 0;        // normal directional movement    if (leftA.isDown)    {        //  Move to the left        player.body.velocity.x = -150;    }    else if (rightD.isDown)    {        //  Move to the right        player.body.velocity.x = 150;            }    else if (upW.isDown)    {        //  Move to the up        player.body.velocity.y = -150;    }    else if (downS.isDown)    {        //  Move to the down        player.body.velocity.y = 150;    }    // diagonal movement    if (leftA.isDown && upW.isDown)    {        //  Move to the left and up        player.body.velocity.x = -150;        player.body.velocity.y = -150;    }        if (leftA.isDown && downS.isDown)    {        //  Move to the left and down        player.body.velocity.x = -150;        player.body.velocity.y = 150;    }        if (rightD.isDown && upW.isDown)    {        //  Move to the right and up        player.body.velocity.x = +150;        player.body.velocity.y = -150;    }        if (rightD.isDown && downS.isDown)    {        //  Move to the right and down        player.body.velocity.x = 150;        player.body.velocity.y = 150;    }    }

Everything works fine when i don't have the movement code in the function update(), but as soon as I add it, phaser does it's little black screen showing you that it's not working. Please help!

Link to comment
Share on other sites

i figured it out. for anyone who's curious, here's what i ended with:

var game = new Phaser.Game(800, 600, Phaser.AUTO, 'game test', { preload: preload, create: create, update: update });function preload() {    game.load.image('background', 'assets/defaultBckgrnd.png');        game.input.maxPointers = 1;    }function create() {    game.add.sprite(0, 0, 'background');        var cirSize = 100;    var cirX = 100;    var cirY = 100;        var graphics = game.add.graphics(0, 0);    //graphics.lineStyle(2, 0xffd900, 1);    graphics.beginFill(0xFF0000, 1);    player = graphics.drawCircle(cirX, cirY, cirSize);    graphics.endFill();        player.anchor.setTo(0.5, 0.5);    player.body.collideWorldBounds = true;        player.x = cirX;    player.y = cirY;    }function update() {        upW = game.input.keyboard.addKey(Phaser.Keyboard.W);    downS = game.input.keyboard.addKey(Phaser.Keyboard.S);    leftA = game.input.keyboard.addKey(Phaser.Keyboard.A);    rightD = game.input.keyboard.addKey(Phaser.Keyboard.D);        // normal directional movement    if (leftA.isDown)    {        //  Move to the left        player.x -= 5;    }     else if (rightD.isDown)    {        //  Move to the right        player.x += 5;            }    else if (upW.isDown)    {        //  Move to the up        player.y -= 5;    }    else if (downS.isDown)    {        //  Move to the down        player.y += 5;    }    // diagonal movement    if (leftA.isDown && upW.isDown)    {        //  Move to the left and up        player.x -= 5;                player.y -= 5;    }        if (leftA.isDown && downS.isDown)    {        //  Move to the left and down        player.x -= 5;                player.y += 5;    }        if (rightD.isDown && upW.isDown)    {        //  Move to the right and up        player.x += 5;                player.y -= 5;    }        if (rightD.isDown && downS.isDown)    {        //  Move to the right and down        player.x += 5;                player.y += 5;    }    }

EDIT: something else i just figured out is that in the create function, player.x = cirX;

player.y = cirY; is unnecessary. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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