ar config = {
type: Phaser.AUTO,
width: 400,
height: 400,
parent:‘play’,
backgroundColor:’#6699CC’,
physics: {
default: ‘arcade’,
arcade: {
gravity: { y: 0 },
debug: false
}
},
scene: {
preload: preload,
create: create,
update: update
}
};
var game = new Phaser.Game(config);
function preload ()
{
//this refers to the object that it refers
// this.load.image(‘snowman’, ‘images/snowman_sad.png’);
this.load.image(‘block’, ‘images/block_ice.png’);
this.load.image(‘star’, ‘images/star.png’);
this.load.spritesheet(‘dude’, ‘images/dude.png’, { frameWidth: 32, frameHeight: 48 } );
}
var player;
var stars;
var platforms;
var cursors;
var score = 0;
var scoreText;
function create ()
{
// this.add.image(0, 0, 'block');
// this.add.image(50, 0, 'block');
// this.add.image(100, 0, 'block');
// this.add.image(150, 0, 'block');
// this.add.image(200, 0, 'block');
platforms = this.physics.add.staticGroup(); //create physics for platform when player hits the platform
platforms.create(25, 25, 'block');
platforms.create(75, 25, 'block');
platforms.create(125, 25, 'block');
platforms.create(175, 25, 'block');
platforms.create(225, 25, 'block');
platforms.create(275, 25, 'block');
platforms.create(325, 25, 'block');
platforms.create(375, 25, 'block');
platforms.create(25, 75, 'block');
platforms.create(75, 75, 'block');
platforms.create(125, 75, 'block');
platforms.create(100, 75, 'block');
platforms.create(125, 75, 'block');
platforms.create(175, 75, 'block');
platforms.create(225, 75, 'block');
platforms.create(275, 75, 'block');
platforms.create(325, 75, 'block');
platforms.create(375, 75, 'block');
platforms.create(25, 125, 'block');
platforms.create(75, 125, 'block');
platforms.create(125, 125, 'block');
platforms.create(100, 125, 'block');
platforms.create(125, 125, 'block');
platforms.create(175, 125, 'block');
platforms.create(225, 125, 'block');
platforms.create(275, 125, 'block');
platforms.create(325, 125, 'block');
platforms.create(375, 125, 'block');
platforms.create(25, 175, 'block');
platforms.create(375, 175, 'block');
platforms.create(25, 225, 'block');
platforms.create(75, 225, 'block');
platforms.create(125, 225, 'block');
platforms.create(100, 225, 'block');
platforms.create(125, 225, 'block');
platforms.create(175, 225, 'block');
platforms.create(225, 225, 'block');
platforms.create(275, 225, 'block');
platforms.create(325, 225, 'block');
platforms.create(375, 225, 'block');
platforms.create(25, 275, 'block');
platforms.create(75, 275, 'block');
platforms.create(100, 275, 'block');
platforms.create(125, 275, 'block');
platforms.create(175, 275, 'block');
platforms.create(225, 275, 'block');
platforms.create(275, 275, 'block');
platforms.create(325, 275, 'block');
platforms.create(375, 275, 'block');
platforms.create(25, 325, 'block');
platforms.create(75, 325, 'block');
platforms.create(100, 325, 'block');
platforms.create(125, 325, 'block');
platforms.create(175, 325, 'block');
platforms.create(225, 325, 'block');
platforms.create(275, 325, 'block');
platforms.create(325, 325, 'block');
platforms.create(375, 325, 'block');
platforms.create(25, 375, 'block');
platforms.create(75, 375, 'block');
platforms.create(100, 375, 'block');
platforms.create(125, 375, 'block');
platforms.create(175, 375, 'block');
platforms.create(225, 375, 'block');
platforms.create(275, 375, 'block');
platforms.create(325, 375, 'block');
platforms.create(375, 375, 'block');
//Creation of Sprite
player = this.physics.add.sprite(75, 175, ‘dude’);
// player.setBounce(0.2);
// player.setCollideWorldBounds(true); //collides the player when player hits the platform
this.anims.create({
key: ‘left’, //key for the action
frames: this.anims.generateFrameNumbers(‘dude’, { start: 0, end: 3 }), //start and end of the sprite sheet
frameRate: 10, //fps
repeat: -1
});
this.anims.create({
key: ‘turn’, //key for the action
frames: [ { key: ‘dude’, frame: 4 } ], // which frame of the spritesheet to show in the game, here frame 4 indicates the idle frame image
frameRate: 20 // we can change the fps for teh character
});
this.anims.create({
key: ‘right’, //key for the action
frames: this.anims.generateFrameNumbers(‘dude’, { start: 5, end: 8 }), ////start and end of the sprite sheet
frameRate: 20, //fps
repeat: -1 //
});
cursors = this.input.keyboard.createCursorKeys(); //function to use the inbuilt keyboard manager to use the keyboard keys for specific action
stars = this.physics.add.group({ //stars physics to collect the star object using the the player
key: ‘star’,
repeat: 0,
setXY: { x: 325, y: 175, stepX:100 }
});
stars.children.iterate(function (child) { //this function let starts bounce on the platform
child.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8));
});
scoreText = this.add.text(16, 16, ‘score: 0’, { fontSize: ‘32px’, fill: ‘#000’ }); // create score counter and this will displa score:0
this.physics.add.collider(player, platforms); //creates a collision physics between player and platform
this.physics.add.collider(stars, platforms); //creates a collision physics between Stars and platform
this.physics.add.overlap(player, stars, collectStar, null, this); //check to see if the player overlaps with a star or not
}
function update ()
{
// movement of the character using keyboard input manager
if (cursors.left.isDown) // moving the character in left direction
{
player.setVelocityX(-230);
player.anims.play('left', true);
}
else if (cursors.right.isDown) //moving the character in right direction
{
player.setVelocityX(230);
player.anims.play('right', true);
}
else
{
player.setVelocityX(0); //when idle
player.anims.play('turn');
}
// if (cursors.up.isDown && player.body.touching.down) // Jumping the character
// {
// player.setVelocityY(-430);
// }
}
console.log(update());
function collectStar (player, star) //if player and star is overlapped then the player collects the star
{
star.disableBody(true, true);
$(’.modal’).modal(‘show’); // to trigger the modal
}