3

我使用 canvasengine 来编写我的 HTML5 游戏。我已经实现了一个到目前为止工作的平铺地图,但是是静态的。

现在我希望玩家能够四处走动。所以我想:“好吧,我使用 canvasengine skew 命令”。这有效:

canvas.Scene.new ({
name: "tutorial",
materials: {
    images: {
        dg_edging232: "/maps/tilesets/dg_edging232.gif"
    }
},
ready: function(stage) {

    this.el = this.createElement();
    var tiled = canvas.Tiled.new ();
    tiled.load(this, this.el, "/maps/tutorial.json");
    tiled.ready(function() {
        var tile_w = this.getTileWidth(),
                tile_h = this.getTileHeight(),
                layer_object = this.getLayerObject();
        stage.append(this.el);
    });



},
render: function(stage) {
    canvas.Input.keyDown(Input.Left);
    canvas.Input.keyDown(Input.Right);
    canvas.Input.keyDown(Input.Up);
    canvas.Input.keyDown(Input.Down);


    stage.refresh();
}
});

现在,我想做这样的事情:

canvas.Input.keyDown(Input.left,this.el.x--);

但我不能让它与上面的语法一起工作。

4

1 回答 1

0

从未使用过此库,但看起来您可能希望将渲染中的keyDown调用移动到ready,并在渲染时测试是否按下了键并相应地移动精灵。

ready: function () {
  // ... your ready code

  // It's not clear from the docs, but it appears like you need to call
  // keyDown for each input to register it, even if you're passing no callbacks.
  // However, I'm guessing here.  This may not be necessary.
  canvas.Input.keyDown(Input.Left);
  // ... and so on for each direction
},

render: function () {
  // every frame test if the key is down, and update the sprite accordingly
  if (canvas.Input.isPressed(Input.Left)) this.el.x--;
  // ... and so on for each direction

  stage.refresh();
}
于 2013-05-06T16:16:14.850 回答