Commit 4ade1232 authored by wildfirecode13's avatar wildfirecode13

1

parent 454cb6a3
...@@ -5,6 +5,7 @@ export default class Body extends Sprite { ...@@ -5,6 +5,7 @@ export default class Body extends Sprite {
this.texture = TextureCache[props.display || 'box']; this.texture = TextureCache[props.display || 'box'];
this.restitution = props.restitution || 1
this.velocityX = this.initialVelocityX = props.initialVelocityX || 0; this.velocityX = this.initialVelocityX = props.initialVelocityX || 0;
this.velocityY = this.initialVelocityY = props.initialVelocityY || 0; this.velocityY = this.initialVelocityY = props.initialVelocityY || 0;
......
import { WidgetBase, Event } from 'spark-wrapper-fyge'; import { WidgetBase, Event } from 'spark-wrapper-fyge';
import metaConfig from '../meta.json'; import metaConfig from '../meta.json';
import Body from './Body'; import Body from './Body';
let ground;
class GameStage extends WidgetBase { class GameStage extends WidgetBase {
onLaunched() { onLaunched() {
this.bodys = []; this.bodys = [];
...@@ -11,65 +11,46 @@ class GameStage extends WidgetBase { ...@@ -11,65 +11,46 @@ class GameStage extends WidgetBase {
this this
); );
this.addbodys(); this.addbodys();
ground = this.stage.viewRect.height;
} }
addbodys() { addbodys() {
const enemy = new Body({ const ball = new Body({
display: 'enemy', display: 'ball',
initialVelocityY: 1 initialVelocityY: 2,
accelerationY: 2,
restitution:0.85
}); });
const bullet = new Body({ const block = new Body({
display: 'bullet', display: 'box',
initialVelocityY: -4 initialVelocityY: 2,
accelerationY: 2,
restitution:0.4
}); });
enemy.x = 150; block.x = 300;
enemy.y = 0;
bullet.x = 170;
bullet.y = 600;
this.addChild(enemy);
this.bodys.push(enemy);
this.addChild(bullet); this.addChild(block);
this.bodys.push(bullet); this.bodys.push(block);
this.addChild(ball);
this.bodys.push(ball);
} }
onEnterFrame() { onEnterFrame() {
this.bodys.forEach(i => i.onEnterFrame()); this.bodys.forEach(i => {
i.onEnterFrame();
const isCollide = this.checkCollide(this.bodys[0], this.bodys[1]) this.checkCollideGround(i);
if (isCollide) });
this.stopGame();
}
checkCollide(A, B) {
const pointA = [A.x + A.width / 2, A.y + A.height / 2];
const pointB = [B.x + B.width / 2, B.y + B.height / 2];
const x0 = Math.abs(pointA[0] - pointB[0]);
const y0 = Math.abs(pointA[1] - pointB[1]);
const x1 = A.width/2 + B.width/2;
const y1 = A.height/2 + B.height/2;
return x0 < x1 && y0 < y1;
}
stopGame() {
this.stage.removeEventListener(
Event.ENTER_FRAME,
this.onEnterFrame,
this
);
} }
/** checkCollideGround(body) {
* 销毁回调 const y0 = body.y + body.height;
*/ if (y0 > ground) {
onDestroy() { body.y = ground - body.height;
} body.velocityY = -body.velocityY * body.restitution;
start() {
} }
stop() {
} }
} }
......
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