Commit 8c416f7e authored by wildfirecode13's avatar wildfirecode13

1

parent 9abebb1e
import { Sprite, TextureCache } from 'spark-wrapper-fyge'; import { Sprite, TextureCache } from 'spark-wrapper-fyge';
export default class DropItem extends Sprite { export default class Body extends Sprite {
constructor(props) { constructor(props) {
super(); super();
this.texture = TextureCache['box']; this.texture = TextureCache[props.display || 'box'];
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 DropItem from './DropItem'; import Body from './Body';
/**
*
*
*/
class GameStage extends WidgetBase { class GameStage extends WidgetBase {
onLaunched() { onLaunched() {
this.drops = []; this.bodys = [];
this.stage.addEventListener( this.stage.addEventListener(
Event.ENTER_FRAME, Event.ENTER_FRAME,
this.onEnterFrame, this.onEnterFrame,
this this
); );
this.addDrops(); this.addbodys();
} }
addDrops() { addbodys() {
const drop = new DropItem({ const ball1 = new Body({
initialVelocityY: -50, display: 'ball',
initialVelocityX: 10, initialVelocityX: 2,
accelerationY:2 initialVelocityY: 2
});
const ball2 = new Body({
display: 'ball',
initialVelocityX: -2,
initialVelocityY: -2
}); });
drop.y = 1000;
this.addChild(drop); ball1.x = 0;
this.drops.push(drop); ball1.y = 0;
ball2.x = 600;
ball2.y = 600;
this.addChild(ball1);
this.bodys.push(ball1);
this.addChild(ball2);
this.bodys.push(ball2);
} }
onEnterFrame() { onEnterFrame() {
this.drops.forEach(i => i.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 d = A.width;
const r = d / 2;
const pointA = [A.x + r, A.y + r];
const pointB = [B.x + r, B.y + r];
const distanceX = Math.abs(pointA[0] - pointB[0]);
const distanceY = Math.abs(pointA[1] - pointB[1]);
const distanceAB = Math.sqrt(distanceX * distanceX + distanceY * distanceY);
return distanceAB < d;
}
stopGame() {
this.stage.removeEventListener(
Event.ENTER_FRAME,
this.onEnterFrame,
this
);
} }
/** /**
......
...@@ -17,10 +17,16 @@ ...@@ -17,10 +17,16 @@
}, },
{ {
"name": "box", "name": "box",
"url": "//yun.duiba.com.cn/spark/assets/022e25a3984ff122fbf960bd0cb87bff48f8c3bf.png", "url": "//yun.duiba.com.cn/aurora/assets/6fb4612326d4e78b715a83d1114fa2d96fb8adcb.png",
"uuid": "box", "uuid": "box",
"ext": ".png" "ext": ".png"
}, },
{
"name": "ball",
"url": "//yun.duiba.com.cn/aurora/assets/b172bfd984e9acb8d6141ef0a978555dd86056bc.png",
"uuid": "ball",
"ext": ".png"
},
{ {
"name": "bg", "name": "bg",
"url": "//yun.duiba.com.cn/spark/assets/bg.c4359b9ea9e843cd8d929567b2fb52ed5518e105.png", "url": "//yun.duiba.com.cn/spark/assets/bg.c4359b9ea9e843cd8d929567b2fb52ed5518e105.png",
......
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