javascript 中的长时间跳跃精灵?
Prolonged jumping sprite in javascript?
我正在构建一个 2d 平台游戏,现在我正在尝试让我的玩家在您按住 space 栏时跳得更远。现在玩家可以上下跳跃,但我喜欢按住键时机器人独角兽的那种效果。我该怎么做呢?我没有使用移相器或任何东西,我看到的大多数教程都使用它。这是我的更新方法中的内容:
var gameEngine = this.game;
if (gameEngine.keyMap["Space"] && !this.jumping) { //only jump if not already in mid jump
this.jumping = true;
this.animationCurrent.elapsedTime = 0;
this.game.space = false;
}
if (this.jumping) {
if (this.animationCurrent.isDone()) {
this.animationCurrent.elapsedTime = 0;
this.jumping = false;
}
var jumpDistance = this.animationCurrent.elapsedTime /
this.animationCurrent.totalTime;
var totalHeight = 150;
if (jumpDistance > 0.5)
jumpDistance = 1 - jumpDistance;
var height = totalHeight * (-4 * (jumpDistance * jumpDistance - jumpDistance));
this.y = this.ground - height;
console.log(this.y);
}
你基本上需要实现一个系统,通过按住跳跃键继续向角色施加逐渐减小的力。
您目前似乎正在将跳跃高度绑定到动画中?正是由于这个原因而不理想;调整东西变得很痛苦。您需要更多基于物理学的方法。
基本信封背面伪代码:
旧式持续跳跃动作:
const JUMP_STRENGTH = 100;
const GRAVITY = 10;
onJumpKeyupPress(){
if(play.canJump)
player.transform.up.velocity = JUMP_STRENGTH;
}
gameTick(){
if(!player.onGround)
{
player.up.velocity -= GRAVITY
player.y += player.up.velocity;
}
else{
player.up.velocity = 0;
}
}
如您所见,这会利用重力将玩家角色拉下超时。但是当用户按下 jump
时,所有的力都会立即施加
按住跳跃键的同时跳跃更大:
const JUMP_STRENGTH = 100;
const GRAVITY = 10;
var jumpStrength = 0;
onJumpKeyupPress(){
if(play.canJump)
jumpStrength = JUMP_STRENGTH;
}
gameTick(){
if(!player.onGround)
{
if(jumpKeyIsStillPressed){
player.up.velocity += jumpStrength;
jumpStrength /= 2;
}
player.up.velocity -= GRAVITY;
player.y += player.up.velocity;
}
else{
player.up.velocity = 0;
jumpStrength = 0;
}
}
在此示例中,用户的初始跳跃强度为 100,每次跳动减半,直到重力变强。
希望我说得够清楚了!
我正在构建一个 2d 平台游戏,现在我正在尝试让我的玩家在您按住 space 栏时跳得更远。现在玩家可以上下跳跃,但我喜欢按住键时机器人独角兽的那种效果。我该怎么做呢?我没有使用移相器或任何东西,我看到的大多数教程都使用它。这是我的更新方法中的内容:
var gameEngine = this.game;
if (gameEngine.keyMap["Space"] && !this.jumping) { //only jump if not already in mid jump
this.jumping = true;
this.animationCurrent.elapsedTime = 0;
this.game.space = false;
}
if (this.jumping) {
if (this.animationCurrent.isDone()) {
this.animationCurrent.elapsedTime = 0;
this.jumping = false;
}
var jumpDistance = this.animationCurrent.elapsedTime /
this.animationCurrent.totalTime;
var totalHeight = 150;
if (jumpDistance > 0.5)
jumpDistance = 1 - jumpDistance;
var height = totalHeight * (-4 * (jumpDistance * jumpDistance - jumpDistance));
this.y = this.ground - height;
console.log(this.y);
}
你基本上需要实现一个系统,通过按住跳跃键继续向角色施加逐渐减小的力。
您目前似乎正在将跳跃高度绑定到动画中?正是由于这个原因而不理想;调整东西变得很痛苦。您需要更多基于物理学的方法。
基本信封背面伪代码:
旧式持续跳跃动作:
const JUMP_STRENGTH = 100;
const GRAVITY = 10;
onJumpKeyupPress(){
if(play.canJump)
player.transform.up.velocity = JUMP_STRENGTH;
}
gameTick(){
if(!player.onGround)
{
player.up.velocity -= GRAVITY
player.y += player.up.velocity;
}
else{
player.up.velocity = 0;
}
}
如您所见,这会利用重力将玩家角色拉下超时。但是当用户按下 jump
时,所有的力都会立即施加按住跳跃键的同时跳跃更大:
const JUMP_STRENGTH = 100;
const GRAVITY = 10;
var jumpStrength = 0;
onJumpKeyupPress(){
if(play.canJump)
jumpStrength = JUMP_STRENGTH;
}
gameTick(){
if(!player.onGround)
{
if(jumpKeyIsStillPressed){
player.up.velocity += jumpStrength;
jumpStrength /= 2;
}
player.up.velocity -= GRAVITY;
player.y += player.up.velocity;
}
else{
player.up.velocity = 0;
jumpStrength = 0;
}
}
在此示例中,用户的初始跳跃强度为 100,每次跳动减半,直到重力变强。
希望我说得够清楚了!