【问题标题】:How to add sprites around the perimeter of a circle in phaser-framework如何在移相器框架中围绕圆的周边添加精灵
【发布时间】:2018-03-17 19:20:15
【问题描述】:

有一个游戏世界。而在游戏世界里面,还有一个小圈子。如何在周边周围添加一圈精灵,使其不会超出圆圈。

【问题讨论】:

    标签: javascript phaser-framework


    【解决方案1】:

    如果我理解正确,您的问题是关于如何计算圆的坐标的更笼统的问题。

    通常,您可以使用 sine 和 cosine 计算圆的位置,而在 JavaScript 和 Phaser 中,您可以这样做:

    addSpritesInCircle: function(xpivot, ypivot, radius, amount) {
    
        // add sprites in a circle
        for (var i = 0; i < amount; i++) {
            // divide sprites evenly around the circle
            var angle = 360.0 * (i / amount);
    
            // calculate circle coordinates
            var xpos = xpivot + (Math.cos(2 * Math.PI * angle / 360.0) * radius);
            var ypos = ypivot + (Math.sin(2 * Math.PI * angle / 360.0) * radius);
    
            // add phaser sprite
            var sprite = this.game.add.sprite(xpos, ypos, 'mysprites', 'alienfoe1');
    
            // optionally also add to a group for handling/updates later
            this.myAliensGroup.add(sprite);
        };
    }
    

    【讨论】:

      猜你喜欢
      • 2022-09-27
      • 1970-01-01
      • 2018-03-26
      • 1970-01-01
      • 2017-06-13
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 2022-11-23
      相关资源
      最近更新 更多