Advertisement
metalx1000

Phaser Tile Mapping Example

Jul 6th, 2015
727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var game = new Phaser.Game(640, 480, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update});
  2.  
  3. function preload() {
  4.  
  5.     game.load.tilemap('map', 'level.csv', null, Phaser.Tilemap.CSV);
  6.     game.load.image('tiles', '16.png');
  7.     game.load.image('player', 'player.png');
  8.  
  9. }
  10.  
  11. var map;
  12. var layer;
  13. var cursors;
  14. var player;
  15.  
  16. function create() {
  17.  
  18.     //  Because we're loading CSV map data we have to specify the tile size here or we can't render it
  19.     map = game.add.tilemap('map', 64, 64);
  20.  
  21.     //  Now add in the tileset
  22.     map.addTilesetImage('tiles');
  23.     map.setCollisionBetween(3, 4);
  24.     map.setCollisionBetween(1, 1);
  25.  
  26.     //  Create our layer
  27.     layer = map.createLayer(0);
  28.  
  29.     //  Resize the world
  30.     layer.resizeWorld();
  31.  
  32.     // Player
  33.     player = game.add.sprite(0,0,"player");
  34.     game.physics.enable(player);
  35.     game.physics.arcade.gravity.y = 200;
  36.     player.body.bounce.set(0.6);
  37.     //keep player on screen
  38.     player.body.collideWorldBounds=true;
  39.     //enable keyboard input
  40.     cursors = game.input.keyboard.createCursorKeys();
  41.  
  42. }
  43.  
  44. function update() {
  45.   game.physics.arcade.collide(player, layer);
  46.   playerControls();
  47. }
  48.  
  49. function playerControls(){
  50.     if (cursors.up.isDown)
  51.     {
  52.         player.body.velocity.y = -150;
  53.     }
  54.     else if (cursors.down.isDown)
  55.     {
  56.         player.body.velocity.y = 150;
  57.     }
  58.  
  59.     if (cursors.left.isDown)
  60.     {
  61.         player.body.velocity.x = -150;
  62.     }
  63.     else if (cursors.right.isDown)
  64.     {
  65.         player.body.velocity.x = 150;
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement