在javascript中跳跃代码 [英] jumping code in javascript

查看:151
本文介绍了在javascript中跳跃代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是11岁的编程新手,我正在使用JavaScript为我的学校制作一个简单的平台游戏.

I'm eleven and new to programming, and I'm making a simple platformer for a game comp at my school using javascript.

现在,我正在研究使角色跳跃的代码.它比角色上下移动更复杂,因为我希望动作流畅且看起来逼真.当角色跳跃时,它会快速离开地面,然后随着速度的升高而放慢,并且到达某个点时,它将开始缓慢下落.它会随着下落而加速(可能是通过使用某种类型的加速度变量),然后撞到地面并完全停止.

Right now I'm working on the code that makes the character jump. It's more complex than the character just going up and then down, because I want the movements to be fluent and look realistic. When the character jumps, it leaves the ground fast, then slows down as it goes higher, and when it reaches a certain point, it will start to fall slowly. It will speed up as it falls (probably by using some type of acceleration variable), and then hit the ground and stop completely.

我希望角色能够在空中左右移动,如果握住了键,则跳一次,然后当角色撞到地面(如果仍然握住键)时,它又跳一次. (游戏中角色应该可以跳得很高)

I want the character to be able to move left and right in the air, and if the key is held, jump once, and then when the character hits the ground, if the key is still held, to jump again. (the in-game character should be able to jump quite high)

我已经尝试过这样做,但是发生了一些搞笑的错误.

I've tried to do this already, but I had some hilarious errors happening.

这是我的代码(很破):

Here's my (very broken) code:

    //movement (x)
    var maxSpeed = 12.5; 
    var xForce = 0;
    var kingXPos = 0;

    //movement (y)
    var yForce = 0;
    var kingYPos = 202;

    //LV design
    var floorHeight = 150;

var draw = function() {

//background and basics
    background(255, 0, 0);
    image(getImage("creatures/Winston"), kingXPos, kingYPos, 50, 50);

//level features (only the floor right now)
    fill(0, 0, 0);
    rect(0, 250, 400, floorHeight);

//right movement
        if (keyIsPressed && keyCode === RIGHT) {
    kingXPos = kingXPos + xForce;
    xForce = xForce + 0.25;
    if (xForce >= maxSpeed && keyIsPressed) {
      xForce = maxSpeed;
    }
  }

//left movement
  if (keyIsPressed && keyCode === LEFT) {
    kingXPos = kingXPos + xForce;
    xForce = xForce - 0.25;
    if (xForce <= -maxSpeed && keyIsPressed) {
      xForce = -maxSpeed;
    }
  }

//jump (not yet functional)
  if (keyTyped && keyCode === UP && kingYPos === floorHeight + 50) {
      kingYPos = kingYPos + yForce;
      yForce = yForce - 0.5;

  }

//other physics
  if (!keyIsPressed) {
    kingXPos = kingXPos + xForce;
    if (xForce > 0) {
      xForce = xForce - 0.25;
    } 
    else if (xForce < 0) {
      xForce = xForce + 0.25;
    }
  }
};

推荐答案

对于刚开始的人来说,这是相当令人印象深刻的.您似乎对几何学有直观的了解.但是,由于受过多少教育,您可能不了解某些领域知识.

That's fairly impressive for someone just starting. You seem to have an intuitive understanding of geometry. However, there are some domain knowledge you may not be aware of due to how much education you've had.

在物理学中,描述运动的正确方程组是:

In physics, the correct set of equations that describes motion is:

1. speed = change_in_location / time

2. acceleration = change_in_speed / time

注意:我在这里使用的是速度"一词,因为它的键入要比速度"短.正确的词在技术上是速度".在物理学中,速度意味着与速度稍有不同.

您需要注意的另一件事是重力是加速度的一种形式.具体来说就是9.8m/s/s的向下加速度

Another thing you need to be aware of is that gravity is a form of acceleration. Specifically it is a downward acceleration of 9.8m/s/s

因此,重写以上所有内容:

So rewriting all of the above:

new_location = (speed * time) + old_location

new_speed = (acceleration * time) + old_speed

如果您假设动画是一个固定时间循环,则可以假设time =1.这样可以简化为:

If you assume a constant time animation loop you can assume that time = 1. So that simplifies it to:

new_location = speed + old_location

new_speed = acceleration + old_speed

这足以模拟重力.由于重力只是加速度:

This is enough to simulate gravity. Since gravity is just an acceleration:

gravity = SOME_NUMBER; // tune this to get the gravity you want

kingYPos = kingYPos + kingYSpeed;

kingYSpeed = kingYSpeed + gravity;

要跳跃,只需立即提高对象速度即可:

To jump just give the object an instant boost in speed:

// jump:
kingYSpeed = -SOME_OTHER_NUMBER; // negative because "up"


注意:领域知识是程序员在解决特定问题时必须理解的编程知识.例如,编写会计软件的程序员应具有一定的会计知识.实际上,并非所有行业的程序员都努力获取领域知识,因为有时会有系统分析师/顾问来编写软件需求.但是,当您编写自己的软件时,您别无选择,只能获得一些领域知识.


Note: Domain knowledge is knowledge outside of programming that a programmer must understand in order to solve a particular problem. For example, a programmer who writes an accounting software should have some knowledge of accounting. In practice, not all programmers in the industry make the effort to acquire domain knowledge because sometimes there's a systems analyst/consultant writing the requirements for the software. But when you write your own software you have no choice but to acquire some domain knowledge.

这篇关于在javascript中跳跃代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆