简体   繁体   English

如何在 Phaser 3 中正确调整精灵的大小(最新)

[英]How to properly size a sprite in Phaser 3 (latest)

I'm creating a hex grid made with Graphics and then generating a texture from that:我正在创建一个用 Graphics 制作的六角网格,然后从中生成一个纹理:

function GraphicsCreator(scene, hexShape, hex) {
  const graphics = scene.add.graphics({ x: 0, y: 0, add: false });

  graphics.lineStyle(3, hex.outlineColour)

  graphics.beginPath();
  graphics.moveTo(hexShape.points[0].x, hexShape.points[0].y);

  for (let i = 1; i < hexShape.points.length; i++) {
    graphics.lineTo(hexShape.points[i].x, hexShape.points[i].y);
  }

  graphics.closePath();
  graphics.strokePath();
  graphics.fillStyle(hex.colour);
  graphics.fillPoints(hexShape.points, true);
  graphics.setDepth(1);

  graphics.generateTexture('hex')

  graphics.destroy();
}

I'm then trying to get that texture for each hex and map it to the proper location which is all working fine.然后我尝试为每个十六进制获取该纹理并将其映射到正确的位置,这一切都很好。

aIHand.forEach(hex => {
  const point = hex.toPoint()
  const corners = hex.corners().map(corner => corner.add(point))
  const hexShape = new Phaser.Geom.Polygon(corners);

  hex.originalPos = point
  hex.id = aiCardNumber

  if (hex.id == 1) {
    GraphicsCreator(this, hexShape, hex)
  }

  const sprite = TextureGetter(this, hex)
  const gameObjectContainer = CreateGameObjectContainer(this, hexShape, hex, true, sprite)

  DebugBounds(sprite, this)

  InputManager(this, gameObjectContainer)
  //aiContainer.add(gameObjectContainer)
  aiCardNumber++
})

function TextureGetter(scene, hex) {
  const sprite = scene.add.sprite(hex.originalPos.x, hex.originalPos.y, 'hex')
  sprite.setDisplayOrigin(0)

  return sprite
}

function DebugBounds(obj, scene) {
  let render = scene.add.graphics();
  let bounds = obj.getBounds();
  render.lineStyle(3, 0xffff37);
  render.strokeRectShape(bounds);
  console.log(obj)
}

When I DebugBounds() for each sprite and also log them, I can see that displayHeight and displayWidth are the size of the entire screen and changing this or scale reduces the image to be basically non-visible as if it is the corner of the entire screen bounds.当我为每个精灵 DebugBounds() 并记录它们时,我可以看到 displayHeight 和 displayWidth 是整个屏幕的大小,并且更改它或缩放会使图像基本上不可见,就好像它是整个屏幕的一角屏幕边界。 I really don't get this, please see the screenshot below我真的不明白,请看下面的截图

记录所有精灵并 DebugBounds() 显示

As far as I'm concerned, the process should be;就我而言,这个过程应该是; CreateGraphics -> GenerateTexture -> loop through array and place texture in position -> bounds should be the size of the texture/graphic. CreateGraphics -> GenerateTexture -> 循环遍历数组并将纹理放置在位置 -> 边界应该是纹理/图形的大小。 What am I doing wrong here?我在这里做错了什么?

Here is also the config incase there is something here causing it, though I've tried removing/changing scale settings这也是配置,因为这里有什么导致它,虽然我已经尝试删除/更改比例设置

const config = {
  type: Phaser.AUTO,
  width: 1920,
  height: 1080,
  scale: {
    mode: Phaser.Scale.FIT,
    parent: 'phaser-example',
    autoCenter: Phaser.Scale.CENTER_BOTH,
    width: 1920,
    height: 1080
  },
  backgroundColor: '#303352',
  physics: {
    default: 'arcade',
    arcade: {
      gravity: { y: 0 },
      debug: true
    }
  },
  scene: {
    preload: preload,
    create: create,
    update: update
  },
};

Thanks谢谢

Thanks to Phaser Discord, graphics.generateTexture needs to have an explicit size感谢 Phaser Discord,graphics.generateTexture 需要有一个明确的大小

so graphics.generateTexture('hex', 30, 30) for example!例如 graphics.generateTexture('hex', 30, 30) !

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

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