- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在用 javascript 制作游戏。现在,当按住跳跃时,我的 Angular 色会一直跳跃。我希望他不要一直跳来跳去。即使保持跳跃,我如何让他停止跳跃并在 2 秒左右后下来?我有重力,我放手后他就下来了..但我希望他自己下来。
播放器
import { detectCollisionLeft } from './collision';
import Map from './map';
import Entity from './entity';
class Player extends Entity {
constructor(x, y) {
super(x, y)
this.width = 24,
this.height = 24,
this.color = "blue",
// this.pos = {
// x: 20, // change to level start later
// y: 20
// }
this.velX = 0;
this.maxVelX = 3;
this.velY = 0;
this.maxVelY = 8;
this.gravity = 3;
this.jumping = false
// this.topLeft = {
// x: this.pos.x,
// y: this.pos.y - this.height
// }
// this.botRight = {
// x: this.pos.x + this.width,
// y: this.pos.y
// }
// this.top = this.pos.y
// this.left = this.pos.x
// this.bot = this.pos.y + this.height;
// this.right = this.pos.x + this.width
}
draw(ctx) {
// console.log('j')
ctx.fillStyle = this.color;
ctx.fillRect(this.pos.x, this.pos.y, this.width, this.height)
}
moveLeft() {
// debugger
// if (this.map.collidingWithMap(this.topLeft)) {
// console.log('hi')
// return
// }
this.velX = -this.maxVelX
}
moveRight() {
this.velX = this.maxVelX
}
jump() {
// if (this.pos.y === 600 - this.height)
if (!this.jumping) {
this.velY = -this.maxVelY
this.jumping = true
}
}
comeDown() {
this.jumping = false
this.velY = this.gravity
}
stop() {
this.velX = 0;
}
update(dt) {
// console.log(this.pos)
if (!dt) return
this.pos.x += this.velX
this.pos.x += this.velX
this.pos.y += this.velY + this.gravity
// if (this.pos.y + this.height > 600) {
// this.pos.y = 600 - this.height
// }
}
}
export default Player;
Controller
import Player from './player1';
import Map from './map';
class Controller {
constructor(player, map, ctx) {
this.player = player;
this.map = map;
this.ctx = ctx
// this.keyboardHandlers = this.keyboardHandlers.bind(this);
// this.keyboardHandlers();
// keyboardHandlers() {
// document.addEventListener("keypress", (e) => {
// if (e.keyCode === 38 || 87) {
// this.player.jumping = false;
// this.player.jump();
// // break;
// }
// })
document.addEventListener("keydown", (e) => {
switch (e.keyCode) {
case 38 && 87: // up arrow
// console.log("w")
if (this.player.jumping === false) {
this.player.jump();
}
break;
// case 40 && 83: // down arrow
// this.player.move("down");
// // console.log(this.player.pos)
// this.player.draw(this.ctx)
// break;
case 37 && 65: // left arrow
this.player.moveLeft();
break;
case 39 && 68: // right arrow
this.player.moveRight();
break;
case 32: // space
// debugger
this.map.flipMap();
// case 38 && 39: // doesnt work
// console.log('upright')
// break;
// case 13: // enter
// if (this.game.menu === true) {
// } else {
// }
default:
break;
}
})
document.addEventListener("keyup", (e) => {
// debugger
switch (e.keyCode) {
case 38 && 87: // up arrow
// console.log("w")
if (this.player.jumping)
this.player.comeDown()
break;
// case 40 && 83: // down arrow
// if (this.player.velY > 0)
// this.player.stop();
// break;
case 37 && 65: // left arrow
if (this.player.velX < 0)
this.player.stop();
break;
case 39 && 68: // right arrow
if (this.player.velX > 0)
this.player.stop();
break;
// case 32: // space
// this.map.flipMap(this.ctx);
// case 38 && 39: // doeswnt work
// console.log('upright')
// break;
// case 13: // enter
// if (this.game.menu === true) {
// } else {
// }
default:
break;
}
})
}
// }
}
export default Controller;
游戏
import Player from './player';
import Map from './map';
import Controller from './controller';
import Tile from './tile'
import { buildLevel, levels } from './level'
import Collision from './collision';
class Game {
constructor(ctx) {
this.ctx = ctx;
// this.map = new Map(ctx);
// // debugger
// this.tile = new Tile()
// this.map = new Map(this.ctx)
this.map = new Map(levels[1].tiles, this, this.color);
this.player = new Player(20, 20);
this.controller = new Controller(this.player, this.map, this.ctx, this);
this.collision = new Collision(this.map)
this.tiles = [];
this.color = "black"
// // this.controller = new Controller(this.player, this.map, ctx)
// // this.keyboardHandlers(ctx)
// this.render(ctx);
};
draw(ctx) {
// debugger
ctx.clearRect(0, 0, 1000, 600)
// debugger
// this.tiles.forEach(tile => tile.draw(ctx))
this.player.draw(ctx)
this.map.tiles.forEach(tile => tile.draw(ctx))
// this.map.render(ctx)
}
start() {
// debugger
// this.tiles = buildLevel(levels[1].tiles, this)
this.map.create(levels[1].tiles, this);
this.lastTime = 0;
// this.controller.keyboardHandlers();
requestAnimationFrame(this.animate.bind(this))
}
animate(time) {
const dt = time - this.lastTime;
this.draw(this.ctx)
// this.tiles.forEach(tile => tile.update())
this.player.update(dt)
this.collision.isColliding(this.player)
// this.lastTime = time;
requestAnimationFrame(this.animate.bind(this))
}
// draw(ctx) {
// console.log('cear')
// ctx.clearRect(0, 0, 1000, 600)
// }
// render(ctx) {
// // debugger
// ctx.clearRect(0, 0, 1000, 600)
// this.map.render(ctx)
// this.player.draw(ctx)
// }
}
const CONSTANTS = {
GRAVITY: 0.4
}
export default Game;
最佳答案
当用户开始跳跃时设置一个超时时间,如果用户还没有释放按键则自动调用comeDown
:
//On key down
case 38 && 87: // up arrow
// console.log("w")
if (this.player.jumping === false) {
this.player.jump();
this._timeout = setTimeout(()=>{
if(this.player.jumping === true)
this.player.comeDown()
},2000)
}
break;
//On key up
case 38 && 87: // up arrow
// console.log("w")
if (this.player.jumping){
clearTimeout(this._timeout)
this.player.comeDown()
}
break;
此外,(与此问题无关),case 38 && 87:
不会按您预期的那样工作:
由于表达式 38 && 87
等于 87(由于 &&
),它不适用于 38。
要写 38 OR 87,你必须写两个折叠的情况:
case 38:
case 87:
//code here
关于javascript - 按住跳跃时如何让我的 Angular 色不飞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58229743/
滚动时,我的移动设备底部的固定元素出现问题。看起来每次滚动时都会重新计算高度,因为移动设备上的文档高度增加了。 我认为原因是地址栏淡出并且文档视口(viewport)变大了。我进行了很多搜索并尝试了不
以下是插入到我自己的哈希表中时进行冲突检测的方法的内部。我正在使用小的测试数字并尝试使我的逻辑正确,变量哈希设置为 0,table.length 为 10。 else {
我正在做一个大元素,之前只用过几次转换,所以我没有太多经验。这次客户对效果有很多要求。 我有一个#item,里面有一个按钮和一个透明的div。将鼠标悬停在透明 div 上的 opacity 更改为 0
我是一个十足的 Java 新手。上周一开始,之前从未用任何语言进行过任何编程。因此,如果我发现简单的事情变得复杂,请耐心等待。 我收到了一个文本文件。如下图: 第一个数据是时间(午夜过后的秒数),第二
在 pygame 中计算重力的最佳方法是什么?我基本上只需要它,所以当玩家按下“向上”时,角色会跳跃。到目前为止,这是我的代码(只是一个带有移动的红色 block 的白色屏幕) import pyga
我用一个简单的渐变扩展了 JComponent 并调整了paintComponent()方法来制作我自己的BottomBar。 然后我将它添加到使用 BorderLayout 的 JFrame 的 S
我使用 Chart.js 渲染折线图。我不想重新调整 x 轴以获得更多样本点。 如果 y 轴值不连续变化,渲染跳跃的最佳方法是什么? 理想情况下,我能够为每个 x 值定义两个 y 值。因此,假设我传入
我有用于左、右、上、下移动的 Sprite 的 KeyEvents。我只是在闲逛,正在考虑另一个我希望 Sprite 跳跃的项目。它不必完全现实,因为我才刚刚开始。我所拥有的是,当按下空格键时,它将导
我运行双显示器设置。 从显示器 1 到显示器 2(或反之亦然)需要大量不必要的鼠标移动。 我的想法是利用一个额外的鼠标按钮(我有两个)并让鼠标从显示器 1 上的 XY 坐标超跳(向星际迷航道歉)到显示
我有一个 slider 可以实时更新其右侧标签上的值。当值从 1 位值变为 2 位值时,我应该怎么做才能防止它“跳跃”?我怎样才能给标签一个固定的位置,这样它就不会改变布局: var range =
我在页面上使用光滑的 slider 。一切都很好,除了一件事:当我拖动幻灯片时,有时图像或文本会弹起,这看起来非常糟糕。我该怎么做才能避免这个问题? 这是我的 code
const int jumpHeight = 10; //Non-Const variables bool running = true
我有一个 bootstrap Accordion 崩溃,它工作得很好,虽然有一件事困扰着我。当正在展开的元素很长并且做一个垂直滚动条时,它也会使整个页面向左小跳,但只要内容不大,一切都是流畅的。 有谁
我开始学习一些 SVG(我想还有 javascript),并且我很难理解为什么这不顺利。 0 移动了少量(大概是 x 轴上水平的“1”),但开始大幅跳跃。这是因为我使用的浏览器(Chrome)刷新/重
我想创建像飞翔的小鸟一样的游戏。我希望玩家在屏幕上连续跳跃。我创建了这段代码,它不像一只飞扬的小鸟跳跃 代码: float jump = 100; // Just example if(Gdx.inp
如下图所示,.hero div 的高度在滚动经过某个点时发生变化(Chrome、Android 6.0.1)。 这是相关的CSS .hero { height: 100%; width
当我将鼠标悬停在 li 上时,我有菜单有点摆动。如何停止摆动。 代码在这里 JSFiddle 最佳答案 摆脱它的最简单方法是像这样修改 css: .inner LI { border: 1px s
我试图让 dino 跳跃,但是当我在 dino 跳跃和下落之间使用 time.sleep(0.1) 时,整个游戏停止0.1秒。 我试过使用 time.sleep,仅此而已,因为我在网上找不到任何其他有
我正在使用 andengine 来实现一个可以使用它在屏幕上拖动的 Sprite 。 所以我想做的是当用户点击屏幕上的任何位置时使 Sprite 跳跃。 或者向上移动然后向下移动。 使用 andeng
我有一个包含 webview 的 viewflipper。在某些情况下(看似随机),webview 的底部会出现抖动/跳跃。这可以持续一秒到几秒之间的任何时间。这是一个视频来说明我在说什么 http:
我是一名优秀的程序员,十分优秀!