บทนำ (Overview)
คราวที่แล้วเราสร้างดาวออกมาเพื่อให้พระเอกของเราจะได้เก็บ (How to create the game with Phaser framework (Part9)) แต่ดูแล้วพระเอกของเรา ไม่สนใจดาวซะอย่างนั้น เดินผ่านไปเฉยเลยเราจะมาทำให้เวลาพระเอกของเราเดินชนดาวแล้ว แล้วดาวหายไป
ขั้นตอน (Steps)
- จากคราวที่แล้ว เราพบว่าพระเอกของเราดันเดินผ่านดาวได้
- เปิดไฟล์ “part8.html”
- ใช้ “Physics” สำหรับการตรวจสอบการซ้อนทับกัน (overlap) ใน “function update()”
//player คือตัวแปรที่บรรจุพระเอกของเรา //stars คือตัวแปรของ Group ที่บรรจุดาวหลาย ๆ ดวงของเรา //collectStar คือชื่อ function ที่จะเอาไว้ใช้เขียนคำสั่งเมื่อวัตถุทั้ง 2 overlap กัน //null ไม่ต้อง Set ค่าสำหรับ parameter นี้ //this หมาย this.game game.physics.arcade.overlap(player, stars, collectStar, null, this);
- ลองเปิดใน Web Browser ปรากฏว่าดำปี๋
- เพราะว่าโปรมแกรมไม่พบ “function collectStar()” ดังนั้นเราจะมาสร้าง function ดังกล่าวกัน
//อ้างอิงจากข้อ (3) function collectStar (player1, star1) { // ลบดาวออกจากหน้าจอ star1.kill(); }
- สรุป “code” ทั้งหมดดังนี้
<script type="text/javascript"> var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update }); function preload() { game.load.image('sky', 'assets/sky.png'); game.load.image('ground', 'assets/platform.png'); game.load.image('star', 'assets/star.png'); game.load.spritesheet('dude', 'assets/dude.png', 32, 48); } function create() { game.add.sprite(0, 0, 'sky'); platforms = game.add.group(); platforms.enableBody = true; var ground = platforms.create(0, game.world.height - 64, 'ground'); ground.scale.setTo(2, 2); var ledge1 = platforms.create(400, 400, 'ground'); var ledge2 = platforms.create(350, 100, 'ground'); var ledge3 = platforms.create(-150, 250, 'ground'); player = game.add.sprite(32, game.world.height - 150, 'dude'); game.physics.startSystem(Phaser.Physics.ARCADE); game.physics.arcade.enable(player); player.body.bounce.y = 0.2; player.body.gravity.y = 300; player.body.collideWorldBounds = true; player.animations.add('left', [0, 1, 2, 3], 10, true); player.animations.add('right', [5, 6, 7, 8], 10, true); ground.body.immovable = true; ledge1.body.immovable = true; ledge2.body.immovable = true; ledge3.body.immovable = true; stars = game.add.group(); stars.enableBody = true; for (var i = 0; i < 10; i++) { var y = 40 + (Math.random() * 300); var x = 40 + (Math.random() * 700); var star = stars.create(x + i, y, 'star'); star.body.gravity.y = 300; star.body.bounce.y = 0.2 + (Math.random() * 0.7); } cursors = game.input.keyboard.createCursorKeys(); } function update() { game.physics.arcade.collide(player, platforms); game.physics.arcade.collide(stars, platforms); game.physics.arcade.overlap(player, stars, collectStar, null, this); player.body.velocity.x = 0; if (cursors.left.isDown) { player.body.velocity.x = -150; player.animations.play('left'); } else if (cursors.right.isDown) { player.body.velocity.x = 150; player.animations.play('right'); } else { player.animations.stop(); player.frame = 4; } if (cursors.up.isDown && player.body.touching.down) { player.body.velocity.y = -350; } } function collectStar (player1, star1) { // Removes the star from the screen star1.kill(); } </script>
บทความที่เกี่ยวข้อง
- How to create the game with Phaser framework (Part1) – Start Game World
- How to create the game with Phaser framework (Part2) – Function preload()
- How to create the game with Phaser framework (Part3) – Display Images
- How to create the game with Phaser framework (Part4) – Function create()
- How to create the game with Phaser framework (Part5) – Physics
- How to create the game with Phaser framework (Part6) – Collide
- How to create the game with Phaser framework (Part7) – Control
- How to create the game with Phaser framework (Part8) – Animation
- How to create the game with Phaser framework (Part9) – Star Shines
- How to create the game with Phaser framework (Part10) – Keep Star
- How to create the game with Phaser framework (Part11) – Count Score