- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我制作了一个 spritesheet 并对其进行了编码,以便它可以运行它。如何使绘图函数每秒运行一次。当我让它运行更新函数中的函数时,它使 Sprite 表变得 super 快,因为这就是世界更新的速度,但我希望它以不同的速度更新。
(function () {
var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
window.requestAnimationFrame = requestAnimationFrame;
})();
var canvas = document.getElementById("canvas"),
ctx = canvas.getContext("2d"),
width = 1000,
height = 200,
player = {
x: width / 2,
y: height - 15,
width: 48,
height: 64,
speed: 3,
velX: 0,
velY: 0,
jumping: false,
grounded: false,
count: 0,
img: new Image()
},
keys = [],
friction = 0.8,
gravity = 0.3;
player.img.src = "img/playersheet.png";
player.img.onload = draw;
var sprX;
var sprY;
setTimeout(draw, 3000);
var boxes = [];
// dimensions
boxes.push({
x: 0,
y: 0,
width: 10,
height: height
});
boxes.push({
x: 0,
y: height - 2,
width: width,
height: 50
});
boxes.push({
x: width - 10,
y: 0,
width: 50,
height: height
});
boxes.push({
x: 120,
y: 100,
width: 80,
height: 80
});
boxes.push({
x: 250,
y: 150,
width: 80,
height: 80
});
boxes.push({
x: 400,
y: 180,
width: 80,
height: 80
});
boxes.push({
x: 270,
y: 150,
width: 40,
height: 40
});
canvas.width = width;
canvas.height = height;
function draw() {
requestAnimationFrame(draw);
sprX = (player.count % 3) * 171;
sprY = Math.floor(player.count / 9) * 351;
ctx.drawImage(player.img, sprX, sprY, 171, 351, 50, 50, 32, 32);
if(player.count == 2)
player.count = 0;
else
player.count++;
}
function update() {
// check keys
if (keys[87] || keys[32]) {
// up arrow or space
if (!player.jumping && player.grounded) {
player.jumping = true;
player.grounded = false;
player.velY = -player.speed * 2;
}
}
if (keys[68]) {
// right arrow
if (player.velX < player.speed) {
player.velX++;
//player.img.src = "img/player_r.png";
}
}
if (keys[65]) {
// left arrow
if (player.velX > -player.speed) {
player.velX--;
//player.img.src = "img/player.png";
}
}
if (keys[83]) {
// down arrow
//player.img.src = "img/player_crouch.png";
}
player.velX *= friction;
player.velY += gravity;
ctx.clearRect(0, 0, width, height);
ctx.fillStyle = "black";
ctx.beginPath();
player.grounded = false;
for (var i = 0; i < boxes.length; i++) {
ctx.rect(boxes[i].x, boxes[i].y, boxes[i].width, boxes[i].height);
var dir = colCheck(player, boxes[i]);
if (dir === "l" || dir === "r") {
player.velX = 0;
player.jumping = false;
} else if (dir === "b") {
player.grounded = true;
player.jumping = false;
} else if (dir === "t") {
player.velY *= -1;
}
}
if(player.grounded){
player.velY = 0;
}
player.x += player.velX;
player.y += player.velY;
ctx.fill();
requestAnimationFrame(update);
}
function colCheck(shapeA, shapeB) {
// get the vectors to check against
var vX = (shapeA.x + (shapeA.width / 2)) - (shapeB.x + (shapeB.width / 2)),
vY = (shapeA.y + (shapeA.height / 2)) - (shapeB.y + (shapeB.height / 2)),
// add the half widths and half heights of the objects
hWidths = (shapeA.width / 2) + (shapeB.width / 2),
hHeights = (shapeA.height / 2) + (shapeB.height / 2),
colDir = null;
// if the x and y vector are less than the half width or half height, they we must be inside the object, causing a collision
if (Math.abs(vX) < hWidths && Math.abs(vY) < hHeights) {
// figures out on which side we are colliding (top, bottom, left, or right)
var oX = hWidths - Math.abs(vX),
oY = hHeights - Math.abs(vY);
if (oX >= oY) {
if (vY > 0) {
colDir = "t";
shapeA.y += oY;
} else {
colDir = "b";
shapeA.y -= oY;
}
} else {
if (vX > 0) {
colDir = "l";
shapeA.x += oX;
} else {
colDir = "r";
shapeA.x -= oX;
}
}
}
return colDir;
}
document.body.addEventListener("keydown", function (e) {
keys[e.keyCode] = true;
});
document.body.addEventListener("keyup", function (e) {
keys[e.keyCode] = false;
});
window.addEventListener("load", function () {
update();
});
<head>
<title>Platformer Game</title>
</head>
<body>
<h3>Arrow keys to move, and space to jump</h3>
<canvas id="canvas"></canvas>
<style>
canvas {
border:1px solid #d3d3d3;
background-color: #f1f1f1;
}
</style>
</body>
最佳答案
如果我理解正确的话,您的 draw
函数只是循环遍历您的 3x3 Sprite 并不断绘制。您可以尝试的一种方法是设置自己的帧速率 (fps) 并检查与开始时间的偏移:
var fps = 24;
var msPerFrame = 1000 / fps;
var startTime;
function draw() {
if (!startTime) startTime = Date.now();
var elapsedTime = Date.now() - startTime;
var spriteIndex = Math.floor(elapsedTime / msPerFrame) % 9;
requestAnimationFrame(draw);
sprX = (spriteIndex % 3) * 171;
sprY = Math.floor(spriteIndex / 9) * 351;
ctx.drawImage(player.img, sprX, sprY, 171, 351, 50, 50, 32, 32);
}
我做了一个工作 fiddle here使用 4x4 的 Sprite 表并尝试重用大量代码,以便您可以看到发生了什么。
关于javascript - Sprite 表上的计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40644613/
我在将 Firebase 调用函数的返回值分配给全局变量时遇到问题。这是我的功能: function getThePushNameById( path , id ){ path.once(
我正在尝试使用 javascript 创建倒计时。我从 here 得到了一些代码并略作修改。 var c=10, t; function timedCount() { document.getE
我想使用java脚本计算点击两个按钮之间的时间差 最佳答案 这里有一个提示 - 使用: var t = Date.now(); 以毫秒分辨率获取当前时间(自 1970 年 1 月 1 日 00:00:
我试图在单击按钮时显示时钟,但并调用设置超时事件来连续显示当前时间。但每当我单击按钮时,它只会鞋一次。如果我在一段时间后单击,那么它也可以工作,但不会显示连续变化的时间。 脚本 function sh
我有一种情况,在模型的 afterSave 中回调,我正在尝试从远程关联访问数据(这是一个具有非常不稳定的关联链接的遗留数据模型)。我发现的是,在回调中我可以对模型执行查找调用,但如果我此时退出,则记
我这里有这段代码,它从数据库中获取一些运动统计数据,并对其进行更新 - 我正试图在检测到变化时发出警报(#scorealert div)。如果只有一个分数发生了变化,这就可以正常工作。当有多个更改时,
我正在制作一个测验应用程序,我想在其中显示用户在玩游戏时所用的时间。它应该采用 HH:MM:SS 格式,从 00:00:00 开始,直到他选择答案。当用户每秒播放时,计时器应该每秒更新一次。另外,我想
我想知道在 Postgres 中执行查询所需的时间,我看到很多建议使用\timing 的响应,但我是 Postgres 的新手,我不知道如何使用它,谁能帮忙 提前谢谢你 最佳答案 您只能将 \timi
作为我大学论文的一部分,我试图测试几种不同类型的数字输入键盘界面的可用性。为此,我需要记录参与者按下的每个按键以及每个按键的时间。仅仅记录他们在表单上提交的内容是不够的,因为我需要监控他们看到并纠正了
我需要在我的网站上制作一些简短的“新闻快讯”...淡入和淡出一些 `s 并需要它们循环... 到目前为止我有这样的东西: $('.text01').hide().fadeIn('slow').dela
我希望有人能帮我解决这个问题。我想测量排序算法。这是我目前的做法: M = 1000 # number of executions N = [1000, 2000, 4000, 16000] # si
我的项目有个小问题:我在我的程序中使用 C++ chrono 库来处理时间。但是当我在调试计时时钟继续运行时到达断点并且当我继续程序时,与计时器一起工作的东西中有疯狂的值。 现在我的问题:是否有库或方
我正在开发一个 C++ 应用程序,它需要精确到毫秒级的详细计时信息。 我们打算使用标准 time() 收集精确到秒的时间在 中发挥作用.我们还想收集自 time() 给出的最后一秒以来经过的毫秒数。
我试图在敲击之间保持节奏。但是,我随机获得巨大的值(value),我不确定为什么。 @implementation GameScene { CFTimeInterval previousFram
我正在通过几个操作解析一个文件,我想测量执行这些操作所花费的时间。 执行此操作并打印时间的最佳方法是什么? 最佳答案 更新:如果您不反对使用外部库并且您使用的是 JDK 5+,Google Guava
我想收集与网络请求的每个阶段所花费的时间相关的统计信息。 httplib 提供: def run(self): conn = httplib.HTTPConnection('www.examp
我正在制作一款 Android 游戏,其中一些图形元素移动速度很快。我打算使用 Canvas,但担心 onDraw 方法会以不规则的间隔调用,从而使快速元素以不规则的速度移动。有没有办法确保定期调用
我正在制作一个按时间间隔执行的应用程序。更新之间我可以等待的绝对最长时间是30秒,介于0和0之间的任何值都是可以接受的,但是我希望15秒是一个很好的衡量标准。但是,它并不像听起来那样容易。我尝试了4种
我的桌面应用程序有一个恼人的问题,我无法弄清楚。我已将问题隔离到以下示例中。我正在尝试做什么...... 我有一个jframe和5个jpanels,每个面板都有不同的图片。启动后,用户将看到面板 A,
是否有一种标准的方法来记录 Quartz 执行任务所花费的时间?我也对基于 Spring 的解决方案持开放态度,因为我正在使用两者。 最佳答案 您可以使用通用计时库,例如 ERMA .它与 sprin
我是一名优秀的程序员,十分优秀!