Rodrigocs

Sprite not colliding with top World Bounds

Recommended Posts

Hello, I've started using phaser just recently, I'm making a RPG game, and so far I've been learning how to make tilemaps, use spritesheets, animate sprites, and I'm falling in love with the framework, I just have a little problem.

My player is set to collide with World Bounds, and it works for every bound, except the top one.

I'm using the es6 webpack template, here's my, so far piece of code.

create() method of my Game.js class. (I have nothing else in update, since I manage that in my own Sprite Class)

    this.game.world.setBounds(0, 0, 100 * 32, 100 * 32)
    this.game.physics.startSystem(Phaser.Physics.P2JS)

    this.game.time.advancedTiming = true
    this.game.stage.smoothed = false
    this.game.physics.p2.setImpactEvents(true)

    /* SET GAME MAP */
    this.map = game.add.tiledmap('map')
    this.player = new Player({
      game: this.game,
      x: 0,
      y: 0,
      lookId: 0,
      health: 100,
      mana: 100
    })
    this.game.world.setBounds(0, 0, 100 * 32, 100 * 32)

Sprite Class 

import Phaser from 'phaser'
import { Outfits } from './Outfits'

const IMAGES = '../../assets/images/'

const LOOK = {
  UP: 0,
  DOWN: 4,
  RIGHT: 2,
  LEFT: 6
}

export default class extends Phaser.Sprite {
  constructor ({ game, x, y, lookId, health, mana }) {
    super(
      game,
      x * 32,
      y * 32,
      Outfits[lookId].sheets[0],
      Outfits[lookId].frame
    )

    this.outfit = Outfits[lookId]
    this.mana = mana
    this.health = health
    this.smoothed = false
    this.anchor.setTo(1, 1)
    game.physics.p2.enable(this)
    this.body.setRectangle(32, 32)
    this.body.debug = true
    this.body.fixedRotation = true
    this.body.offset.x = -16
    this.body.offset.y = -16
    this.cursors = game.input.keyboard.createCursorKeys()
    game.camera.follow(this)
    game.add.existing(this)
  }

  /**
   *  SETS A NEW FRAME TO THE PLAYER SPRITE LOOKING
   */
  playerPosition (difference) {
    let spriteId = this.outfit.frame + difference
    let lastSprite = 143
    if (spriteId > lastSprite) {
      this.loadTexture(this.outfit.sheets[1])
      return spriteId - lastSprite
    } else {
      this.loadTexture(this.outfit.sheets[0])
      return spriteId
    }
  }

  update () {
    this.body.setZeroVelocity()
    console.log('World position', this.worldPosition)

    // this.animations.play('moveUp', 5, true)
    if (this.cursors.up.isDown) {
      this.frame = this.playerPosition(LOOK.UP)

      // if (this.worldPosition.y <= 0) return
      this.body.moveUp(1000)
      // this.animations.play('up', 5, true)
    }
    if (this.cursors.right.isDown) {
      this.body.moveRight(1000)
      // this.animations.play('right', 5, true)
      this.frame = this.playerPosition(LOOK.RIGHT)
    }

    if (this.cursors.down.isDown) {
      this.body.moveDown(1000)
      this.frame = this.playerPosition(LOOK.DOWN)
      // this.animations.play('down', 5, true)
    }
    if (this.cursors.left.isDown) {
      this.body.moveLeft(1000)
      this.frame = this.playerPosition(LOOK.LEFT)
      // this.animations.play('left', 5, true)
    }
    //
    // if (this.game.camera.atLimit.x) {
    //   console.log('LimitX', this.game.world)
    //   // starfield.tilePosition.x -= (ship.body.velocity.x * game.time.physicsElapsed);
    // }
    //
    // if (this.game.camera.atLimit.y) {
    //   console.log('LimitY', this.game.world)
    //   //  starfield.tilePosition.y -= (ship.body.velocity.y * game.time.physicsElapsed);
    // }
    //   this.game.input.keyboard.onUpCallback = e => {
    //     this.player.animations.stop()
    //     if (e.keyCode == Phaser.Keyboard.UP) {
    //       this.player.frame = this.playerStartingFrame + 3
    //     }
    //     if (e.keyCode == Phaser.Keyboard.DOWN) {
    //       this.player.frame = this.playerStartingFrame
    //     }
    //     if (e.keyCode == Phaser.Keyboard.LEFT) {
    //       this.player.frame = this.playerStartingFrame + 9
    //     }

    //     if (e.keyCode == Phaser.Keyboard.RIGHT) {
    //       this.player.frame = this.playerStartingFrame + 6
    //     }
    // }
  }
}

 

In summary my player does collide with world bounds but only bottom, left and right, not top.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.