Created
September 10, 2016 10:24
-
-
Save flash1293/5a5f7743719cc1c0bd69251a3feaf513 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render }); | |
function preload() { | |
game.load.spritesheet('dude', 'assets/games/starstruck/dude.png', 32, 48); | |
game.load.image('background', 'assets/games/starstruck/background2.png'); | |
game.load.spritesheet('tile', 'assets/games/starstruck/tiles-1.png', 32, 32); | |
} | |
var player; | |
var facing = 'left'; | |
var jumpTimer = 0; | |
var cursors; | |
var jumpButton; | |
var bg; | |
var tiles; | |
function create() { | |
game.physics.startSystem(Phaser.Physics.ARCADE); | |
game.time.desiredFps = 30; | |
bg = game.add.tileSprite(0, 0, 800, 600, 'background'); | |
game.physics.arcade.gravity.y = 600; | |
player = game.add.sprite(32, 32, 'dude'); | |
game.physics.enable(player, Phaser.Physics.ARCADE); | |
player.body.bounce.y = 0.2; | |
player.body.collideWorldBounds = true; | |
player.body.setSize(20, 32, 5, 16); | |
player.animations.add('left', [0, 1, 2, 3], 10, true); | |
player.animations.add('turn', [4], 20, true); | |
player.animations.add('right', [5, 6, 7, 8], 10, true); | |
tiles = game.add.group(); | |
var j = 5; | |
for (var i = 0; i < 100; i++) { | |
tile = game.add.sprite(30+32*i, 30+32*j, 'tile'); | |
game.physics.enable(tile, Phaser.Physics.ARCADE); | |
tiles.add(tile); | |
tile.body.immovable = true; | |
tile.body.gravity = new Phaser.Point(-100,-600); | |
tile.body.maxVelocity = new Phaser.Point(50,50); | |
tile.body.setSize(33, 32, 0, 0); | |
} | |
j = 4 | |
for (var i = 30; i < 100; i++) { | |
tile = game.add.sprite(30+32*i, 30+32*j, 'tile'); | |
game.physics.enable(tile, Phaser.Physics.ARCADE); | |
tiles.add(tile); | |
tile.body.immovable = true; | |
tile.body.gravity = new Phaser.Point(-100,-600); | |
tile.body.maxVelocity = new Phaser.Point(50,50); | |
tile.body.setSize(33, 32, 0, 0); | |
} | |
cursors = game.input.keyboard.createCursorKeys(); | |
jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); | |
} | |
function update() { | |
game.physics.arcade.collide(player, tiles); | |
player.body.velocity.x = 0; | |
if (cursors.left.isDown) | |
{ | |
player.body.velocity.x = -150; | |
if (facing != 'left') | |
{ | |
player.animations.play('left'); | |
facing = 'left'; | |
} | |
} | |
else if (cursors.right.isDown) | |
{ | |
player.body.velocity.x = 150; | |
if (facing != 'right') | |
{ | |
player.animations.play('right'); | |
facing = 'right'; | |
} | |
} | |
else | |
{ | |
if (facing != 'idle') | |
{ | |
player.animations.stop(); | |
if (facing == 'left') | |
{ | |
player.frame = 0; | |
} | |
else | |
{ | |
player.frame = 5; | |
} | |
facing = 'idle'; | |
} | |
} | |
if (jumpButton.isDown && player.body.touching.down && game.time.now > jumpTimer) | |
{ | |
player.body.velocity.y = -250; | |
jumpTimer = game.time.now + 750; | |
} | |
} | |
function render () { | |
game.debug.text(game.time.suggestedFps, 32, 32); | |
// game.debug.text(game.time.physicsElapsed, 32, 32); | |
// game.debug.body(player); | |
// game.debug.bodyInfo(player, 16, 24); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment