简体   繁体   English

HTML5 Canvas:在EaselJS中使用箭头键移动/平移世界

[英]HTML5 Canvas: Moving/panning world with arrow keys in EaselJS

After a year of studying and experimenting through trial-and-error, I feel that I am starting to understand JavaScript a bit more. 经过一年的反复试验和学习,我觉得我开始更多地了解JavaScript了。 So, now, I wanna try my hand at writing a simple 2D platform game (think Super Mario World or Sonic the Hedgehog). 所以,现在,我想尝试编写一个简单的2D平台游戏(想想Super Mario World或Sonic the Hedgehog)。 For this, I'll be employing the EaselJS library. 为此,我将使用EaselJS库。

I am trying to figure out how I can move/pan the 'world' within a canvas by use of the left and right arrow keys. 我试图弄清楚如何使用左右箭头键在画布中移动/平移'世界'。 I know how I can run a function upon a keydown of the arrow key, but I'm not too sure how I should approach the moving/panning. 我知道我可以在一个运行的函数keydown箭头键的,但我也不太清楚应该怎么接近移动/平移。

可视化

Should I adjust the position/coordinates of every single thing within the canvas when a key is pressed? 当按下某个键时,我应该调整画布中每个东西的位置/坐标吗? Or should I perhaps put everything in a container and move the position/coordinates of the container? 或者我应该把所有东西放在容器中并移动容器的位置/坐标?

I'll appreciate anything that nudges me into the right direction. 我会感激任何促使我走向正确方向的东西。 Tyvm :) Tyvm :)

Updated with answer The chosen answer below confirmed that I indeed had to put everything in a container so that I can set the position of that container. 更新回答 下面选择的答案证实我确实必须将所有内容放入容器中,以便我可以设置该容器的位置。 This is the code I drafted, and it works. 这是我起草的代码,它的工作原理。

// Canvas
var stage = new createjs.Stage('game');
createjs.Ticker.addEventListener('tick', tick);
function tick(event) {
    stage.update();
}

// Cave
var cave = new createjs.Bitmap('img/cave.png');
cave.x = cave.y = 0;

// World
// Pans World if the left or right arrow keys are pressed
var world = new createjs.Container();
world.x = 0;
world.y = 0;
world.addChild(cave);
stage.addChild(world);
$(window).keydown(function(e){
    switch (e.which || e.keyCode){
        case 39: // right arrow key
            world.regX += 10;
            break;
        case 37: // left arrow key
            world.regX -= 10;
            break;
    }
});

I tried updating world.x as well as world.regX . 我尝试更新world.x以及world.regX Somehow, world.regX seems to go smoother. 不知何故, world.regX似乎更顺畅了。

I think the best way is to put all display objects which will be scrolled into a scrollableObjectContainer (perhaps you have static elements like lifebar). 我认为最好的方法是将所有将滚动的显示对象放入一个scrollableObjectContainer(也许你有像lifebar这样的静态元素)。

So when you move, just update scrollableObjectContainer.regX. 所以当你移动时,只需更新scrollableObjectContainer.regX。

You can use tweenJS for a smoother scroll. 您可以使用tweenJS进行更流畅的滚动。

Just an idea... but take a look at the canvas translate function and use it at the beginning of each redraw to set the context from which to draw everything else. 只是一个想法...但是看一下画布翻译功能,并在每次重绘开始时使用它来设置绘制其他所有内容的上下文。

http://www.html5canvastutorials.com/advanced/html5-canvas-transform-translate-tutorial/ http://www.html5canvastutorials.com/advanced/html5-canvas-transform-translate-tutorial/

Usually I would agree with EaselJS for most canvas related projects, however the type of game you want to build would be better suited for melonJS. 通常我会同意EaselJS的大多数画布相关项目,但是你想要构建的游戏类型更适合melonJS。

Check it out http://melonjs.org 看看http://melonjs.org

Here is a sample of 2d top down scroll and rotation following a player. 这是一个2d自上而下滚动和旋转跟随玩家的示例。

https://jsfiddle.net/StaticDelvar/bt9ntuL5/ https://jsfiddle.net/StaticDelvar/bt9ntuL5/

But it boils down to: 但它归结为:

world.regX = player.x;
world.regY = player.y;
world.rotation = -player.rotation;

Then everything including the player goes into the World container and will be rotated as the player moves. 然后包括玩家在内的所有东西都会进入World容器,并在玩家移动时旋转。 GUI must be after world so it draws on top of it. GUI必须在世界之后才能在它之上。

  • Stage 阶段
    • World 世界
      • Objects 对象
      • Player 播放机
    • Gui

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM