Jump to content

how to update text with a condition


Shadow20
 Share

Recommended Posts

Hello, I am trying out phaser and tried to make a game of pong and i want to display the score so i used the update text example to guide me but i got the error that text.setText is not a valid function by firebug and i could not find it in the documentation either. therefore i tried this instead:

text = "Score: " + playerScore.toString();

but the game freezes when it hits this line. any help with this issue?

 

here is my code so far:

var game = new Phaser.Game(480,320,Phaser.AUTO, '', {preload:preload, create:create , update:update})var paddles;var paddle;var ball;var cursors;var enemyScore = 0;var playerScore = 0;var text = "Score: " + playerScore.toString();var style = { font: "65px Arial", fill: "#ff0044", align: "center" };function preload(){    game.load.image('paddle', 'assets/paddle.png');    game.load.image('ball', 'asssets/ball.png');}function create(){    paddles = game.add.group();    paddle = game.add.sprite(10, 190, 'paddle');    paddle.body.immovable = true;    paddle.body.collideWorldBounds = true;    paddles.add(paddle);    paddle = game.add.sprite(450, 90, 'paddle');    paddle.body.immovable = true;    paddle.body.collideWorldBounds = true;    paddles.add(paddle);    ball = game.add.sprite(240, 160, 'ball');    ball.body.velocity.x = -150;    ball.body.velocity.y = 800;    ball.body.collideWorldBounds = true;    ball.body.bounce.setTo(1, 1);    cursors = game.input.keyboard.createCursorKeys();    var renderText = game.add.text(game.world.centerX, game.world.centerY, text, style);}function update(){    game.physics.collide(ball, paddles, null, null, this);    paddle.body.velocity.setTo(0, 0);        if(cursors.up.isDown){        paddle.body.velocity.y = -100;    }    if(cursors.down.isDown){        paddle.body.velocity.y = 100;    }        if(ball.x < 10 || ball.x > game.width - 50){        ballReset(ball.x);    }}function ballReset(x){    if(x < 10){        enemyScore++;    }    else if(x > game.width-50){        playerScore++;         text = "Score: " + playerScore.toString();    }    ball.x = 240;    ball.y = 160;}
Link to comment
Share on other sites

You should use setText on your 'renderText' variable, your  'text' variable isn't a Phaser text object, it is only a string, on JavaScript you don't need to use .toString() to concatenate strings and variables with  the plus sign should be enough, also I recommend you to put your 'renderText' variable outside of your functions so you can use it on the update or in the ball reset function.

Link to comment
Share on other sites

@Arlefreak: thanks, i figured it out after looking at the example again yesterday but due to the post approval i couldnt remove it or change it. thats interesting that you dont need to .toString() to concatanate the values though. Thanks for the help!

 

I saw .content in the docs although i got it working per what Arlefreak suggested so i dont know if it is worth looking into?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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