简体   繁体   English

在单击的位置Phaser3上添加图像

[英]Add image on clicked place Phaser3

I want to create a game like Tic-tac toy, but can't set the correct position for the image (inside x() & y() function). 我想创建一个类似Tic-tac玩具的游戏,但无法为图像设置正确的位置(在x()y()函数内部)。 Clicking on a special platform(square) want to place x or y inside that platform. 单击特殊的平台(正方形)要在该平台内放置x或y。 In my case, it creates inside the first platform always. 就我而言,它总是在第一个平台内创建。

export default class A extends Phaser.Scene {
  constructor () {
    super(SCENE_GAME)
    this.counter = 0
    const gameOptions = {
      tileSize: 200,
    }
    const gameConfig = {
      width: gameOptions.tileSize * 3,
      height: gameOptions.tileSize * 3,
    }
  }

  create = () => {
    this.platformArray = []
    this.platformGroup = this.add.group()
    for (let i = 0; i < 3; i++) {
      this.platformArray[i] = []
      for (let j = 0; j < 3; j++) {
        this.platform = this.add.sprite(j * gameOptions.tileSize + gameOptions.tileSize / 2, i * gameOptions.tileSize + gameOptions.tileSize / 2, 'platform').setInteractive()
        this.platform.on('pointerdown', this.clickCounter, this)
      }
    }
  }

  clickCounter () {
    this.counter++
    if (this.counter % 2 === 1) {
      this.x()
    } else {
      this.y()
    }
  }

  x () {
    this.add.sprite(gameOptions.tileSize / 2, gameOptions.tileSize / 2, 'x')
  }

  y () {
    this.add.sprite(gameOptions.tileSize / 2, gameOptions.tileSize / 2, 'y')
  }

The reason that they're showing up in the same place is because you're using the tileSize only for the positioning. 它们出现在同一位置的原因是因为您仅将tileSize用于定位。

So if tileSize were 16, x() would always add a sprite at position 8, 8. 因此,如果tileSize为16, x()将始终在位置8、8处添加一个sprite。

In your for loop you have this: 在您的for循环中,您有以下内容:

this.platform = this.add.sprite(
    j * gameOptions.tileSize + gameOptions.tileSize / 2,
    i * gameOptions.tileSize + gameOptions.tileSize / 2, 'platform')
 .setInteractive()

You can see that you're taking the tileSize and multiplying by the grid position. 您会看到您正在使用tileSize并乘以网格位置。

I would recommend changing your x and y functions to accept either a number or a Pointer, which should be already be accessible in clickCounter() if you add the argument (adding a console.log(arguments); in clickCounter() should log out the arguments available). 我建议您将xy函数更改为接受数字或指针,如果您添加参数(添加console.log(arguments);clickCounter()应该注销clickCounter() ,则应该已经可以在clickCounter()访问它了可用的参数)。

Based upon the pointer's position you can then do the math to determine what tile was clicked on. 然后,根据指针的位置,可以进行数学运算以确定单击的图块。

Another alternative would be to add custom properties to each tile sprite, say a row and column , set to the appropriate i / j value. 另一种替代方法是向每个tile精灵添加自定义属性,例如将rowcolumn设置为适当的i / j值。 The clicked on sprite should then be accessible in clickCounter too as one of the arguments, and then you can just pull the custom properties. 然后,点击的子画面也应该作为参数之一在clickCounter可访问,然后您就可以提取自定义属性。

If you're using TypeScript, you'll want to create a custom object that extends Sprite. 如果您使用的是TypeScript,则需要创建一个扩展Sprite的自定义对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM