Sprite

#HelloGameDev #HelloErmine #HelloWorld2021

Sprite

เป็นการสร้าง object ที่ภาพมี animation โดยเราจะใช้คำสั่ง this.add.sprite(x,y,'key') แต่เมื่อเราต้องการให้ภาพ animation นั้นมีระบบฟิสิกส์ ให้เราใช้ this.physics.add.sprite(x,y,'key') ลงไปใน Create function

ตัวอย่าง

preload(){
     this.load.spritesheet('bird','src/image/bird.png',
        { frameWidth: 410 , frameHeight: 310}); 
}
create(){
     bird = this.add.sprite(425, 700, 'bird').setScale(0.5);
}

คำสั่ง .setScale(ขนาด); คือการกำหนดขนาดของ object โดยเลขที่ใส่จะเป็นตัวคูณว่า object จะใหญ่ขนาดไหน

ผลลัพธ์ที่ได้

ตัวอย่างของการสร้าง Sprite เท่านั้น ยังไม่ใช่การทำ Animation

การหา frameWidth เเละ frameHeight

  • frameWidth

    • ความยาวรูป/จำนวนเฟรม

  • frameHeight

    • ความสูงของรูป

ในตัวอย่างมี 8 เฟรม ความยาวรูปเท่ากับ 3280 คำนวณได้ว่า 3280 / 8 = 410 (frameWidth)

Last updated

Was this helpful?