如何使 if 语句只运行一次。我正在用 javascript 制作一个游戏,一旦用户得分为 5,我想在数组中添加一个新的敌人。我遇到的问题是,一旦 if 语句为真,它就会一遍又一遍地添加新的敌人,直到整个屏幕都被敌人覆盖。我希望它在分数为 5 时仅添加 1 个新敌人,然后在分数为 10 时添加另一个,依此类推。我想不出有什么方法可以实现这一点。谢谢。
素描.js
var score = 0;
//3 three enemies once game starts
function StartingEnemies() {
for (var i = 0; i < 2; i++) {
enemies[i] = new BasicEnemy();
}
}
//add new enemies into the array
function spawnNewEnemies() {
enemies.push(new BasicEnemy());
}
if (score == 5) {
spawnNewEnemies();
}
Run Code Online (Sandbox Code Playgroud) 如何控制动画的速度?翻译动画从下到上,我想在动画执行期间放慢速度.我怎样才能做到这一点?这是我有的:
public void SlideToAbove() {
Animation slide = null;
slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
slide.setDuration(300);
slide.setFillAfter(true);
slide.setFillEnabled(true);
toolBar.startAnimation(slide);
final Animation finalSlide = slide;
slide.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
CoordinatorLayout.LayoutParams lp = new CoordinatorLayout.LayoutParams(
toolBar.getWidth(), toolBar.getHeight());
lp.setMargins(0, 0, 0, 0);
appBarLayout.setLayoutParams(lp);
finalSlide.setFillAfter(true);
}
});
Run Code Online (Sandbox Code Playgroud)