我使用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--);
但我无法使用上述语法。
答案 0 :(得分: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();
}