isepulveda Posted March 12, 2018 Share Posted March 12, 2018 I have been trying to use phaser examples in my games. I noticed that the examples are not using states. The game that I am developing is using states, so I can't use the examples because I keep getting errors. Here's an example of what I am talking about: Phaser example: game.input.onDown.addOnce(updateText, this); my code game.input.onDown.addOnce(this.updateText(), this); What am I doing wrong? What can I do different? Link to comment Share on other sites More sharing options...
Fenopiù Posted March 12, 2018 Share Posted March 12, 2018 Have you tried: game.input.onDown.addOnce(this.updateText, this); or better: game.input.onDown.addOnce(function(){this.updateText()}, this); ? Link to comment Share on other sites More sharing options...
in mono Posted March 12, 2018 Share Posted March 12, 2018 Maybe you should refer to this.game instead of just game. Phaser examples do use states, you just can't change them. Link to comment Share on other sites More sharing options...
samme Posted March 12, 2018 Share Posted March 12, 2018 There's always at least state, even if it's the default state. The difference with the Phaser 2 Examples is that those are referencing ordinary functions rather than methods of the current state. You need game.input.onDown.addOnce(this.updateText, this); Remember, you're passing a function value (this.updateText) not the result of a function call (this.updateText()). Link to comment Share on other sites More sharing options...
isepulveda Posted March 12, 2018 Author Share Posted March 12, 2018 I used a function within the create: function. game.input.onDown.addOnce(update, this); function update(){ count++; if(count <= 1){ this.message.setText("We need your help to save the world"); return count; } } }, update: function(){ } Now, I am trying to update the text for each click. Can anyone help? Link to comment Share on other sites More sharing options...
3man7 Posted March 13, 2018 Share Posted March 13, 2018 @isepulveda Everything in 'update' is going to run 60 times/second. The clicking event should be put in 'create'. game.inputEnabled = true; game.input.onDown.add(function () { count++; if (count <= 1) { this.message.setText("We need your help to save the world"); } }, this); Could you explain a little bit what you are trying to achieve exactly? Any slight changes to the code will return different cases. Link to comment Share on other sites More sharing options...
isepulveda Posted March 13, 2018 Author Share Posted March 13, 2018 create: function(){ count = 0; this.cursor = game.input.keyboard.createCursorKeys(); game.stage.backgroundColor = "#4488AA"; this.player = game.add.sprite(600, 250, 'player'); this.player.anchor.setTo(0.5, 0.5); game.physics.arcade.enable(this.player); this.friend = game.add.sprite(400, 250, 'friend'); this.friend.anchor.setTo(0.5, 0.5); game.physics.arcade.enable(this.friend); // Create a Rectangle var rectangle = new Phaser.Rectangle(0, 515, 1000, 200); // Create a BitmapData just to plot the points to var bmd = game.add.bitmapData(game.width, game.height); bmd.rect(rectangle.x, rectangle.y, rectangle.width, rectangle.height, '#ffffff'); bmd.addToWorld(); this.message = game.add.text(500, 540, 'Hello, thank you for coming to save us.', {font: "18px Arial", fill: "#333333", align: "center"}); this.message.anchor.setTo(0.5, 0.5); this.timer = game.time.create(false); game.time.events.loop(Phaser.Timer.SECOND * 4, update, this); function update(){ count++; if(count === 0 ){ this.message.setText("1. We need your help to save the world"); }else if(count === 1){ this.message.setText("To be able to save the world, you must gain magic."); }else if(count === 2){ this.message.setText("The mini games in each world will give you a new magic spell. You must complete the mini-game before exploring the rest of the world. Good luck to you."); } } }, I am trying to display a dialogue between two characters. Link to comment Share on other sites More sharing options...
3man7 Posted March 14, 2018 Share Posted March 14, 2018 Hey, So you have 4 seconds for each of the messages to read; clicking on the screen will skip the current one but also resets the timer back. Take your time looking/understanding the code below and let me know if something doesn't seem to make sense to you. Main.Game = function (game) {}; var myTimer, count = 0; Main.Game.prototype = { create: function () { this.cursor = game.input.keyboard.createCursorKeys(); game.stage.backgroundColor = "#4488AA"; this.player = game.add.sprite(600, 250, 'player'); this.player.anchor.setTo(0.5, 0.5); game.physics.arcade.enable(this.player); this.friend = game.add.sprite(400, 250, 'friend'); this.friend.anchor.setTo(0.5, 0.5); game.physics.arcade.enable(this.friend); // Create a Rectangle var rectangle = new Phaser.Rectangle(0, 515, 1000, 200); // Create a BitmapData just to plot the points to var bmd = game.add.bitmapData(game.width, game.height); bmd.rect(rectangle.x, rectangle.y, rectangle.width, rectangle.height, '#ffffff'); bmd.addToWorld(); //myMessage this.myMessage = game.add.text(500, 540, '', { font: "18px Arial", fill: "#333333", align: "center" }); this.myMessage.anchor.setTo(0.5, 0.5); //myTimer myTimer = game.time.create(false); //input enabler game.inputEnabled = true; game.input.onDown.add(this.updateMsg, this); this.updateMsg(); }, updateTmr: function () { if (count <= 2) { count++; myTimer.destroy(); myTimer.add(Phaser.Timer.SECOND * 4, this.updateMsg, this); myTimer.start(); myTimer.resume(); } else { myTimer.destroy(); } }, updateMsg: function () { switch (count) { case 0: this.myMessage.setText('Hello, thank you for coming to save us.'); break; case 1: this.myMessage.setText('1. We need your help to save the world'); break; case 2: this.myMessage.setText('To be able to save the world, you must gain magic.'); break; case 3: this.myMessage.setText('The mini games in each world will give you a new magic spell. You must complete the mini-game before exploring the rest of the world. Good luck to you.'); break; } this.updateTmr(); }, update: function () { //stuff to run at 60 times per second }, render: function () { game.debug.text("count: " + count, 32, 40); game.debug.text("myTimer: " + myTimer.duration.toFixed(0), 32, 60); } }; The render function will make you see things better - although it has an impact on fps so be sure to remove it whenever you try to get the best out of your project/game. Good luck! Link to comment Share on other sites More sharing options...
Recommended Posts