Commit 66f73525 authored by honingwon's avatar honingwon

1

parent fbd059e5
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,25 +10,31 @@ class GameStage extends WidgetBase { ...@@ -11,25 +10,31 @@ class GameStage extends WidgetBase {
this this
); );
this.addbodys(); this.addbodys();
ground = this.stage.viewRect.height; // this.addGround({x:0,y:500, width:300,height:10});
this.addGround({x:0,y:this.stage.viewRect.height, width:300,height:10});
// ground = this.stage.viewRect.height;
}
grounds;
addGround(ground){
this.grounds = this.grounds||[];
this.grounds.push(ground);
} }
addbodys() { addbodys() {
const ball = new Body({ const ball = new Body({
display: 'ball', display: 'ball',
initialVelocityY: 2, initialVelocityY: 0,
accelerationY: 2, accelerationY: 2,
restitution:0.85 restitution:0
}); });
const block = new Body({ const block = new Body({
display: 'box', display: 'box',
initialVelocityY: 2, initialVelocityY: 0,
accelerationY: 2, accelerationY: 0,
restitution:0.4 restitution:0
}); });
block.x = 300; // block.x = 300;
this.addChild(block); this.addChild(block);
this.bodys.push(block); this.bodys.push(block);
...@@ -46,12 +51,18 @@ class GameStage extends WidgetBase { ...@@ -46,12 +51,18 @@ class GameStage extends WidgetBase {
checkCollideGround(body) { checkCollideGround(body) {
const y0 = body.y + body.height; const y0 = body.y + body.height;
if (y0 > ground) { //默认从上往下走,碰到第一个就停止
body.y = ground - body.height; for (let i = 0; i < this.grounds.length; i++) {
const ground = this.grounds[i];
if (y0 > ground.y) {
body.y = ground.y - body.height;
body.velocityY = -body.velocityY * body.restitution; body.velocityY = -body.velocityY * body.restitution;
break;
} }
} }
}
} }
/** /**
......
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