/** * Generated from the Phaser Sandbox * * //phaser.io/sandbox/lKrEaNmk * * This source requires Phaser 2.6.2 */ var game = new Phaser.Game(600, 450, Phaser.AUTO, '', { preload: preload, create: create, update: update, render: render }); function preload() { game.load.baseURL = 'http://examples.phaser.io/assets/'; game.load.crossOrigin = 'anonymous'; game.load.image('sky', 'particlestorm/sky10.png'); game.load.image('ground', 'sprites/platform.png'); game.load.image('star', 'games/starstruck/star2.png'); game.load.spritesheet('dude', 'games/starstruck/dude.png', 32, 48); } // (A valiable of the group contains the ground and the 2 ledges) var platforms; // (A valiable of the player) var player; // (A valiable of the keyboard cursor) var cursors; // (A valiable of the group contains the stars) var stars; // (A Valiable of the score) var score = 0; // (A Valiable of the Text) var scoreText; function create() { cursors = game.input.keyboard.createCursorKeys(); game.forceSingleUpdate = false; // We're going to be using physics, so enable the Arcade Physics system game.physics.startSystem(Phaser.Physics.ARCADE); // A simple background for our game let sky = game.add.sprite(0, 0, 'sky'); // (Scale it to fit the game screen.) sky.scale.setTo(0.75, 0.75); // The platforms group contains the ground and the 2 ledges we can jump on platforms = game.add.group(); // We will enable physics for any object that is created in this group platforms.enableBody = true; // Here we create the ground. let ground = platforms.create(0, game.world.height - 32, 'ground'); // Scale it to fit the width of the game (the original sprite is 400x32 in size) ground.scale.setTo(1.5, 0.5); // This stops it from falling away when you jump on it ground.body.immovable = true; // Now let's create two ledges let ledge = platforms.create(300, 300, 'ground'); ledge.scale.setTo(0.75, 0.5); ledge.body.immovable = true; ledge = platforms.create(-110, 185, 'ground'); ledge.scale.setTo(0.75, 0.5); ledge.body.immovable = true; // The player and its settings player = game.add.sprite(32, game.world.height - 120, 'dude'); player.scale.setTo(0.75, 0.75); // We need to enable physics on the player game.physics.arcade.enable(player); // Player physics properties. Give the little guy a slight bounce. player.body.bounce.y = 0.2; player.body.gravity.y = 225; player.body.collideWorldBounds = true; // Our two animations, walking left and right. player.animations.add('left', [0, 1, 2, 3], 10, true); player.animations.add('right', [5, 6, 7, 8], 10, true); stars = game.add.group(); stars.enableBody = true; // Here we'll create 12 of them evenly spaced apart for (let i = 0; i < 12; i++) { // Create a star inside of the 'stars' group let star = stars.create(i * 50, 0, 'star'); // Let gravity do its thing star.body.gravity.y = 6; // This just gives each star a slightly random bounce value star.body.bounce.y = 0.7 + Math.random() * 0.2; } scoreText = game.add.text(12, 12, 'Score: 0', { font: "24px", fill: "#000" }); } function update() { var hitPlatform = game.physics.arcade.collide(player, platforms); game.physics.arcade.collide(stars, platforms); game.physics.arcade.overlap(player, stars, collectStar, null, this); // Reset the players velocity (movement) player.body.velocity.x = 0; if (cursors.left.isDown) { // Move to the left player.body.velocity.x = -120; player.animations.play('left'); } else if (cursors.right.isDown) { // Move to the right player.body.velocity.x = 120; player.animations.play('right'); } else { // Stand still player.animations.stop(); player.frame = 4; } // Allow the player to jump if they are touching the ground. if (cursors.up.isDown && player.body.touching.down && hitPlatform) { player.body.velocity.y = -250; } } function collectStar(player, star) { // Removes the star from the screen star.kill(); // Add and update the score score += 10; scoreText.text = 'Score: ' + score; } function render() { }