Commit cc8ebb4c authored by wildfirecode13's avatar wildfirecode13

1

parent c7e435f0
import { Sprite, TextureCache } from 'spark-wrapper-fyge';
export default class Body extends Sprite {
constructor(props) {
super();
if(typeof(props.display)==='string')
this.texture = TextureCache[props.display || 'box'];
else
this.addChild(props.display);
this.bodyData = props.bodyData;
const img = new Sprite();
img.texture = TextureCache[props.display];
img.x = props.offset[0];
img.y = props.offset[1];
this.addChild(img);
this.restitution = props.restitution;
this.velocityX = this.initialVelocityX = props.initialVelocityX || 0;
......
......@@ -19,7 +19,7 @@ class GameStage extends WidgetBase {
this.onEnterFrame,
this
);
this.addbodys();
const groundData = { x: 0, y: this.stage.viewRect.height / 2, width: 100, height: 10 };
const ground = new Ground({ data: groundData, display: createGroundDisplay(groundData) })
this.addGround(ground);
......@@ -31,6 +31,8 @@ class GameStage extends WidgetBase {
const groundData2 = { x: 200, y: this.stage.viewRect.height / 3 * 2 * 1.2, width: 200, height: 10 };
const ground2 = new Ground({ data: groundData2, display: createGroundDisplay(groundData2) })
this.addGround(ground2);
this.addbodys();
}
grounds;
......@@ -49,10 +51,12 @@ class GameStage extends WidgetBase {
addbodys() {
const ball = new Body({
display: 'box',
offset:[-23,0],
initialVelocityY: 0,
initialVelocityX: 2,
accelerationY: 2,
restitution: 0
initialVelocityX: 1,
accelerationY: 1,
restitution: 0,
bodyData:{width:34,height:91}
});
this.addChild(ball);
this.bodys.push(ball);
......@@ -66,14 +70,14 @@ class GameStage extends WidgetBase {
}
checkCollideGround(body) {
const y0 = body.y + body.height;
const y0 = body.y + body.bodyData.height;
//默认从上往下走,碰到第一个就停止
for (let i = 0; i < this.grounds.length; i++) {
const ground = this.grounds[i];
const dis0 = body.width / 2 + ground.data.width / 2;
const dis1 = Math.abs(body.x + body.width / 2 - ground.data.x - ground.data.width / 2);
const dis0 = body.bodyData.width / 2 + ground.data.width / 2;
const dis1 = Math.abs(body.x + body.bodyData.width / 2 - ground.data.x - ground.data.width / 2);
if (y0 > ground.y && dis1 < dis0) {
body.y = ground.y - body.height;
body.y = ground.y - body.bodyData.height;
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