2017-11-10 118 views
0

我正试图编写一个程序,其中游戏的结束屏幕只在最后一个动画结束后才显示。我使用的计数器在每个对象被删除后执行(只有在它完成动画后),并且当计数器归零时,它应该应该显示结束屏幕。不幸的是,从我所知道的情况来看,反陈述根本没有注册。我插入了一个不起作用的打印语句。p5.play counter not working

var star; 
var score; 
var counter; 
function setup() { 
    createCanvas(600,400); 
    score = 0; 
    counter = 20; 

    for (var s = 0; s < 20; s++) { 
     star = createSprite(random(width), random(height)); 
     star.addAnimation("idle", idleAnim); 
     star.addAnimation("explode", explAnim); 
     star.changeAnimation("idle"); 

     star.onMousePressed = function() { 
      this.changeAnimation("explode"); 
      this.animation.looping = false; 
      score +=1 
      if (this.getAnimationLabel() == "explode" && this.animation.getFrame() == this.animation.getLastFrame()) { 
       this.remove(); 
       counter -= 1; 
       print(counter); 
      } 
     } 
    } 

} 
function draw() { 
    if (score == 20 && counter == 0) { 
     background(255,222,51) 
     textSize(90); 
     fill(0) 
     text("YOU WIN!",95,225) 
    } else { 
      drawSprites(); 
    } 
    } 

回答

0

你需要退后一步,并debug your program。例如,你确定star.onMousePressed()函数正在触发吗?您确定if声明按照您的预期工作吗?你确定player.dir()函数被调用吗?

这听起来像您的if声明没有被输入。你能找出那条线上的所有东西的价值吗?哪件事与你预期的价值不同?

使用console.log()语句或使用JavaScript调试器来回答以上所有问题。找出哪一行代码的行为与预期不同,然后在MCVE中隔离该问题。祝你好运。