【问题标题】:Phaser Game Development XDKPhaser 游戏开发 XDK
【发布时间】: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


【解决方案1】:

由于onClickListener 坐在BasicGame.Game.prototype 中,您应该从this 获得它:

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

【讨论】:

  • Hurray 已编译...但我看不到文字 You clicked " + counter + " times!";
  • 如何将文本变量传递给该函数?
  • 关于文本,对我来说一切似乎都是正确的。您应该在this.counterLabel.text = ... 处放置一个断点,以至少查看是否正确创建了文本(如果证明很难,请将代码与var text = "..." + counter + "..." 分开)。关于传递文本,您可以使用实例变量 (this.logo) 或闭包中的变量 (counter)。你已经在这样做了。
  • 很遗憾我对所有这些 xdk 东西都是新手... :'( 你能指点我一些参考学习资源吗 感谢您帮助 anwyays...
  • 抱歉通知我们两个。根据我的阅读,XDK 使用 Chrome 开发工具(如果不尝试启用 CDT),并且在调试时使用它的调试器(这是个好消息)。 Here你可以找到说明。
猜你喜欢
  • 2017-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多