Guest User

Untitled

a guest
Jun 28th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var config = {
  2.     type: Phaser.AUTO,
  3.     width: 800,
  4.     height: 600,
  5.     physics: {
  6.         default: 'arcade',
  7.         arcade: {
  8.             gravity: {
  9.                 y: 300
  10.             },
  11.             debug: true
  12.         }
  13.     },
  14.     scene: {
  15.         preload: preload,
  16.         create: create,
  17.         update: update
  18.     }
  19. };
  20.  
  21. var groundLayer;
  22. var platformLayer;
  23. var ladderLayer;
  24. var player;
  25.  
  26. var startPoint;
  27. var endPoint;
  28.  
  29. var ladder = false;
  30.  
  31. var game = new Phaser.Game(config);
  32.  
  33. function preload() {
  34.  
  35.     // map made with Tiled in JSON format
  36.     this.load.tilemapTiledJSON('map', 'assets/Tiled-9.json');
  37.     this.load.spritesheet('tiles', 'assets/tiles64x64.png', {frameWidth: 64, frameHeight: 64});
  38.     this.load.spritesheet('ladder', 'assets/ladder64x64.png',{frameWidth: 64, frameHeight: 64});
  39.     this.load.atlas('player', 'assets/player.png', 'assets/player.json');
  40. }
  41.  
  42. function create() {
  43.     map = this.make.tilemap({key: 'map'});
  44.    
  45.  
  46.  
  47.     // Must match tileSets name above ( tiles64x64 )
  48.     var Tiles = map.addTilesetImage('tiles64x64','tiles');
  49.  
  50.     // create the ground layer
  51.     groundLayer = map.createStaticLayer('groundLayer', Tiles, 0, 0);
  52.     platformLayer = map.createStaticLayer('platformLayer', Tiles, 0, 0);
  53.  
  54.     // Set starting and ending position using name
  55.     startPoint = map.findObject("ObjectLayer", obj => obj.name === "startPoint");
  56.     endPoint = map.findObject("ObjectLayer", obj => obj.name === "endPoint");
  57.    
  58.     // create the player sprite    
  59.     player = this.physics.add.sprite(0, 0, 'player');
  60.     player.setBounce(0.1); // our player will bounce from items
  61.    
  62.     // small fix to our player images, we resize the physics body object slightly
  63.     player.body.setSize(player.width*0.8, player.height*0.8);
  64.     player.setCollideWorldBounds(true); // don't go out of the map  
  65.  
  66.     // Set player to starting position
  67.     player.setPosition(startPoint.x, startPoint.y);  
  68.    
  69.     // set the boundaries of our game world
  70.     this.physics.world.bounds.width = groundLayer.width;
  71.     this.physics.world.bounds.height = groundLayer.height;
  72.  
  73.     // the player will collide with this layer
  74.     groundLayer.setCollisionByProperty({ collides: true });
  75.     platformLayer.setCollisionByProperty({ collides: true });
  76.  
  77.     // Collides with platform and ground
  78.     this.physics.add.collider(groundLayer, player);
  79.     this.physics.add.collider(platformLayer, player);
  80.  
  81.     // Add ladder tiles & layers
  82.     var ladderTiles = map.addTilesetImage('ladder64x64','ladder');
  83.     ladderLayer = map.createStaticLayer('ladderLayer', ladderTiles, 0, 0);
  84.  
  85.     // This one not working
  86.     var ladderGroup = this.physics.add.group(ladderLayer);
  87.    
  88.     // If overlapped with ladder, call the function
  89.     this.physics.add.overlap(player, ladderGroup, function (player) {
  90.         console.log('ladder overlap',player.x,player.y);              
  91.     });
  92.  
  93.     // player walk animation
  94.     this.anims.create({
  95.         key: 'walk',
  96.         frames: this.anims.generateFrameNames('player', {prefix: 'p1_walk', start: 1, end: 11, zeroPad: 2}),
  97.         frameRate: 10,
  98.         repeat: -1
  99.     });
  100.     // idle with only one frame, so repeat is not neaded
  101.     this.anims.create({
  102.         key: 'idle',
  103.         frames: [{key: 'player', frame: 'p1_stand'}],
  104.         frameRate: 10,
  105.     });
  106.  
  107.     cursors = this.input.keyboard.createCursorKeys();
  108.  
  109.   // set bounds so the camera won't go outside the game world
  110.   this.cameras.main.setBounds(0, 0, map.widthInPixels, map.heightInPixels);
  111.   // make the camera follow the player
  112.   this.cameras.main.startFollow(player);
  113.  
  114.   // set background color, so the sky is not black    
  115.   this.cameras.main.setBackgroundColor('#ccccff');
  116.  
  117. }
  118.  
  119.  
  120. function onLadder() {
  121.     ladder = true;
  122.     console.log('On ladder ', ladder);
  123. }
  124.  
  125.  
  126. function update() {
  127.  
  128.     ladder = false;
  129.  
  130.     if (cursors.left.isDown)
  131.     {
  132.         player.body.setVelocityX(-200);
  133.         player.anims.play('walk', true); // walk left
  134.         player.flipX = true; // flip the sprite to the left
  135.     }
  136.     else if (cursors.right.isDown)
  137.     {
  138.         player.body.setVelocityX(200);
  139.         player.anims.play('walk', true);
  140.         player.flipX = false; // use the original sprite looking to the right
  141.     } else {
  142.         player.body.setVelocityX(0);
  143.         player.anims.play('idle', true);
  144.     }
  145.     // jump
  146.     if (cursors.up.isDown && player.body.onFloor())
  147.     {
  148.         player.body.setVelocityY(-500);        
  149.     }
  150.  
  151.     //console.log('player ', player.x, player.y);
  152.  
  153.     // Check for reaching endPoint object
  154.     if ( player.x >= endPoint.x && player.y >= endPoint.y ) {
  155.         console.log('Reached endPoint');
  156.     }
  157.    
  158. }
Advertisement
Add Comment
Please, Sign In to add comment