Official phaser beginner tute

To show the sky background image you write this.
this.add.image(400, 300, ‘sky’);
To add a platform you do this.
platforms = this.physics.add.staticGroup();
platforms.create(600, 400, ‘ground’);
Why use ‘add.image’ for one and ‘create’ for another?

also

player = this.physics.add.sprite(100, 450, ‘dude’);
Why is this code ‘add.sprite’ instead of perhaps ‘add.image’ like the sky example?

also

To create a platform you write this.
platforms.create(50, 250, ‘ground’);
To create the player you write this.
player = this.physics.add.sprite(100, 450, ‘dude’);
player.setBounce(0.2);
player.setCollideWorldBounds(true);
Why is there no ‘create’ in the player code as in the platforms code?

Thanks
Steve

2 Likes

Why use ‘add.image’ for one and ‘create’ for another?

You add the image and you add the staticGroup called platforms. A group is a special object that contains other objects, in this case it is a group that automatically adds physics to objects added to it. You can add already existing objects to it, using platforms.add(…), it needs to have another method called create in able to create new objects directly into it.

Why is this code ‘add.sprite’ instead of perhaps ‘add.image’ like the sky example?

A sprite is a different type of gameobject compared to an image. It is an image with extended functionality, for example the ability to be animated.

Why is there no ‘create’ in the player code as in the platforms code?

There is no specific player code, you would have to create your own class or function for this and wrap the whole add.sprite, setBounce, setCollideWorldBounds etc into it. The platforms (staticGroup) is a special object that has been constructed to have extra helpers to make them fast and easy to use.

What the player code in your example does is create a sprite, a sprite that is no different from any other sprite you can add (say an enemy sprite). Then it is followed by using the methods available in the sprite object to configure how you want this specific sprite (player) to behave. In this case have a bit of bounce on collision and to collide with the bounds of the world.

An example to do your own “create” is to make a function:

// A function that adds a sprite and then sets the same properties as in the example.
function CreatePlayer(x, y, texture, bounceAmount, collideWorldBounds){
    var sprite = this.physics.add.sprite(x, y, texture);
    sprite.setBounce(bounceAmount);
    sprite.setCollideWorldBounds(collideWorldBounds);

    return sprite;
}

// Use the function to create your player.
var player = CreatePlayer(100, 450, 'dude', 0.2, true);

Not saying this is what you should do, only trying to explain by showing.

4 Likes

Thanks for your help. I think I might have to do a bit of revision regarding objects and methods etc.

1 Like

Thankyou, for your help!

1 Like

This was really helpful