<分区>
我正在学习如何使用 Phaser 框架编写 HTML5 游戏。作为学习过程的一部分,我正在做的一件事是研究和改编我在网上找到的例子。我现在正在处理的示例是 4096/2048/Threes clone .在示例源代码中,我看到了一个奇怪的模式。作者在操作某个变量时,始终在变量和自增/自减运算符之间放置一个空格。例如,下面是他们源代码的第 248 到 268 行:
moveTile: function(tile, row, col, distance, changeNumber){
this.movingTiles ++; // example gap
this.tweens.add({
targets: [tile.tileSprite],
x: this.tileDestination(col, COL),
y: this.tileDestination(row, ROW),
duration: gameOptions.tweenSpeed * distance,
onComplete: function(tween){
tween.parent.scene.movingTiles --; // example gap
if(changeNumber){
tween.parent.scene.transformTile(tile, row, col);
}
if(tween.parent.scene.movingTiles == 0){
tween.parent.scene.scoreText.text = tween.parent.scene.score.toString();
tween.parent.scene.bestScoreText.text = tween.parent.scene.bestScore.toString();
tween.parent.scene.resetTiles();
tween.parent.scene.addTile();
}
}
})
},
令我惊讶的是,这仍然增加/减少了 this.movingTiles
变量。由于开发人员在与 this.movingTiles
变量交互时始终这样做,我相信这一定是经过深思熟虑的选择。此外,在设置 for 循环时,开发人员不会在变量和递增/递减运算符之间放置间隙。这意味着它是有针对性和一致的行为。
increment/decrement operator 上的 Mozilla 文档仅提及前置和附加递增/递减运算符之间的区别。他们不讨论增加差距。我对 javascript 相当熟悉,但我当然不是专家。所以我想问你有没有办法解决他们的疯狂,或者这只是一种奇怪的特质?
我想明确表示,我没有不尊重原开发者的意思。他的例子对我来说是无价的学习工具。我只是想确保我理解其中的细微差别。