Commit cc8ebb4c authored by wildfirecode13's avatar wildfirecode13

1

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