2016-03-27 18 views
1

'daki bir metni nasıl yapabilirim ve güncelleyebilirim Oluştur işlevinde game.add.text(...) arasında bir sayı görüntüleyen bir değişken yaptım, ancak değiştiğinden beri her düğmeyi bastığımda metnin güncellenmesini istiyorum. Ve eğer güncelleme fonksiyonunda game.add.text(...) yaparsam yığınlanır ve birçok metin katmanı görünür.Phaser (HTML)

cevap

0

Size bunun nasıl işleneceği hakkında bir fikir vermesi gereken basit bir sürüm.

<script> 
    var game = new Phaser.Game(400, 300, Phaser.AUTO, 'phaser-example', { preload: preload, create: create }); 
    // You'll need to make sure your button click action can access the number to display. 
    var clickCount = 0; 
    // We'll also need to keep track of the text object that we add to the game. 
    var clickCounter; 

    function preload() { 
     game.load.image('button', 'assets/button.png'); 
    } 

    function create() { 
     game.stage.backgroundColor = '#124184'; 
     // When creating the text object, make sure it'll be accessible from our button handler. 
     clickCounter = game.add.text(5, 5, 'Total clicks: ' + clickCount, { fill: '#ffffff', font: '14pt Arial' }); 

     game.add.button(200, 150, 'button', actionOnClick, this); 
    } 

    function actionOnClick() { 
     clickCount++; 
     // The key here is setText(), which allows you to update the text of a text object. 
     clickCounter.setText('Total clicks: ' + clickCount); 
    } 
</script> 

Yani, yerine yeni bir metin nesnesi oluşturmak yerine, oluşturmak birine başvuru depolamak ve güncellemek setText() kullanın.

+0

Teşekkürler! Şimdiye kadar harika çalışıyor – BaysQuorv

+0

Küçük bir sorunum olsa da. Kendi resmimi ekleyemiyorum. "Asset/button.png" ifadesini düğme resminin adlandırıldığı "button.png" olarak değiştirdim. Fakat asıl oyunda hiç görüntü yok ve düğme yok oluyor. Ama "button.png" yi mevcut olmayan bir şeye yaptığımda, bu düğme yeşil kare olarak görünür. Düğmeye stil vermenin herhangi bir yolu var mı? gerçek bir görüntü eklemeden – BaysQuorv

+0

Tarayıcınızdaki ağ sekmesine bakarsanız ve görüntüyü yüklerken yenileme 404 gösterir mi? Ve evet, elle bir arka plan çizebilir veya bir metin öğesi kullanabilir ve stil uygulayabilirsiniz. –

0

bu deneyin:

clickBtn: No: = 0; // örneğin

game.add.text ('Clicks' + this.clickBtn);

this.btn = this.add.button (bazı kod, this.yourFunction);

this.yourFunction() {

this.clickBtn + = 1 'dir;

}

Size yardımcı olacağını düşünüyorum.

Saygılarımızla, Sergey.