简体   繁体   English

垂直移动div时水平移动div

[英]Moving div horizontally while it's moving vertically

I am currently creating a game, a platformer. 我目前正在创建一个游戏,一个平台游戏。 I already have the character moving around horizontally, however, I do not really know how I will make him jump... and move at the same time. 我已经使角色水平移动了,但是,我真的不知道如何使他跳起来并同时移动。

I decided not to use jQuery animate to move the character around, therefore I have no idea how to enable the character to move at the same time as a jump. 我决定不使用jQuery animate来移动角色,因此我不知道如何使角色在跳跃的同时移动。 I have seen an example of perfect jumping and moving with jQuery on this JSFiddle , however it uses animate . 我已经在这个JSFiddle上看到了使用jQuery完美跳跃和移动的示例 ,但是它使用了animate

How can I make the character jump (smoothly), and be able to move while jump at the same time (without using animate)? 如何使角色跳跃(平稳), 能够同时跳跃(不使用动画)而移动?

I have a CodePen (the CSS in the Pen is SCSS, but it's CSS here), but here is the code anyways: 我有一个CodePen (笔中的CSS是SCSS,但是这里是CSS),但无论如何这是代码:

 var game_anim = function() { var level = [ [0, 1, "l", 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, "l", "l", 1], [0, 0, 0, 0, 2, 2, 2, 2, 2, 2], [0, 0, 0, 0, 0, 3, 3, 0, 3], ]; var $player = $(".player"); var $game = $(".game"); $(document).keydown(function(event) { // keycodes: left = 37, right = 39 if (event.which == 39 || event.which == 68) { // right arrow or D if ($player.position().left < $game.width() - $player.width()) { $player.css("left", "+=10"); } } if (event.which == 37 || event.which == 81 || event.which == 65) { // left arrow or Q on AZERTY or A on QWERTY if ($player.position().left > 0) { $player.css("left", "-=10"); } } if (event.which == 38) { if ($player.position().top > 0) { $player.css("top", "-=10"); } } }); } $(document).ready(function() { game_anim(); }); 
 .game { position: absolute; left: calc((100% - 800px)/2); height: 500px; width: 800px; border: 2px solid black; } .block { height: 50px; width: 50px; position: absolute; } .stone { background-color: black; } .lava { background-color: red; } .player { height: 50px; width: 50px; background-color: #3747C0; bottom: 0; position: absolute; } .player .eyes { border-radius: 50%; background-color: white; position: absolute; height: 10px; width: 10px; } .player .eye_R { left: 7px; top: 10px; } .player .eye_L { left: 32px; top: 10px; } .player .mouth { height: 8.5px; width: 32px; background-color: white; border-radius: 5px; left: calc((50px - 32px)/2); bottom: 10px; position: absolute; } .ypos-0 { bottom: 0px; position: absolute; } .ypos-1 { bottom: 50px; position: absolute; } .ypos-2 { bottom: 100px; position: absolute; } .ypos-3 { bottom: 150px; position: absolute; } .ypos-4 { bottom: 200px; position: absolute; } .ypos-5 { bottom: 250px; position: absolute; } .ypos-6 { bottom: 300px; position: absolute; } .ypos-7 { bottom: 350px; position: absolute; } .ypos-8 { bottom: 400px; position: absolute; } .xpos-0 { left: 0px; /*position: absolute;*/ } .xpos-1 { left: 50px; /*position: absolute;*/ } .xpos-2 { left: 100px; /*position: absolute;*/ } .xpos-3 { left: 150px; /*position: absolute;*/ } .xpos-4 { left: 200px; /*position: absolute;*/ } .xpos-5 { left: 250px; /*position: absolute;*/ } .xpos-6 { left: 300px; /*position: absolute;*/ } .xpos-7 { left: 350px; /*position: absolute;*/ } .xpos-8 { left: 400px; /*position: absolute;*/ } .xpos-9 { left: 450px; /*position: absolute;*/ } .xpos-10 { left: 500px; /*position: absolute;*/ } .xpos-11 { left: 550px; /*position: absolute;*/ } .xpos-12 { left: 600px; /*position: absolute;*/ } .xpos-13 { left: 650px; /*position: absolute;*/ } .xpos-14 { left: 700px; /*position: absolute;*/ } .xpos-15 { left: 750px; /*position: absolute;*/ } .xpos-16 { left: 800px; /*position: absolute;*/ } .xpos-17 { left: 850px; /*position: absolute;*/ } .xpos-18 { left: 900px; /*position: absolute;*/ } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class = "game"> <div class = "player"> <div class = "eyes eye_R"></div> <div class = "eyes eye_L"></div> <div class = "mouth"></div> </div> </div> 

Can someone please help? 有人可以帮忙吗?

You can add a transition: 0.5s to you .player class, this will smooth the movement. 您可以在您的.player类中添加一个transition: 0.5s ,这将使运动平滑。 I'd also consider using css translate to move the object since changing top, left, right and bottom calls a redraw which will cause frame drops latter down the rode. 我还考虑使用css转换来移动对象,因为更改上,左,右和下调用会重画,这将导致帧掉落到骑行者身上。

http://www.w3schools.com/css/css3_transitions.asp http://www.w3schools.com/css/css3_transitions.asp

Here's a nice read on animation perfomance: http://blogs.adobe.com/webplatform/2014/03/18/css-animations-and-transitions-performance/ 这是有关动画性能的不错的阅读: http : //blogs.adobe.com/webplatform/2014/03/18/css-animations-and-transitions-performance/

if you want to "fall" after jumping you'll also need to add code for that 如果您想在跳跃后“摔倒”,还需要为此添加代码

if (event.which == 38) {
    if ($player.position().top > 0) {
        $player.css("top", "-=10");
        setTimeout(function(){
            $player.css("top", "+=10");
        }, 500); //500 since I suggested 0.5s in css transition
    }
}

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

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