나는 flappy bird 기반 복제 게임을 가지고 있으며 그것에 대한 높은 점수 기능을 만들어야합니다. 게임이 다시로드되고 High Score를 전역 변수로 설정하는 방법을 모르기 때문에 최고 점수는 순식간에 사라집니다. 당신이 그것을보고 싶다면 여기에 링크가 있습니다. (경고 : 헤드폰 볼륨 낮추기)
http://www.theindependentwolf.com/game/flappyWolf.html
// Initialize Phaser, and creates a 400x490px game
var game = new Phaser.Game(400, 490, Phaser.AUTO, 'gameDiv');
// Creates a new 'main' state that will contain the game
var mainState = {
// Function called first to load all the assets
preload: function() {
// Change the background color of the game
// game.stage.backgroundColor = '#71c5cf';
//background Image
game.load.image('woods', 'woods.jpg');
// Load the bird sprite
game.load.image('bird', 'whiteWolf2.png');
// Load the pipe sprite
game.load.image('pipe', 'pipe.png');
},
// Fuction called after 'preload' to setup the game
create: function() {
// Set the physics system
game.physics.startSystem(Phaser.Physics.ARCADE);
game.add.tileSprite(0, 0, 400, 490, 'woods');
// Display the bird on the screen
this.bird = this.game.add.sprite(100, 245, 'bird');
// Add gravity to the bird to make it fall
game.physics.arcade.enable(this.bird);
this.bird.body.gravity.y = 1000;
// Call the 'jump' function when the spacekey is hit
var spaceKey = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
spaceKey.onDown.add(this.jump, this);
// Create a group of 20 pipes
this.pipes = game.add.group();
this.pipes.enableBody = true;
this.pipes.createMultiple(20, 'pipe');
// Timer that calls 'addRowOfPipes' ever 1.5 seconds
this.timer = this.game.time.events.loop(1500, this.addRowOfPipes, this);
// Add a score label on the top left of the screen
this.score = 0;
this.hiScore = 0;
this.scoreLabel = this.game.add.text(20, 20, "Score: ", { font: "30px Arial", fill: "#ffffff" });
this.labelScore = this.game.add.text(120, 20, "0", { font: "30px Arial", fill: "#ffffff" });
this.hiScoreLabel = this.game.add.text(200, 20, "Hi Score: ", { font: "30px Arial", fill: "#ffffff" });
labelHiScore = this.game.add.text(340, 20, "0", { font: "30px Arial", fill: "#ffffff" });
/*
Code for the pause menu
*/
},
// This function is called 60 times per second
update: function() {
// If the bird is out of the world (too high or too low), call the 'restartGame' function
if (this.bird.inWorld == false)
this.restartGame();
// If the bird overlap any pipes, call 'restartGame'
game.physics.arcade.overlap(this.bird, this.pipes, this.restartGame, null, this);
},
// Make the bird jump
jump: function() {
// Add a vertical velocity to the bird
this.bird.body.velocity.y = -350;
},
// Restart the game
restartGame: function() {
// Start the 'main' state, which restarts the game
if(this.score > this.hiScore ){
labelHiScore.text = this.score;
}
game.state.start('main');
},
// Add a pipe on the screen
addOnePipe: function(x, y) {
// Get the first dead pipe of our group
var pipe = this.pipes.getFirstDead();
// Set the new position of the pipe
pipe.reset(x, y);
// Add velocity to the pipe to make it move left
pipe.body.velocity.x = -200;
// Kill the pipe when it's no longer visible
pipe.checkWorldBounds = true;
pipe.outOfBoundsKill = true;
},
// Add a row of 6 pipes with a hole somewhere in the middle
addRowOfPipes: function() {
var hole = Math.floor(Math.random()*5)+1;
for (var i = 0; i < 8; i++)
if (i != hole && i != hole +1)
this.addOnePipe(400, i*60+10);
this.score += 1;
this.labelScore.text = this.score;
},
};
// Add and start the 'main' state to start the game
game.state.add('main', mainState);
// document.getElementById("#startButton").onclick(function(){
// alert("Hi");
labelHiScore = 0;
game.state.start('main');
// });
다음 변경 사항을 시도하십시오.
restartGame 메서드에서 :
restartGame: function() {
if(this.score > localStorage.getItem("hiScore") ){
localStorage.setItem("hiScore", this.score);
this.labelHiScore.text = localStorage.getItem("hiScore");
}
game.state.start('main');
},
"labelHiScore"가변 선언에서 :
this.labelHiScore = this.game.add.text(340, 20, ("hiScore" in localStorage ? localStorage.getItem("hiScore") : "0"), { font: "30px Arial", fill: "#ffffff" });
간단히 말해서 문제가 무엇인지 파악했으면합니다.
restartGame 메서드에서 기능 범위 대신 창 범위 변수에 점수를 할당하려고합니다. "이"를 추가합니다. 현재 개체에서 변수를 찾습니다.
또한 최고 점수를 유지하고 게임이 다시로드 될 때 로컬 저장소에서 값을 선택하기 위해 로컬 저장소에 점수를 추가했습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다