2016-06-11 38 views
0

我使用xdk進行phaser html 5遊戲開發。 我從他們的網站使用示例點擊計數器,但它不工作。有人能幫我嗎?
這裏是代碼如下:Phaser Game Development XDK

/* globals Phaser:false */ 
// create BasicGame Class 
BasicGame = { 

}; 

// create Game function in BasicGame 
BasicGame.Game = function (game) { 
}; 

var counter = 0; 
// set Game function prototype 
BasicGame.Game.prototype = { 

    init: function() { 
     // set up input max pointers 
     this.input.maxPointers = 1; 
     // set up stage disable visibility change 
     this.stage.disableVisibilityChange = true; 
     // Set up the scaling method used by the ScaleManager 
     // Valid values for scaleMode are: 
     // * EXACT_FIT 
     // * NO_SCALE 
     // * SHOW_ALL 
     // * RESIZE 
     // See http://docs.phaser.io/Phaser.ScaleManager.html for full document 
     this.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL; 
     // If you wish to align your game in the middle of the page then you can 
     // set this value to true. It will place a re-calculated margin-left 
     // pixel value onto the canvas element which is updated on orientation/
     // resizing events. It doesn't care about any other DOM element that may 
     // be on the page, it literally just sets the margin. 
     this.scale.pageAlignHorizontally = true; 
     this.scale.pageAlignVertically = true; 
     // Force the orientation in landscape or portrait. 
     // * Set first to true to force landscape. 
     // * Set second to true to force portrait. 
     this.scale.forceOrientation(false, true); 
     // Sets the callback that will be called when the window resize event 
     // occurs, or if set the parent container changes dimensions. Use this 
     // to handle responsive game layout options. Note that the callback will 
     // only be called if the ScaleManager.scaleMode is set to RESIZE. 
     this.scale.setResizeCallback(this.gameResized, this); 
     // Set screen size automatically based on the scaleMode. This is only 
     // needed if ScaleMode is not set to RESIZE. 
     this.scale.updateLayout(true); 
     // Re-calculate scale mode and update screen size. This only applies if 
     // ScaleMode is not set to RESIZE. 
     this.scale.refresh(); 

    }, 

    preload: function() { 

     // Here we load the assets required for our preloader (in this case a 
     // background and a loading bar) 
     this.load.image('logo', 'asset/phaser.png'); 
    }, 

    create: function() { 
     // Add logo to the center of the stage 
     this.logo = this.add.sprite(
      this.world.centerX, // (centerX, centerY) is the center coordination 
      this.world.centerY, 
      'logo'); 
     // Set the anchor to the center of the sprite 
     this.logo.anchor.setTo(0.5, 0.5); 
     this.logo.inputEnabled = true; 
     this.logo.events.onInputDown.add(onClickListener, this); 

     //no add text on screen to check click counter 
     this.counterLabel = this.add.text(250, 16, '' , {fill:'FFFFFF'}); 


    }, 

    onClickListener: function() { 
     counter++; 
    this.counterLabel.text = "You clicked " + counter + " times!"; 
    }, 

    gameResized: function (width, height) { 

     // This could be handy if you need to do any extra processing if the 
     // game resizes. A resize could happen if for example swapping 
     // orientation on a device or resizing the browser window. Note that 
     // this callback is only really useful if you use a ScaleMode of RESIZE 
     // and place it inside your main game state. 

    } 

}; 

它引發此例外:

this.logo.events.onInputDown.add(onClickListener,本);

+0

你檢查,如果'資產/ phaser.png'是正確的,文件是否存在?如果您有任何服務器,我也建議您檢查服務器的目錄。 –

+0

我使用xdk我不這麼認爲有任何服務器,我需要啓動/停止手動ide應該照顧的 – sector11

+0

我明白了。您可以提供的另一個信息是例外情況。您發佈的代碼是生成異常的代碼,但不是異常本身。 xdk文本中是否有錯誤? –

回答

1

由於onClickListener坐在BasicGame.Game.prototype,你應該從this得到它:

this.logo.events.onInputDown.add(this.onClickListener, this); 
+0

華友世紀編譯...但我不能看到您點擊「+ counter +」次的文本!「; – sector11

+0

我該如何傳遞文本變量到該功能? – sector11

+0

關於該文本,一切似乎都是正確的,你應該把一個斷點在'this.counterLabel.text = ...'查看文本是否正確創建(如果證明有困難,用'var text =「...」+ counter +「...」分隔代碼' )。關於傳遞文本,你可以使用實例變量('this.logo')或者變量在閉包中('counter')。你已經在做這件事了。 –