球在单击所需按钮时未按预期方式工作,Phaser 3。

huangapple go评论87阅读模式
英文:

the ball does not function as desired when clicked on the desired button, Phaser 3

问题

这段代码似乎有问题,当我添加这段代码时,一个按钮需要减速,一个按钮需要加速。

this.downButton = this.add.image(80, 530, 'up-bubble').setInteractive();
this.upButton = this.add.image(230, 530, 'down-bubble').setInteractive();
this.input.on('gameobjectup', function (pointer, gameobject) {

    if (gameobject === this.downButton && this.spinSpeed > 0)
    {
        this.spinSpeed -= 0.1;
    }
    else if (gameobject === this.upButton && this.spinSpeed < 9.9)
    {
        this.spinSpeed += 0.1;
    }

});

但是,当我将这段代码放在generateBalls()之间时,它根本不起作用。

generateBalls() {
    // ... 其他代码 ...

    this.downButton = this.add.image(80, 530, 'up-bubble').setInteractive();
    this.upButton = this.add.image(230, 530, 'down-bubble').setInteractive();
    this.input.on('gameobjectup', function (pointer, gameobject) {

        if (gameobject === this.downButton && this.spinSpeed > 0)
        {
            this.spinSpeed -= 0.1;
        }
        else if (gameobject === this.upButton && this.spinSpeed < 9.9)
        {
            this.spinSpeed += 0.1;
        }

    });

    // ... 其他代码 ...
}

请检查是否有任何错误或警告消息,以便进一步排查问题。

英文:

there seems to be something wrong with the code, one button has to slow down and one button to speed up when I add this code,

    this.downButton = this.add.image(80, 530, &#39;up-bubble&#39;).setInteractive();
    this.upButton = this.add.image(230, 530, &#39;down-bubble&#39;).setInteractive();
    this.input.on(&#39;gameobjectup&#39;, function (pointer, gameobject) {

        if (gameobject === this.downButton &amp;&amp; this.spinSpeed &gt; 0)
        {
            this.spinSpeed -= 0.1;
        }
        else if (gameobject === this.upButton &amp;&amp; this.spinSpeed &lt; 9.9)
        {
            this.spinSpeed += 0.1;
        }

    });   

but, when I add this code between generateBalls (), it doesn't work at all, it doesn't work,

   generateBalls() {

    const hitArea = new Phaser.Geom.Rectangle(0, 0, 32, 32);
    const hitAreaCallback = Phaser.Geom.Rectangle.Contains;        
    
    const circle = new Phaser.Geom.Circle(400, 300, 220);
    const balls  = this.add.group(null, { 
        key: &#39;balls&#39;, 
        frame: [0, 1, 5], 
        repeat: 5, 
        setScale: { x: 3, y: 3 },
        hitArea: hitArea,
        hitAreaCallback: hitAreaCallback,
    });

  

    this.downButton = this.add.image(80, 530, &#39;up-bubble&#39;).setInteractive();
    this.upButton = this.add.image(230, 530, &#39;down-bubble&#39;).setInteractive();
    this.input.on(&#39;gameobjectup&#39;, function (pointer, gameobject) {

        if (gameobject === this.downButton &amp;&amp; this.spinSpeed &gt; 0)
        {
            this.spinSpeed -= 0.1;
        }
        else if (gameobject === this.upButton &amp;&amp; this.spinSpeed &lt; 9.9)
        {
            this.spinSpeed += 0.1;
        }

    });       
 


    Phaser.Actions.PlaceOnCircle( balls.getChildren(), circle);

    return balls;
}
 generateDance() {
        this.spinSpeed = 0.003;  
        return this.tweens.addCounter({
            from: 220,
            to: 160,
            duration: 9000,
            delay: 2000,
            ease: &#39;Sine.easeInOut&#39;,
            repeat: -1,
            yoyo: true
        });
    }
update() {

    this.playerEyes.update();
    Phaser.Actions.RotateAroundDistance( this.balls.getChildren(), { x: 400, y: 300 }, this.spinSpeed, this.dance.getValue());
  

}

I took the code from the Phaser 3 example
this is https://phaser.io/examples/v3/view/tweens/tween-time-scale

答案1

得分: 0

以下是代码的翻译部分:

现在两个按钮都正常工作

generateDance() {

    this.downButton = this.add.image(230, 530, 'up-bubble').setInteractive();
    this.upButton = this.add.image(80, 530, 'down-bubble').setInteractive();

    this.spinSpeed = 0.003;

    this.downButton.on('pointerdown', (event) => {

        if (this.spinSpeed < 1) { this.spinSpeed += 0.002; }
    });

    this.upButton.on('pointerdown', (event) => {

        if (this.spinSpeed > 0) { this.spinSpeed -= 0.001; }
    });

    return this.tweens.addCounter({
        from: 220,
        to: 160,
        duration: 9000,
        delay: 2000,
        ease: 'Sine.easeInOut',
        repeat: -1,
        yoyo: true
    });
}

希望这对您有所帮助。如果您有其他翻译需求,请随时告诉我。

英文:

now both buttons function properly

 generateDance() {

    this.downButton = this.add.image(230, 530, &#39;up-bubble&#39;).setInteractive();
    this.upButton = this.add.image( 80, 530, &#39;down-bubble&#39;).setInteractive();

    this.spinSpeed = 0.003;

    this.downButton.on (&#39;pointerdown&#39;, (event) =&gt; {

        if (this.spinSpeed &lt; 1) { this.spinSpeed += 0.002; }
    });

    this.upButton.on(&#39;pointerdown&#39;, (event) =&gt; {

        if (this.spinSpeed &gt; 0 ) { this.spinSpeed -= 0.001; }
    }); 
    
    return this.tweens.addCounter({
        from: 220,
        to: 160,
        duration: 9000,
        delay: 2000,
        ease: &#39;Sine.easeInOut&#39;,
        repeat: -1,
        yoyo: true
    });
}

huangapple
  • 本文由 发表于 2020年1月6日 15:54:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定