phaser3学习笔记:scene数据交换

代码素材后续提供下载链接

import Phaser from 'phaser'

class GameScene extends Phaser.Scene {
  constructor() {
    super('GameScene')
    this.score = 0
    this.lives = 6
  }

  preload() {
    this.load.image('bg', 'assets/example/scene/sky4.png')
    this.load.image('crate', 'assets/example/scene/crate.png')
  }

  create() {
    // Store the score and lives in the Game Registry
    // 把数据存到Game Registry里
    this.registry.set('score', this.score)
    this.registry.set('lives', this.lives)

    // 添加背景图片
    this.add.image(400, 300, 'bg')
    // 添加箱子
    for (let i = 0; i < 64; i++) {
      let x = Phaser.Math.Between(0, 800)
      let y = Phaser.Math.Between(0, 600)
      // 添加,并设置可互动
      let box = this.add.image(x, y, 'crate').setInteractive()
      // 一半的箱子颜色为红色
      if (i % 2) {
        box.setTint(0xff0000)
      }
    }
    // 点击箱子事件
    this.input.on('gameobjectup', this.clickHandler, this)
  }

  clickHandler(pointer, box) {
    console.log(box)
    // 没有生命
    if (this.lives === 0) {
      return
    }
    // 箱子消失
    box.input.enable = false
    box.setVisible(false)
    // 销毁box
    // box.destory()

    // if the box was tinted red, you lose a life
    // 点中红色箱子,生命减1
    if (box.tintTopLeft === 255) {
      this.lives--
      this.registry.set('lives', this.lives)
    } else {
      // 点击普通箱子,增加分数
      this.score++
      this.registry.set('score', this.score)
    }
  }
}

class UIScene extends Phaser.Scene {
  constructor() {
    // 不是第一个scene,要显示,需要active:true, 后面的scene层级在前的scene前面
    super({ key: 'UIScene', active: true })
    this.scoreText
    this.livesText
  }

  create() {
    // text object to display the score
    this.scoreText = this.add.text(10, 10, 'Score: 0', { font: '32px Arial', fill: '#000000' })
    this.livesText = this.add.text(10, 48, 'Lives: 6', { font: '32px Arial', fill: '#000000' })
    // check the registry and hit our callback every time the 'score' value is updated
    // 监听registry值变化
    this.registry.events.on('changedata', this.updateData, this)

    // 从一个scene 启动另一个scene,
    // this.scene.start('PassDataScene') // start 方式,其他的scene会直接消失
    // 启动 其他的scene还在,launch之后,可以设置visible来显示和隐藏
    this.scene.launch('PassDataScene')
    console.log(this.scene.isVisible('PassDataScene'))
    console.log(this.scene.isActive('PassDataScene'))
    setTimeout(() => {
      this.scene.setVisible(false, 'PassDataScene')
      setTimeout(()=>{
        this.scene.setVisible(true, 'PassDataScene')
      },5000)
    }, 5000)
  }

  updateData(parent, key, data) {
    if (key === 'score') {
      this.scoreText.setText('Score: ' + data)
    } else if (key === 'lives') {
      this.livesText.setText('Lives: ' + data)
    }
  }
}

class PassDataScene extends Phaser.Scene {
  constructor() {
    super({ key: 'PassDataScene', active: false })
  }

  create() {
    this.add.text(20, 100, 'launch from other scene', { font: '32px Arial', fill: '#000000' })
  }
}

export function DataExchangeInit(ele) {
  return new Phaser.Game({
    type: Phaser.AUTO,
    width: 800,
    height: 600,
    backgroundColor: '#000',
    parent: ele,
    scene: [GameScene, UIScene, PassDataScene]
  })
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章