2016-06-11 79 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')。你已经在做这件事了。 –