Commit 4fc3d9b8 authored by Friends233's avatar Friends233

小鸟下坠、跳跃

parent 482fc052
......@@ -2,6 +2,7 @@ import { changeScene } from "../../module/ctrls";
import { Scene } from "../../module/views/Scene";
import { Tools } from "../tools/Tools";
import UI from "../tools/UI";
import { Bird } from "./bird";
import { LoadingScene } from "./LoadingScene";
import Button = FYGE.Button;
import Container = FYGE.Container;
......@@ -20,8 +21,69 @@ export class IndexScene extends Scene {
]
}
/** 玩家(鸟) */
private bird: Bird = null
/** 游戏是否结束 */
private isGameOver: boolean = false
/** 游戏是否开始 */
private isGameStart: boolean = false
async initUi() {
UI.Sp(this, Texture.from('com_bg.jpg'))
// UI.Sp(this, Texture.from('com_bg.jpg'))
this.addChild(new Sprite(Texture.from('com_bg.jpg')))
const bird = this.bird = new Bird()
bird.x = (this.width - bird.width) * 0.5
bird.y = 800
this.addChild(bird)
this._initEvents()
}
_initEvents() {
this.addEventListener(FYGE.Event.ENTER_FRAME, this._enterFrame, this)
this.addEventListener(FYGE.MouseEvent.CLICK, this.birdJump, this)
}
/** 检查小鸟掉落位置 */
_checkRmove() {
if (this.bird.y >= (this.bird.height + this.height)) {
// this.removeChild(this.bird)
this.gameOver()
}
}
/** 帧动画 */
_enterFrame() {
if (this.isGameOver || !this.isGameStart) return
console.log(this.bird.y)
this.bird.fail(1)
this._checkRmove()
}
/** 游戏结束 */
gameOver() {
this.isGameOver = true
this.isGameStart = false
}
/** 小鸟跳跃 */
birdJump() {
if (!this.isGameStart) {
this.startGame()
return
}
this.bird.jump()
}
/** 开始游戏 */
startGame() {
if (this.bird.y > this.height) {
this.bird.y = 0
this.bird.reset()
}
this.isGameStart = true
this.isGameOver = false
}
destroy() {
......
import { changeScene } from "../../module/ctrls";
import { Scene } from "../../module/views/Scene";
import { Tools } from "../tools/Tools";
import UI from "../tools/UI";
import { LoadingScene } from "./LoadingScene";
import Button = FYGE.Button;
import Container = FYGE.Container;
import Shape = FYGE.Shape;
import Sprite = FYGE.Sprite;
import TEXT_ALIGN = FYGE.TEXT_ALIGN;
import TextField = FYGE.TextField;
import VERTICAL_ALIGN = FYGE.VERTICAL_ALIGN;
import Texture = FYGE.Texture;
/** 初始坠落步长 */
const FIAL_STEP = 5
/** 初始跳跃步长 */
const JUMP_STEP = 30
export class Bird extends Container {
/** 坠落步长 */
failStep = FIAL_STEP
/** 跳跃步长 */
jumpStep = JUMP_STEP
constructor() {
super()
this.initUi()
}
async initUi() {
this.addChild(new Sprite(Texture.fromUrl('//yun.duiba.com.cn/aurora/assets/0bcb2c26a85addb1714b4c63f2a873aafe210749.png')))
}
/**
* 小鸟坠落
* @param acceleration 加速比
*/
fail(acceleration = 0) {
this.failStep += acceleration
// console.log(this.step.toFixed(2))
this.y = Math.max(this.y + this.failStep, 0)
if(this.y == 0){
this.reset()
}
}
/** 小鸟跳跃 */
jump() {
if (this.y <= 0) return
this.failStep -= this.jumpStep
}
/** 重置 */
reset(){
this.failStep = FIAL_STEP
this.jumpStep = JUMP_STEP
}
destroy() {
super.destroy();
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment