【发布时间】:2016-06-11 17:45:06
【问题描述】:
我正在使用 xdk 进行 Phaser html 5 游戏开发。
我正在使用他们网站上的示例点击计数器,但它不起作用。有人可以帮我吗?
下面是代码:
/* 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, this);
【问题讨论】:
-
您是否检查过
asset/phaser.png是否正确且文件是否存在?我也建议检查服务器所在的目录(如果有的话)。 -
我正在使用 xdk 我不认为所以有任何服务器需要我手动启动/停止 ide 应该照顾好这个
-
我明白了。您可以提供的另一个信息是例外情况。您发布的代码是产生异常但不是异常本身的代码。 xdk 不会在文本中给出错误吗?
-
另外,我谈到了检查路径,因为通常它是复数形式的
assets。以防拼写错误。 -
嗨 this.logo.events.onInputDown.add(onClickListener);是未定义错误“onClickListener”。 (W117),抱歉,是的,phaser.png 正在加载,没有问题。
标签: intel-xdk phaser-framework