javascript - 游戏循环中一个元素上的两个 jQuery 动画
问题描述
我试图通过单击一个按钮来让一个 html 元素跳转,而另一个动画正在该元素上运行。如果满足条件(如果生命值很重要则闪烁)或单击按钮(如果喂食则跳跃),动画会在游戏循环中触发。
问题:每次循环重复,动画函数都会被再次调用并放入队列;当健康再次恢复正常时,闪烁不会停止。在动画的回调函数中清除队列仅在它是默认队列(“fx”)时才有效,但我需要几个自定义队列。
使用全局布尔变量来允许或拒绝调用动画函数对我来说不是解决方案,因为我希望 jump() 和 flash() 动画被多个 html 元素使用。
我在learn.jquery.com中阅读了有关排队和在元素上启动第二个队列的信息,还阅读了有关动画和排队的jQuery API 文档,但还没有找到解决方案。
查看 flashAnimation1(),这是预期的结果:队列在回调中清除,因此如果触发停止,元素将停止闪烁。但这仅适用于默认(fx)队列。
letJump1(无队列)和 letJump2(自定义队列)让元素不受控制地上升或缩小。
flashAnimation2() 是一个闪烁的混乱。
你能帮我吗?
let requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || windows.msRequestAnimationFrame;
let cancelAnimationFrame = window.cancelAnimationFrame || window.mozCancelAnimationFrame;
let reqAnimF;
let progress = 0;
let lastRender = 0;
let animationOn = false;
const button = document.getElementById("button");
const state = document.getElementById("state");
const jump = document.getElementById("jump");
const block = $("#fadeInAndOut");
const changeStatus = () => animationOn = !animationOn;
const flashAnimation1 = (element) => { $(function() {
element.animate( {opacity: 0}, "fast", "linear" )
.animate( {opacity: 1}, "fast", "linear", ()=> element.clearQueue() );
})
}
const flashAnimation2 = (element) => { $(function() {
element.queue("flash", (next)=> {
element.animate( {opacity: 0}, {
duration: "fast",
easing: "linear",
queue: "flash"
}).animate( {opacity: 1}, {
duration: "fast",
easing: "linear",
queue: "flash",
complete: element.clearQueue()
});
next();
}).dequeue("flash");
});
};
// Jumping: no queue
const letJump1 = (element) => { $(function() {
element.animate( {width: "+=50", height: "+=20"}, {
duration: "fast",
queue: false,
always: () => {
element.animate( {width: "-=50", height: "-=20"}, {
duration: "fast",
queue: false
});
}
}
);
})};
// Jumping queued to another queue
function letJump2(element) {
$(function() {
element.queue("jump", function(next) {
element.animate({width: "+=50", height: "+=20"}, {
duration: "fast",
queue: "jump"
})
.animate( {width: "-=50", height: "-=20"}, {
duration: "fast",
queue: "jump"
});
next();
}).dequeue("jump");
})
};
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
/* function update(progress) {} */
function draw() {
state.innerHTML = animationOn;
if (animationOn) {
flashAnimation2(block);
}
}
function loop(timestamp) {
progress = timestamp - lastRender;
/* update(progress); */
draw();
lastRender = timestamp;
reqAnimF = requestAnimationFrame(loop);
}
button.addEventListener("click", changeStatus);
//jump.addEventListener("click", letJump1(block)); // button is dead for some reason
reqAnimF = requestAnimationFrame(loop);
.center {
position: relative;
margin: auto;
width: 300px;
height: 300px;
text-align: center;
border: 1px solid red;
}
button {
margin: 0 0 20px;
}
#fadeInAndOut {
margin: auto;
width: 100px;
height: 100px;
background-color: black;
}
#jump {
position: absolute;
left: 50%;
bottom: 0;
transform: translateX(-50%);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="center">
<h1 id="state"></h1>
<button type="button" id="button">Flash on/off</button>
<div id="fadeInAndOut"></div>
<button type="button" id="jump" onclick="letJump1(block)">Jump</button>
</div>
解决方案
推荐阅读
- kotlin - kotlin.UninitializedPropertyAccessException:lateinit 属性 salesGST 模型类尚未安装
- c++ - 无法使用 cimg 访问图像中的像素强度(返回 0)
- javascript - 解析 JavaScript 的最小前瞻标记数
- c# - Azure 存储表返回异常 400 错误请求
- grails - Grails 4:数据库迁移插件问题
- laravel - 如何从查询构建器中提取独特的关系?
- node.js - 如何清除 EPERM:安装 gulp 时不允许操作警告
- angular - 如何在将表单发送到服务器之前设置 FormControl 的值
- python - 如何根据我的会员向量用不同的标签绘制我的图表?
- c# - 如何将我的 linux (cntose) 服务器上的 asp.net mvc 网站主机或此网站代码转换为 asp.net 核心