hi! thank you very muc, however i don’t understand…
my game have the following code:
var config = {
type: Phaser.AUTO,
width: 1200,
height: 900,
physics: {
// for example, this part 'arcade' will be the definition of what type of physics we want for our game, (in this case our character will fall after a jump)
default: 'arcade',
arcade: {
gravity: { y: 300 },
debug: false
}
},
scene: {
preload: preload,
create: create,
update: update
}
};
var game = new Phaser.Game(config);
function preload ()
{
// with this functions, we prepare 'sky' (the background) and 'ground' (the platforms). We put this two phrases into the function preload, that's because we want to let our game load this assets whe we start the game, this is the two first things who will load our game.
this.load.image('sky','assets/sky.jpg');
this.load.image('ground', 'assets/platform.png');
this.load.image('star', 'assets/star.png');
// this is the preload resources for our game theme
this.load.audio('theme', [
'assets/music/music.mp3'
]);
this.load.spritesheet('dude',
'assets/dude.png',
{ frameWidth: 32, frameHeight: 48 }
);
}
var platforms;
var music;
var player;
var star;
var score = 0;
var scoreText;
function create ()
{
music = this.sound.add('theme');
player = this.physics.add.sprite(100, 450, 'dude').setDepth(2);
cursors = this.input.keyboard.createCursorKeys();
scoreText = this.add.text(16, 16, 'Estrellitas: 0', { fontSize: '32px', fill: '#000' }).setDepth(2);
player.setBounce(0.2);
player.setCollideWorldBounds(true);
stars = this.physics.add.group({
key: 'star',
repeat: 11,
setXY: { x: 12, y: 0, stepX: 70 }
});
stars.children.iterate(function (child) {
child.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8)).setDepth(2);
});
this.anims.create({
key: 'left',
frames: this.anims.generateFrameNumbers('dude', { start: 0, end: 3 }),
frameRate: 10,
repeat: -1
});
this.anims.create({
key: 'turn',
frames: [ { key: 'dude', frame: 4 } ],
frameRate: 20
});
this.anims.create({
key: 'right',
frames: this.anims.generateFrameNumbers('dude', { start: 5, end: 8 }),
frameRate: 10,
repeat: -1
});
player.body.setGravityY(300)
// with this command, we make a background, 'sky' is called by this, and we can change our width and height if we edit the following values (the numbers below)
this.add.image(400, 300, 'sky');
//this is the same variable that we used above, but in the create section, so we can create the music.
var music = this.sound.add('theme');
//this command basically tells the game to play the music.
music.play();
platforms = this.physics.add.staticGroup();
// with this, we create some platforms, as i said above, instead of 'sky' we call to 'ground' to get the assets and let the game load the plaftorms corretcly.
// but in this case, we can't modify the heigth and width, we can only modify the coordinates of the platforms.
this.physics.add.collider(player, platforms);
this.physics.add.collider(stars, platforms);
this.physics.add.overlap(player, stars, collectStar, null, this);
function collectStar (player, star)
{
star.disableBody(true, true);
score += 10;
scoreText.setText('Score: ' + score);
}
platforms.create(500, 900, 'ground').setScale(2).refreshBody();
platforms.create(999, 500, 'ground');
platforms.create(100, 600, 'ground');
platforms.create(800, 999, 'ground');
}
function update ()
{
if (cursors.left.isDown)
{
player.setVelocityX(-160);
player.anims.play('left', true);
}
else if (cursors.right.isDown)
{
player.setVelocityX(160);
player.anims.play('right', true);
}
else
{
player.setVelocityX(0);
player.anims.play('turn');
}
if (cursors.up.isDown && player.body.touching.down)
{
player.setVelocityY(-330);
}
}
</script>
</body>
</html>
how can i adapt your code to my game and make another level??