Phaser3游戲三角學(xué)應(yīng)用--一只跟隨屏幕點(diǎn)擊位置游動(dòng)的魚

fish
fish

資源圖:

fish-136x80.png
undersea-bg.png

代碼

var config = {
    type: Phaser.AUTO,
    parent: 'iFiero', // game id; html中為 <div id="iFiero"></div>
    width: 500,
    height: 380,
    scene: {
        preload: preload,
        create: create
    }
};

var game = new Phaser.Game(config);

// 初始化代碼
function init() {

}

function preload() {
    this.load.image('bg', 'assets/undersea-bg.png');
    //this.load.image('arrow', 'assets/sprites/arrow.png');
    this.load.spritesheet('fish', 'assets/fish-136x80.png', {
        frameWidth: 136,
        frameHeight: 80
    });
}


function create() {
    this.add.image(0, 0, 'bg').setOrigin(0).setScale(0.65);

    // this.arrow =  this.add.image(250, 200, 'arrow', Phaser.Math.Between(0, 5));
    this.fish = this.add.image(0, 80, 'fish', 0).setScale(0.7);

    this.input.on('pointerdown', function (pointer) {

        // 三角函數(shù) 得出魚要旋轉(zhuǎn)的角度
        this.fish.rotation = Math.atan2(pointer.y - this.fish.y, pointer.x - this.fish.x);

        // 判斷魚是否需要反轉(zhuǎn):點(diǎn)擊的位置和魚頭相同=>不反轉(zhuǎn)
        if ((pointer.x > this.fish.x)) {
            console.log("點(diǎn)擊的位置和魚頭相同=>不反轉(zhuǎn)");
            this.fish.flipY = false;
        }

        // 判斷魚是否需要反轉(zhuǎn):點(diǎn)擊的位置和魚頭相反=>反轉(zhuǎn)
        if ((pointer.x < this.fish.x)) {
            console.log("點(diǎn)擊的位置和魚頭相反=>反轉(zhuǎn)");
            this.fish.flipY = true;
        }

        // 讓魚移動(dòng)到點(diǎn)擊的位置
        this.tweens.add({
            targets: this.fish,
            x: pointer.x,
            y: pointer.y,
            duration: 3000,
            ease: 'Power2',
        });

    }, this);
}

更多游戲教學(xué):www.iFiero.com -- 為游戲開發(fā)深感自豪

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容