2016-08-03 4 views
0

사용 후 나는이 같은 세 개의 버튼이 있습니다사용 안 함을 클릭 phaser.io에서 한 번의 클릭 자바 스크립트

astroid: function() { 

    astroid1 = this.add.button(15, 80, 'astroid1', this.astroid1Clicked, this); 
    astroid2 = this.add.button(200, 10, 'astroid2', this.astroid2Clicked, this); 
    astroid3 = this.add.button(400, 40, 'astroid3', this.astroid3Clicked, this); 
    astroid4 = this.add.button(622, 90, 'astroid4', this.astroid4Clicked, this); 
}, 

을 모든 버튼에 대한 기능을 가지고는 클릭 :

// Observing which asteroid is clicked and checking answer accordingly. 
astroid1Clicked: function() { 
    this.fire(); 

    //console.log(astroidContains[0]); 
    // console.log(answear); 
    //this.isCorrectAnswerHit(25, 100); 
    if (astroidContains[0] == answear) { 
     setTimeout(function() { 
      //50 and 100 are the axis of astroid1 
      Game.isCorrectAnswerHit(50, 100); 
     }, 500); 
    } else { 
     this.isWrongAnswerHit(); 
    } 
    allowClick = false; 

    return false; 
}, 

astroid2Clicked: function() { 

    this.fire(); 

    //console.log(astroidContains[1]); 
    //console.log(answear); 
    if (astroidContains[1] == answear) { 
     setTimeout(function() { 
      Game.isCorrectAnswerHit(260, 10); 
     }, 500); 
    } else { 
     this.isWrongAnswerHit(); 
    } 

    allowClick = false; 
    return false; 
}, 

astroid3Clicked: function() { 

    this.fire(); 

    //console.log(astroidContains[2]); 
    //console.log(answear); 
    if (astroidContains[2] == answear) { 
     setTimeout(function() { 
      Game.isCorrectAnswerHit(450, 40); 
     }, 500); 
    } else { 
     this.isWrongAnswerHit(); 
    } 
    allowClick = false; 
    return false; 
}, 

astroid4Clicked: function() { 

    this.fire(); 
    //bullets.destroy(); 
    //console.log(astroidContains[3]); 
    //console.log(answear); 
    if (astroidContains[3] == answear) { 
     setTimeout(function() { 
      Game.isCorrectAnswerHit(620, 100); 
     }, 500); 
    } else { 
     this.isWrongAnswerHit(); 
    } 
    allowClick = false; 
    return false; 
}, 

내가 하나에 사용자가 클릭으로 원하는 버튼은 사용자가 클릭하는 버튼을 포함하여 모든 버튼이 비활성화 될 때 모든 기능이 실행되고 사용자는 아무 버튼이나 클릭 할 수 있습니다. 많은 것을 시도했지만 그들이 원하는대로 작동하지 않도록 도와주세요. jQuery가 없으면됩니다.

+0

먼저 코드를 리팩터링해야한다고 생각합니다. 반복되는 내용이 많습니다. 예를 들어, 대답 확인 논리를 매개 변수로 필요한 정보를 전달하는 별도의 함수로 옮길 수 있습니다. –

+0

모든 버튼을 클릭 할 때마다 다른 작업을 수행하기 때문에 작업이 완료 될 때까지 코드를 반복하고 있습니다. 다른 또는 동일한 버튼 클릭을 방지하고 싶습니다. –

답변

1

왜 이미 가지고있는 깃발을 사용하지 않습니까?

astroid4Clicked: function() { 
    if (this.allowClick == true) { 
    effect... 
    this.allowClick = false; 
    } 
} 

그런 다음 플래그를 다시 true로 설정하는시기와 상황을 결정해야합니다.

update() 함수에서 플래그를 확인하는 것이 좋으며 모든 함수에서 분리하지 않는 것이 좋습니다.

그리고 네 당신은 코드를 많이 반복이 ...

+0

내 바보 같은 실수를 지적 해 주셔서 감사합니다. 지금은 제가 원하는대로 작업하고 있습니다. –

0

나의 추천 콜백을 사용하고 버튼 클래스를 사용하지 않는 것입니다.

// Assuming `this` is the Phaser.State -- this is for callback safety 
let thisState = this 
var myFireFunction = function(event) { 
    thisState.fire() 
    // ... 

    // Add a delay timer (or, this could be a callback when the bullet strikes the target) 
    var delay = thisState.game.time.create(true) 
    delay.duration = 500 //milliseconds 

    // add the callback to the input again 
    delay.onComplete.addOnce(function() { 
     thisState.input.onDown.addOnce(myFireFunction) 
    }) 
    delay.start() 
} 

// Add the callback ONCE to the `onDown` event 
this.input.onDown.addOnce(myFireFunction) 

이렇게하면 사용자가 빠르게 화면을 누르고 여러 스레드에서 여러 이벤트를 호출하지 못하게됩니다. 게임이 첫 번째 이벤트 처리를 완료하면 onDown 신호에 콜백을 추가 할 수 있습니다.

이 아이디어를 확장하는 방법을 알아 보려면 Phaser's powerful Signal ClassTimer Class 및 관련 자습서를 찾아보십시오.

관련 문제