Last active
August 29, 2015 14:19
-
-
Save thaisingle/b6a1acbd1ba9a400b3a0 to your computer and use it in GitHub Desktop.
How to create Flappy Bird (Part 4) – Create pipes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var game = new Phaser.Game(400, 490, Phaser.AUTO, 'flappyDiv'); | |
var mainState = { | |
preload: function() { | |
game.stage.backgroundColor = '#71c5cf'; | |
game.load.image('bird', 'assets/bird.png'); | |
game.load.image('pipe', 'assets/pipe.png'); | |
}, | |
create: function() { | |
this.bird = this.game.add.sprite(100, 245, 'bird'); | |
game.physics.startSystem(Phaser.Physics.ARCADE); | |
game.physics.arcade.enable(this.bird); | |
this.bird.body.gravity.y = 1000; | |
var spaceKey = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); | |
spaceKey.onDown.add(this.jump, this); | |
this.pipes = game.add.group(); //สร้าง gorup | |
this.pipes.enableBody = true; //เพิ่ม physics ให้กับ group | |
//createMultiple(quantity, key, frame, exists) | |
this.pipes.createMultiple(20, 'pipe'); //สร้าง pide 20 อัน | |
//หน่วงเวลาไว้ 1.6 วินาที | |
this.timer = game.time.events.loop(1600, this.addLongPipes, this); | |
}, | |
update: function() { | |
if (this.bird.inWorld == false){ | |
this.restartGame(); | |
} | |
}, | |
jump: function() { | |
this.bird.body.velocity.y = -350; | |
}, | |
restartGame: function() { | |
game.state.start('main'); | |
}, | |
addShotPipe: function(x, y) { | |
var pipe = this.pipes.getFirstDead(); | |
pipe.reset(x, y); | |
pipe.body.velocity.x = -200; | |
//ให้ pipe สามารถตรวจสอบว่าถึงขอบ game world หรือยัง | |
pipe.checkWorldBounds = true; | |
//เมื่อ pipe เลย game world สั่งลบเลย | |
pipe.outOfBoundsKill = true; | |
}, | |
addLongPipes: function() { | |
//สุ่มตำแหน่ง | |
var blank = Math.floor(Math.random() * 5) + 1; | |
//สร้าง pipe 8 อัน | |
for (var i = 0; i < 8; i++){ | |
//ตำแหน่งที่สุ่มและบวกหนึ่งจะไม่แสดง pipe | |
if (i != blank && i != blank + 1){ | |
//แสดง pipe | |
this.addShotPipe(400, (i * 60) + 10); | |
} | |
} | |
}, | |
}; | |
game.state.add('main', mainState); | |
game.state.start('main'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment