Last active
June 28, 2016 01:40
-
-
Save jeffersonbenson/626e766390cf19bfa4886b4da1579e54 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(640, 640, Phaser.CANVAS, '', { preload: preload, create: create, update: update }); | |
function preload() { | |
//the first parameter is what we'll call it, the second is what it actually is. | |
game.load.tilemap('map', 'Assets/Tilemaps/newSnow.json', null, Phaser.Tilemap.TILED_JSON); | |
game.load.image('tileset', 'Assets/Tilemaps/snowTiles.png'); | |
game.load.image('penguin', 'Assets/penguin.jpg'); | |
game.load.image('movement', 'Assets/movementOutline.png'); | |
var map; | |
var tileset; | |
var penguin; | |
var Background; | |
var Foreground; | |
} | |
function create() { | |
map = game.add.tilemap('map'); | |
//first parameter is what to look for in json, within tilesets:name. | |
//the second is what to apply it to (in this case the tileset image referred to as tileset) | |
map.addTilesetImage('Snow1', 'tileset'); | |
// map.setCollision([29], true); | |
Background = map.createLayer('Background'); | |
Foreground = map.createLayer('Foreground'); | |
map.setCollisionBetween(1, 100, true, Foreground); | |
Background.resizeWorld(); | |
//game.add.sprite(0, 0, 'background'); | |
game.stage.backgroundColor = '#71c5cf'; | |
player = game.add.sprite(0, 0, 'penguin'); | |
movement = game.add.sprite (0,32, 'movement'); | |
//BUG:anchor will not change to center. sprite is off center in grid | |
player.anchor.setTo(.5); | |
//this is supposed to snap to the grid. | |
player.inputEnabled = true; | |
movement.inputEnabled = true; | |
movement.input.enableDrag(); | |
player.input.enableSnap(32, 32, true, true); | |
movement.input.enableSnap(32, 32, true, true); | |
game.physics.enable(player, Phaser.Physics.ARCADE); | |
player.body.collideWorldBounds = true; | |
marker = game.add.graphics(); | |
marker.lineStyle(2, 0x000000, 1); | |
marker.drawRect(0, 0, 32, 32); | |
cursors = game.input.keyboard.createCursorKeys(); | |
} | |
function update() { | |
marker.x = Background.getTileX(game.input.activePointer.worldX) * 32; | |
marker.y = Background.getTileY(game.input.activePointer.worldY) * 32; | |
movement.events.onDragStop.add(function(){console.log("Moved!");}, this); | |
if (game.input.mousePointer.isDown) { | |
console.log("clicked!"); | |
player.body.x = game.input.mousePointer.x; | |
player.body.y = game.input.mousePointer.y; | |
} | |
// var dirMove = 4; | |
// if (cursors.left.isDown) | |
// { | |
// player.body.x -= dirMove; | |
// } | |
// else if (cursors.right.isDown) | |
// { | |
// player.body.x += dirMove; | |
// } | |
// else if (cursors.up.isDown) | |
// { | |
// player.body.y -= dirMove; | |
// } | |
// else if (cursors.down.isDown) | |
// { | |
// player.body.y += dirMove; | |
// } | |
// else | |
// { | |
// player.animations.stop(); | |
// } | |
game.physics.arcade.collide(player, Foreground); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment