Phaser set group sprites的锚点属性

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Phaser set group sprites的锚点属性相关的知识,希望对你有一定的参考价值。

我是Phaser的新手,有一个简短的问题。我有一个名为cups的组对象,并且向其中添加了6个精灵。但是,如果每个子画面都没有将其锚属性设置为0.5,0.5,则它们也不会放置在我想要的位置。将其添加到组中后,我可以更改每个精灵锚,但是我觉得必须有更好的方法,例如

var myGroup = game.add.group();
..add sprites here
myGroup.anchor.setTo(0.5,0.5);

这是我当前的代码。

window.onload = function() {

    var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });

    function preload() {
        game.load.image('table', 'assets/img/table.png');
        game.load.image('cup', 'assets/img/cup.png');
        game.load.image('ball', 'assets/img/ball.png');
    }

    function create() {
        var table = game.add.sprite(game.world.centerX, game.world.centerY, 'table');
        table.anchor.setTo(0.5,0.5);

        var cupW = game.cache.getImage('cup').width;
        var cupH = game.cache.getImage('cup').height;
        var cups = game.add.group();

        var cup = cups.create(game.world.centerX, cupH / 2, 'cup');
        cup.anchor.setTo(0.5,0.5);
        cup = cups.create(game.world.centerX - cupW, cupH / 2, 'cup');
        cup.anchor.setTo(0.5,0.5);
        cup = cups.create(game.world.centerX + cupW, cupH / 2, 'cup');
        cup.anchor.setTo(0.5,0.5);
        cup = cups.create(game.world.centerX - cupW / 2, cupH + (cupH / 2), 'cup');
        cup.anchor.setTo(0.5,0.5);
        cup = cups.create(game.world.centerX + cupW / 2 , cupH + (cupH / 2), 'cup');
        cup.anchor.setTo(0.5,0.5);
        cup = cups.create(game.world.centerX, (cupH * 2) + (cupH / 2), 'cup');
        cup.anchor.setTo(0.5,0.5);

        var ball = game.add.sprite(game.world.centerX, game.world.centerY,'ball');
        ball.anchor.setTo(0.5,0.5);

    }

    function update() {

    }

}
答案

当您将项目添加到组中时,它们成为子项,您可以在group.children

另一答案

下面是您的代码示例,这种方式更容易=>

以上是关于Phaser set group sprites的锚点属性的主要内容,如果未能解决你的问题,请参考以下文章

更改 Phaser.Sprite 某些颜色的不透明度

pygame.sprite.Group

在 Phaser 中销毁精灵

Phaser 2 - 如何检测我何时拖动另一个精灵?

尝试从 spritesheet 为 sprite Phaser 3 设置动画 - 出现错误无法读取未定义的属性“框架”

Phaser Framework - 检查精灵总旋转