Created
March 5, 2014 13:49
-
-
Save SomMeri/9367499 to your computer and use it in GitHub Desktop.
immovable sprite full version
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
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<title>Phaser - Making your first game, part 1</title> | |
<script type="text/javascript" src="js/phaser.1.1.6.min.js"></script> | |
<style type="text/css"> | |
body { | |
margin: 0; | |
} | |
</style> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update }); | |
function preload() { | |
game.load.image('sky', 'assets/sky.png'); | |
game.load.image('ground', 'assets/platform.png'); | |
game.load.spritesheet('tree', 'assets/spooky_trees.png', 59, 148); | |
game.load.spritesheet('spider', 'assets/LPC_Spiders/spider01.png', 64, 64, 44);//, -1);//, -1, 10); | |
} | |
var platforms, player, trees; | |
function create() { | |
// The platforms group contains the ground and the 2 ledges we can jump on | |
platforms = game.add.group(); | |
var ground = platforms.create(0, game.world.height - 64, 'ground'); | |
ground.scale.setTo(2, 2); | |
ground.body.immovable = true; //TODO test with false | |
trees = game.add.group(); | |
var tree = trees.create(264, game.world.height - 200,'tree',10); | |
tree.body.immovable = true; | |
player = game.add.sprite(64, game.world.height - 500, 'spider'); | |
// Player physics properties. Give the little guy a slight bounce. | |
player.body.bounce.y = 0.2; | |
player.body.gravity.y = 1000; //this is ridiculously high | |
player.body.collideWorldBounds = true; | |
player.body.setRectangle(38, 38, 13, 13); | |
// Our two animations, walking left and right. | |
player.animations.add('left', [11, 12, 13, 14, 15, 16, 17, 18, 19, 20], 15, true); | |
player.animations.add('right', [31, 32, 33, 34, 35, 36, 37, 38, 39, 40], 15, true); | |
cursors = game.input.keyboard.createCursorKeys(); | |
} | |
function update() {//trees | |
game.physics.collide(player, platforms); | |
game.physics.collide(player, trees); | |
player.body.velocity.x = 0; | |
if (cursors.left.isDown) { | |
// Move to the left | |
player.body.velocity.x = -150; | |
player.animations.play('left'); | |
} else if (cursors.right.isDown) { | |
// Move to the right | |
player.body.velocity.x = 150; | |
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) { | |
player.body.velocity.y = -500; | |
} | |
} | |
</script> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment