Commit 4ade1232 authored by wildfirecode13's avatar wildfirecode13

1

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