isepulveda

Phaser Examples - loading files without states

Recommended Posts

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?

 

Share this post


Link to post
Share on other sites

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()).

Share this post


Link to post
Share on other sites

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? 

Share this post


Link to post
Share on other sites

@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.

Share this post


Link to post
Share on other sites
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. 

Share this post


Link to post
Share on other sites

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! :)

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

  • Recently Browsing   0 members

    No registered users viewing this page.