【问题标题】:speed and slow the ball using the button使用按钮加速和减速球
【发布时间】:2020-04-22 02:38:11
【问题描述】:

你能告诉我如何在点击时使用按钮加快和减慢球的速度吗? 我使用了代码,但它还没有工作,

    this.upButton = this.add.sprite(230, 530, 'down-bubble').setInteractive({ cursor: 'pointer' });

    this.downButton = this.add.sprite(80, 530, 'up-bubble').setInteractive({ cursor: 'pointer' });

    this.input.on('gameobjectup', function (pointer, gameobject) {

        if (gameobject === this.downButton &&  this.spinSpeed.timeScale > 100)
        {
            this.spinSpeed.timeScale -= 10.1;
        }
        else if (gameobject === this.upButton &&  this.spinSpeed.timeScale < 19.9)
        {
            this.spinSpeed.timeScale += 10.1;
        }

    });



generateDance() {

    this.spinSpeed = 0.003;

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

【问题讨论】:

  • 你能添加你用来创建球的代码吗?以及调用您的generateDance() 方法的代码?
  • 你好,先生,这是调用球的代码

标签: javascript typescript phaser-framework


【解决方案1】:

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: 'balls', 
        frame: [0, 1, 5], 
        repeat: 5, 
        setScale: { x: 3, y: 3 },
        hitArea: hitArea,
        hitAreaCallback: hitAreaCallback,
    });

    // console.log(balls.getChildren().length);


    this.input.on('gameobjectover', function (pointer, gameObject) {
        document.body.style.cursor = 'pointer';
    });

    this.input.on('gameobjectout', function (pointer, gameObject) {
        document.body.style.cursor = 'default';
    });

    this.upButton = this.add.sprite(230, 530, 'down-bubble').setInteractive({ cursor: 'pointer' });

    this.downButton = this.add.sprite(80, 530, 'up-bubble').setInteractive({ cursor: 'pointer' });

    this.input.on('gameobjectup', function (pointer, gameobject) {

        if (gameobject === this.downButton &&  this.spinSpeed.timeScale > 100)
        {
            this.spinSpeed.timeScale -= 10.1;
        }
        else if (gameobject === this.upButton &&  this.spinSpeed.timeScale < 19.9)
        {
            this.spinSpeed.timeScale += 10.1;
        }

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

    return balls;
}

generateDance() {

    this.spinSpeed = 0.003;
    // https://phaser.discourse.group/t/how-to-create-key-combo-with-custom-buttons/2622
    return this.tweens.addCounter({
        from: 220,
        to: 160,
        duration: 9000,
        delay: 2000,
        ease: 'Sine.easeInOut',
        repeat: -1,
        yoyo: true
    });
}

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

}

【讨论】:

    【解决方案2】:
    preload(): void {
    
        this.load.image('sky', './src/games/gewgly/assets/images/sky.png');
    
        this.load.image('up-bubble', './src/games/gewgly/assets/images/up-bubble.png');
        this.load.image('down-bubble', './src/games/gewgly/assets/images/down-bubble.png');
    
        this.load.spritesheet('balls', './src/games/gewgly/assets/images/balls.png', { frameWidth: 17, frameHeight: 17 });
    
    }
    
    
    create(): void {
    
        this.add.image(400, 300, 'sky');
        this.balls = this.generateBalls();    
        this.dance = this.generateDance();
        // this.buttonsspeed();
    
        this.playerEyes = new Eye(this.generateEyeArg(200,135,3.0));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-24
      • 1970-01-01
      • 1970-01-01
      • 2013-02-28
      • 1970-01-01
      • 2013-10-18
      • 2011-12-30
      相关资源
      最近更新 更多