Browse Source

大白跳一跳问题修复

master
jiannibang 6 years ago
parent
commit
f780e8f66a
  1. 1
      phaser/game-ballmove/constant/index.js
  2. 54
      phaser/game-ballmove/scenes/PlayScene.js

1
phaser/game-ballmove/constant/index.js

@ -6,5 +6,4 @@ export const gameOptions = {
platformDistanceRange: [250, 450],
platformHeightRange: [-50, 50],
platformLengthRange: [40, 120],
localStorageName: "bestballscore3"
};

54
phaser/game-ballmove/scenes/PlayScene.js

@ -64,6 +64,7 @@ class PlayScene extends Phaser.Scene {
platform.setImmovable(true);
platform.displayWidth = blockWidth;
platform.displayHeight = blockHeight;
platform.body.checkCollision.left = true;
}
break;
default:
@ -82,23 +83,22 @@ class PlayScene extends Phaser.Scene {
platform.setImmovable(true);
platform.displayWidth = blockWidth;
platform.displayHeight = blockHeight;
const number = Math.floor(Math.random() * 3 + 1);
platform.name = number;
if (number === 2 || number === 1) platform.body.checkCollision.left = true;
this.attachSidekick(platform, number);
}
create() {
this.physics.world.setFPS(30);
this.platformGroup = this.physics.add.group();
this.bgGroup = this.physics.add.group();
for (let i = 0; i < 2; i++) {
let bg = this.bgGroup.create(
this.game.config.width * (i + 1 / 2),
const bg = this.add.image(
this.game.config.width / 2,
this.game.config.height / 2,
"bg"
);
bg.setImmovable(true);
bg.displayWidth = this.game.config.width;
bg.displayHeight = this.game.config.height;
}
this.ball = this.physics.add.sprite(
this.game.config.width * gameOptions.ballPosition,
(this.game.config.height / 4) * 3 - gameOptions.bounceHeight,
@ -109,16 +109,16 @@ class PlayScene extends Phaser.Scene {
this.ball.body.checkCollision.down = true;
this.ball.body.checkCollision.up = false;
this.ball.body.checkCollision.left = false;
this.ball.body.checkCollision.right = false;
this.ball.body.checkCollision.right = true;
this.ball.displayWidth = 120;
this.ball.displayHeight = 120;
this.ball.setSize(160, 250, true);
// this.ball.setScale(0.5);
let platformX = this.ball.x;
this.platformX = this.ball.x;
for (let i = 0; i < 4; i++) {
this.newPlatForm(platformX);
platformX += Phaser.Math.Between(
this.newPlatForm(this.platformX);
this.platformX += Phaser.Math.Between(
gameOptions.platformDistanceRange[0],
gameOptions.platformDistanceRange[1]
);
@ -137,19 +137,20 @@ class PlayScene extends Phaser.Scene {
}
movePlatforms() {
// this.bgGroup.setVelocityX(-gameOptions.platformSpeed * 0.5);
this.platformGroup.setVelocityX(-gameOptions.platformSpeed);
}
stopPlatforms() {
this.bgGroup.setVelocityX(0);
this.platformGroup.setVelocityX(0);
}
getRightmostPlatform() {
let rightmostPlatform = 0;
this.platformGroup.getChildren().forEach((platform) => {
rightmostPlatform = Math.max(rightmostPlatform, platform.x);
});
return rightmostPlatform;
return this.platformGroup
.getChildren()
.filter(({ name }) => name)
.reduce(
(rightmostPlatform, platform) =>
Math.max(rightmostPlatform, platform.x),
0
);
}
update() {
const result = this.physics.world.collide(this.platformGroup, this.ball);
@ -164,26 +165,21 @@ class PlayScene extends Phaser.Scene {
children.forEach((platform) => {
if (platform.getBounds().right < 0) {
if (platform.name) {
this.updateScore(1);
setTimeout(() => {
this.newPlatForm(
this.getRightmostPlatform() -
Number(platform.name) * blockWidth +
this.updateScore(3 - Number(platform.name));
this.stopPlatforms();
const platformX =
this.getRightmostPlatform() +
Phaser.Math.Between(
gameOptions.platformDistanceRange[0],
gameOptions.platformDistanceRange[1]
)
);
});
this.newPlatForm(platformX);
this.movePlatforms();
}
this.platformGroup.remove(platform, true, true);
}
});
this.bgGroup.getChildren().forEach((bg) => {
if (bg.getBounds().right < 0) {
bg.x += this.game.config.width * 2;
}
}, this);
if (this.ball.y > this.game.config.height) {
this.game.onLose && this.game.onLose();
this.scene.pause();

Loading…
Cancel
Save