Object create() 方法

了解 JavaScript Object 原型上 create() 方法的相關資訊。 在 ES5 中引入。 用於創建一個新的對象,並指定其原型。 使用方法: const newObject = Object.create(prototype) 範例: const animal = {} const dog = Object.create(animal) 新創建的對象將繼承所有原型對象的屬性。 您可以指定第二個參數以添加原型缺失的新屬性給對象: const newObject = Object.create(prototype, newProperties) 其中 newProperties 是一個對象,其中定義了每個屬性。 範例: const animal = {} const dog = Object.create(animal, { breed: { value: 'Siberian Husky' } }); console.log(dog.breed) //'Siberian Husky' 我並沒有只寫 breed: 'Siberian Husky',而是必須傳遞一個屬性描述對象,定義在本頁開頭。 Object.create() 經常與 Object.assign() 結合使用: const dog = Object.assign(Object.create(animal), { bark() { console.log('bark') } })

Phaser: GameObjects

本文是 Phaser 系列文章的一部分。點擊這裡查看系列文章的第一篇。 在 create 函式內,我們可以將 GameObjects 加到遊戲中。 例如我們可以畫出形狀,像是一個圓形: function create() { const circle = this.add.circle(100, 100, 90, 0xffffff) } 這會在座標 (100, 100) 上加入一個直徑為 90 的白色圓形。這些數字都是以像素為單位。 circle 變數包含了新增的圓形的參考。 this 在函式的上下文中是指場景物件。 另一個例子是 this.add.text(),這會在遊戲中加入文字: const text = this.add.text(130, 100, 'test') 你可以通過傳遞一組選項來自訂文字的外觀: const text = this.add.text(50, 100, 'Test', { font: '20px Arial', fill: '#FFFFFF' }) 任何 GameObject 都有一組屬性。例如,我們可以使用 text.x 和 text.y 來訪問 2D 空間中的 x 和 y 軸位置。