Commit ffdce49d authored by 邱旭's avatar 邱旭

08.FlppyBird-模拟重力

parent b30ed884
......@@ -52,24 +52,19 @@
this.gravity = gravity; // 保存gravity
// 放到合适的位置
const { width, height } = this.getSize();
this.setPosition(
(winSize.height - height) / 2 - 100,
(winSize.width - width) / 2,
);
const { width, height } = this.size;
this.top = (winSize.height - height) / 2 - 100;
this.left = (winSize.width - width) / 2;
}
update() {
super.update();
let top = this.getPosition().top
// v = v0 + a * t²
this.speed += this.gravity; // 速度 = 速度 + 加速度 * 时间²
top += this.speed; // 更新位置
this.top += this.speed; // 更新位置
this.setPosition(top);
}
}
......@@ -88,21 +83,20 @@
this.bg2 = bg2;
this.speed = speed;
bg1.setPosition(winSize.height - bg1.getSize().height); // 放在底部
bg2.setPosition(winSize.height - bg2.getSize().height); // 放在底部
bg1.top = winSize.height - bg1.size.height; // 放在底部
bg2.top = winSize.height - bg2.size.height; // 放在底部
}
update() {
super.update();
// 获取一些参数
let { top: bg1Top, left: bg1Left } = this.bg1.getPosition();
const bg1Width = this.bg1.getSize().width;
const bg2Top = this.bg2.getPosition().top;
let bg1Left = this.bg1.left;
const bg1Width = this.bg1.size.width;
// 计算位置
bg1Left -= this.speed; // 计算位置
this.bg1.setPosition(bg1Top, bg1Left); // 设置bg1的位置
this.bg2.setPosition(bg2Top, bg1Left + this.bg1.getSize().width); // bg2跟在bg1后面
this.bg1.left = bg1Left; // 设置bg1的位置
this.bg2.left = bg1Left + this.bg1.size.width; // bg2跟在bg1后面
// 如果超出屏幕则交换bg1和bg2,为了做到循环滚动
if (bg1Left <= -bg1Width) {
......@@ -129,7 +123,7 @@
const landMgr = new ScrollMgr("land", land1, land2, 5);
// 将背景放在地面的上面,因为默认top是0,子节点在内部定位在底部,所以只需要把背景定位在负的land的高度就可以了
bgMgr.setPosition(-land1.getSize().height);
bgMgr.top = -land1.size.height;
</script>
<script>
......
......@@ -26,11 +26,9 @@ class Bird extends GameObject {
this.gravity = gravity; // 保存gravity
// 放到合适的位置
const { width, height } = this.getSize();
this.setPosition(
(winSize.height - height) / 2 - 100,
(winSize.width - width) / 2,
);
const { width, height } = this.size;
this.top = (winSize.height - height) / 2 - 100;
this.left = (winSize.width - width) / 2;
}
/* ... */
......@@ -52,14 +50,11 @@ class Bird extends GameObject {
update() {
super.update();
let top = this.getPosition().top
// v = v0 + a * t²
this.speed += this.gravity; // 速度 = 速度 + 加速度 * 时间²
top += this.speed; // 更新位置
this.setPosition(top);
this.top += this.speed; // 更新位置
}
}
```
......
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