Advertisement
metalx1000

Phaser basic tile map jumper with collision

Jul 7th, 2015
785
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var game = new Phaser.Game(480, 320, 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', 'tiles64.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.     //  Because we're loading CSV map data we have to specify the tile size here or we can't render it
  18.     map = game.add.tilemap('map', 32, 32);
  19.  
  20.     //  Now add in the tileset
  21.     map.addTilesetImage('tiles');
  22.     map.setCollisionBetween(3, 4);
  23.  
  24.     //  Create our layer
  25.     layer = map.createLayer(0);
  26.  
  27.     //  Resize the world
  28.     layer.resizeWorld();
  29.  
  30.     // Player
  31.     player = game.add.sprite(32,32,"player");
  32.     player.scale.setTo(.5,.5);
  33.     game.physics.enable(player, Phaser.Physics.ARCADE);
  34.     game.camera.follow(player);
  35.     game.physics.arcade.gravity.y = 200;
  36.     //keep player on screen
  37.     //player.body.collideWorldBounds=true;
  38.     //enable keyboard input
  39.     cursors = game.input.keyboard.createCursorKeys();
  40.  
  41.     //fullscreen on click
  42.     game.input.onDown.add(go_fullscreen, this);
  43. }
  44.  
  45. function update() {
  46.   game.physics.arcade.collide(player, layer);
  47.   playerControls();
  48. }
  49.  
  50. function playerControls(){
  51.     if (cursors.up.isDown && player.body.blocked.down)
  52.     {
  53.         player.body.velocity.y = -300;
  54.     }
  55.     else if (cursors.down.isDown)
  56.     {
  57.         //player.body.velocity.y = 550;
  58.     }
  59.  
  60.     if (cursors.left.isDown)
  61.     {
  62.         player.body.velocity.x = -150;
  63.     }
  64.     else if (cursors.right.isDown)
  65.     {
  66.         player.body.velocity.x = 150;
  67.     }else{
  68.         player.body.velocity.x = 0
  69.     }
  70. }
  71.  
  72. function go_fullscreen(){
  73.   game.scale.fullScreenScaleMode = Phaser.ScaleManager.SHOW_ALL;
  74.   game.scale.startFullScreen();
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement