Commit d7b71a7e authored by 王勇霞's avatar 王勇霞

跳一跳

parent 347546a9
...@@ -34,8 +34,6 @@ export class GameScene extends Scene { ...@@ -34,8 +34,6 @@ export class GameScene extends Scene {
xiangpop: FYGE.Sprite; xiangpop: FYGE.Sprite;
monster: FYGE.Sprite; monster: FYGE.Sprite;
jumpLock = false;
isonce = false; isonce = false;
wordList = this.getWordList(); // 待随机出现的列表 wordList = this.getWordList(); // 待随机出现的列表
...@@ -140,7 +138,7 @@ export class GameScene extends Scene { ...@@ -140,7 +138,7 @@ export class GameScene extends Scene {
this.addRole(); this.addRole();
this.addEventListener(FYGE.Event.ENTER_FRAME, this.onFarm, this); this.addEventListener(FYGE.Event.ENTER_FRAME, this.onFarm, this);
this.composites.add(this.world, [this._role.phyBody]); // this.composites.add(this.world, [this._role.phyBody]);
} }
_role; //人物 _role; //人物
bgCon; //背景移动 bgCon; //背景移动
...@@ -148,12 +146,12 @@ export class GameScene extends Scene { ...@@ -148,12 +146,12 @@ export class GameScene extends Scene {
addRole() { addRole() {
let offset = (1624 - this.stage.viewRect.height) / 2; let offset = (1624 - this.stage.viewRect.height) / 2;
let role = this._role = new Role({ let role = this._role = new Role({
fx: 339,
fy: 980 - offset,
stageY: this.stage.viewRect.y, stageY: this.stage.viewRect.y,
}); });
role.fx = 339;
role.fy = 980 - offset;
this.bgCon.addChild(role); this.bgCon.addChild(role);
this.composites.add(this.world, [role.phyBody]); this.composites.add(this.world, [this._role.phyBody]);
} }
engine; // matter引擎初始化 engine; // matter引擎初始化
composites; composites;
...@@ -179,7 +177,8 @@ export class GameScene extends Scene { ...@@ -179,7 +177,8 @@ export class GameScene extends Scene {
this.composites = Composite; this.composites = Composite;
Matter.Events.on(this.engine, "collisionStart", this.onCollisionStart.bind(this)); Matter.Events.on(this.engine, "collisionStart", this.onCollisionStart.bind(this));
Matter.Events.on(this.engine, "tick", this.onCollisionTick.bind(this)); // Matter.Events.on(this.engine, "tick", this.onCollisionTick.bind(this));
// Matter.Events.on(this.engine, "beforeUpdate", this.onBeforeUpdate.bind(this));
// 创建一个渲染器 // 创建一个渲染器
const render = Render.create({ const render = Render.create({
// element: document.body, // element: document.body,
...@@ -212,8 +211,15 @@ export class GameScene extends Scene { ...@@ -212,8 +211,15 @@ export class GameScene extends Scene {
}); });
Render.run(render); Render.run(render);
} }
onBeforeUpdate(e) {
if (this.engine.timing.timestamp - this.lastTime >= 1000) {
this._role.jump()
this.lastTime = this.engine.timing.timestamp;
}
}
onCollisionTick(e) { onCollisionTick(e) {
} }
lastTime = 0
onCollisionStart(e) { onCollisionStart(e) {
let pairs = e.pairs; let pairs = e.pairs;
// //
...@@ -222,14 +228,11 @@ export class GameScene extends Scene { ...@@ -222,14 +228,11 @@ export class GameScene extends Scene {
// ip 与 云朵 碰撞 // ip 与 云朵 碰撞
console.log(p.bodyA.objType, p.bodyB.objType) console.log(p.bodyA.objType, p.bodyB.objType)
if ((p.bodyA.objType == "cloud" && p.bodyB == this._role.phyBody) || (p.bodyB.objType == "cloud" && p.bodyA == this._role.phyBody)) { if ((p.bodyA.objType == "cloud" && p.bodyB == this._role.phyBody) || (p.bodyB.objType == "cloud" && p.bodyA == this._role.phyBody)) {
// this._role.jump() if (this.engine.timing.timestamp - this.lastTime >= 400) {
if (!this.jumpLock) {
this._role.jump() this._role.jump()
this.jumpLock = true; this.lastTime = this.engine.timing.timestamp;
setTimeout(() => {
this.jumpLock = false
}, 200)
} }
} }
}) })
// pairs.map((p) => { // pairs.map((p) => {
...@@ -324,6 +327,9 @@ export class GameScene extends Scene { ...@@ -324,6 +327,9 @@ export class GameScene extends Scene {
} }
timer = null; timer = null;
onFarm() { onFarm() {
let roleY = this._role.y + this.bgCon.y;
this.moveMap(600 - roleY)
// let roleY = this._role.y + this.bgCon.y; // let roleY = this._role.y + this.bgCon.y;
// if (roleY < 600) { // if (roleY < 600) {
...@@ -363,7 +369,7 @@ export class GameScene extends Scene { ...@@ -363,7 +369,7 @@ export class GameScene extends Scene {
} }
moveMap(y) { moveMap(y) {
// this.bgCon.y = this.bgCon.y + y; this.bgCon.y = this.bgCon.y + y;
} }
removeWorld() { removeWorld() {
......
...@@ -23,7 +23,7 @@ export default class MapItem extends FYGE.Container { ...@@ -23,7 +23,7 @@ export default class MapItem extends FYGE.Container {
// 至于为什么比对的位置, FYGE的适配缩放, 以及Main的剪裁 // 至于为什么比对的位置, FYGE的适配缩放, 以及Main的剪裁
this.phyBody = Matter.Bodies.rectangle(this.fx*scale+itemImg.width*scale/2, this.fy*scale+itemImg.height*scale/2-data.stageY*scale,itemImg.width*scale, 10, { this.phyBody = Matter.Bodies.rectangle(this.fx*scale+itemImg.width*scale/2, this.fy*scale+itemImg.height*scale/2-data.stageY*scale,itemImg.width*scale, 10, {
isStatic: true, isStatic: true,
friction: 0, friction: 1,
isSensor: true, // 传感器,可以检测到碰撞,但是不参与碰撞 isSensor: true, // 传感器,可以检测到碰撞,但是不参与碰撞
render: { render: {
fillStyle: "blue" // 填充的颜色 fillStyle: "blue" // 填充的颜色
......
...@@ -14,8 +14,6 @@ export default class ObstacleProp extends FYGE.Container { ...@@ -14,8 +14,6 @@ export default class ObstacleProp extends FYGE.Container {
this.phyBody = Matter.Bodies.rectangle(this.x, this.y, itemImg.width , itemImg.height , { this.phyBody = Matter.Bodies.rectangle(this.x, this.y, itemImg.width , itemImg.height , {
isStatic: true, isStatic: true,
friction: 1,
isSensor: true, // 传感器,可以检测到碰撞,但是不参与碰撞 isSensor: true, // 传感器,可以检测到碰撞,但是不参与碰撞
}); });
......
...@@ -11,26 +11,26 @@ export default class Role extends FYGE.Container { ...@@ -11,26 +11,26 @@ export default class Role extends FYGE.Container {
jumpSvga; jumpSvga;
downSvga; downSvga;
lightSvga; lightSvga;
fx; // fx;
fy; // fy;
bili; bili;
staY; staY;
constructor(data?) { constructor(data?) {
super(); super();
this.fx = data.fx; // this.fx = data.fx;
this.fy = data.fy; // this.fy = data.fy;
this.staY = data.stageY; this.staY = data.stageY;
this.position.set(this.fx, this.fy); this.position.set(this.x, this.y);
let itemImg = this.itemImg = UI.Sp(this, "controlpeo.png", 0, 0); let itemImg = this.itemImg = UI.Sp(this, "controlpeo.png", 0, 0);
let scale = this.bili = document.body.clientWidth / 750; let scale = this.bili = document.body.clientWidth / 750;
// 至于为什么比对的位置, FYGE的适配缩放, 以及Main的剪裁 // 至于为什么比对的位置, FYGE的适配缩放, 以及Main的剪裁
this.phyBody = Matter.Bodies.rectangle(this.fx * scale + itemImg.width * scale / 2, this.fy * scale + itemImg.height * scale / 2 - data.stageY * scale, itemImg.width * scale, itemImg.height * scale, { this.phyBody = Matter.Bodies.rectangle(this.x * scale + itemImg.width * scale / 2, this.y * scale + itemImg.height * scale / 2 - data.stageY * scale, itemImg.width * scale, itemImg.height * scale, {
isStatic: false, isStatic: false,
isSensor: false, // 传感器,可以检测到碰撞,但是不参与碰撞 // isSensor: false, // 传感器,可以检测到碰撞,但是不参与碰撞
restitution: 0.8, // restitution: 0.1,
friction: 0.01, // friction: 0.01,
inverseMass: 0.2, // inverseMass: 0.2,
// restitution: 0.8, // 定义刚体恢复(弹性)的数字 // restitution: 0.8, // 定义刚体恢复(弹性)的数字
// // // density: 0.05,// 定义刚体密度的数字,即单位面积的质量 // // // density: 0.05,// 定义刚体密度的数字,即单位面积的质量
// // friction: 0.02, // // friction: 0.02,
...@@ -43,6 +43,7 @@ export default class Role extends FYGE.Container { ...@@ -43,6 +43,7 @@ export default class Role extends FYGE.Container {
}); });
// 绘制ip // 绘制ip
this.getSvga() this.getSvga()
this.setPhyPos();
this.addEventListener( this.addEventListener(
FYGE.Event.ADDED_TO_STAGE, FYGE.Event.ADDED_TO_STAGE,
() => { () => {
...@@ -96,8 +97,26 @@ export default class Role extends FYGE.Container { ...@@ -96,8 +97,26 @@ export default class Role extends FYGE.Container {
/** 根据物理刚体,更新当前的坐标。 */ /** 根据物理刚体,更新当前的坐标。 */
private onFarm() { private onFarm() {
this.x = (this.phyBody.position.x - this.itemImg.width * this.bili / 2) / this.bili; let Tx = (this.phyBody.position.x - this.itemImg.width * this.bili / 2) / this.bili;
this.y = (this.phyBody.position.y + (this.staY * this.bili) - this.itemImg.height * this.bili / 2) / this.bili; let Ty = (this.phyBody.position.y + (this.staY * this.bili) - this.itemImg.height * this.bili / 2) / this.bili;
this.x = Tx;
this.y = Ty;
if (Tx > 750) {
this.fx = 0;
this.fy = Ty;
} else if (Tx < 0) {
this.fx = 750;
this.fy = Ty
} else {
this.fx = Tx;
this.fy = Ty
}
}
// this.x = Tx
// }
// this.x =
// this.y =
// if (this.beforeY) { // if (this.beforeY) {
// if (this.beforeY > this.phyBody.position.y) { // if (this.beforeY > this.phyBody.position.y) {
...@@ -128,42 +147,30 @@ export default class Role extends FYGE.Container { ...@@ -128,42 +147,30 @@ export default class Role extends FYGE.Container {
// this.beforeY = this.phyBody.position.y; // this.beforeY = this.phyBody.position.y;
}
jump() { jump() {
setTimeout(() => { setTimeout(() => {
Matter.Body.applyForce(this.phyBody, this.phyBody.position, { Matter.Body.setVelocity(this.phyBody, { x: 0, y: -8 }); // 立即设置刚体的线速度。位置、角度、力等不变
x: 0, // Matter.Body.applyForce(this.phyBody, this.phyBody.position, {
y: -0.1 // x: 0,
}); // y: -10
// });
}) })
} }
// onMouseDown(e) { set fx(value: number) {
// let offsetX = e.stageX - this.x; this.position.x = value;
// let offsetY = e.stageY - this.y; this.setPhyPos();
// this.stage.addEventListener( }
// FYGE.MouseEvent.MOUSE_MOVE,
// function (event) { set fy(value: number) {
// let resx = event.stageX - offsetX; this.position.y = value;
// let resy = event.stageY - offsetY; this.setPhyPos();
// if (resx < 50 || resx > 700) { return } }
// this.x = resx;
// // this.y = resy; setPhyPos() {
// Matter.Body.setPosition(this.phyBody, { Matter.Body.setPosition(this.phyBody, {
// x: resx, x: this.x * this.bili + this.itemImg.width * this.bili / 2,
// y: this.y, y: this.y * this.bili + this.itemImg.height * this.bili / 2 - this.staY * this.bili,
// }); });
// }, }
// this
// );
// this.stage.addEventListener(
// FYGE.MouseEvent.MOUSE_UP,
// function (event) {
// this.stage.removeAllEventListener()
// },
// this
// );
// }
} }
\ No newline at end of file
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