2017-11-10 122 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中隔離該問題。祝你好運。