简体   繁体   English

HTML5游戏滚动

[英]HTML5 game scrolling

so here I am, making a HTML5 and javascript game, and I'm trying to make the character walk to the middle of the screen, stop scrolling, and then continue walking when the camera hits the edge of the screen. 所以我在这里,正在制作一个HTML5和Javascript游戏,我试图使角色走到屏幕中间,停止滚动,然后在相机碰到屏幕边缘时继续走动。

http://gifmaker.me/PlayFrameAnimation.php?folder=20140629160DoiXmJ9u4hISNMxDsnhUI like this thing. http://gifmaker.me/PlayFrameAnimation.php?folder=20140629160DoiXmJ9u4hISNMxDsnhUI喜欢这个东西。

if (world.regX > 0 && world.regX < backgroundEdge-camera.width && jerome.x > camera.width/2) {
                jerome.x = camera.width/2 };
if (world.regX > 0 && world.regX < backgroundEdge-camera.width && jerome.x < camera.width/2) { 
                jerome.x = camera.width/2 };

(world is the container for the background image.) (世界是背景图片的容器。)

this works fine, so long as you're not turning around near the edges of the screen, because it will just jump forward to the middle of the screen, and go from there. 只要您不在屏幕边缘附近转过转,它就可以正常工作,因为它只会向前跳到屏幕中间,然后从那里去。

the answer to this question is probably incredibly obvious, but I just cannot think of it for the life of me, so sorry if I'm sounding stupid here. 这个问题的答案可能非常明显,但我只是一生都想不起来,如果在这里听起来很蠢,对不起。

any kind of help would be appreciated, because I've been stuck on this for a while now. 任何形式的帮助将不胜感激,因为我已经坚持了一段时间。

thanks in advance! 提前致谢!

Using Simon Sarris' answer as a base, I came up with this demo 以Simon Sarris的回答为基础,我想到了这个演示

All I added was the following: 我添加的内容如下:

if(offsetX >= -worldW + can.width / 2) { // Handle right edge
    if(offsetX <= 0) {                   // Handle left edge
        ctx.translate(offsetX, offsetY);
    } else {
        // Do nothing, default position
    }        
} else { 
    ctx.translate(-worldW + can.width / 2, 0);
}

Essentially, this just checks to see if the object is within half of a screen of the "world's width". 本质上,这只是检查对象是否在“世界宽度”屏幕的一半以内。 If it's not within that, it moves the "camera" by translating the context. 如果不在此范围内,它将通过翻译上下文来移动“相机”。 If it is within that, it keeps a set position depending on which requirement is not being met. 如果在其中,则根据未满足的要求将其保持在设定的位置。

Hope it helps! 希望能帮助到你!

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

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