基于瓷砖的JavaScript游戏 - 滞后滚动
Tile based javascript game - laggy scroll
我正在用javascript创建一个基于瓷砖的游戏。我绝对是初学者。计划是在我尝试制作这个游戏的同时学习javascript。我在尝试滚动"游戏"时遇到严重的滞后问题
对于实时预览,您可以在此处看到不好的地方:http://iwansfactory.com/tycoon/index.html
我的javascript生成它们,它们的HTML部分看起来像这样: <div class="tiletype100" id="x0y0" style="left: 2151px; top: -540px;"></div>
该 css:
.tiletype2 {
z-index: 0;
position: absolute;
width: 600px;
height: 800px;
background-image: url("http://iwansfactory.com/tycoon/road2.png");
background-repeat: no-repeat;
}
javascript 滚动函数是这样的:
var right = document.documentElement.scrollLeft;
var bottom = document.documentElement.scrollTop;
var rightscrollvalue = 0;
var bottomscrollvalue = 0;
function rightenter() {
rightscrollvalue = 40;
scrollright();
}
function leftenter() {
rightscrollvalue = -40;
scrollright();
}
function rightout() {
rightscrollvalue = 0;
}
function scrollright() {
if (rightscrollvalue != 0) {
right = right + rightscrollvalue;
console.log(right);
window.scrollTo(right, bottom);
setTimeout(function() {
scrollright();
}, 50);
}
}
function bottomenter() {
bottomscrollvalue = 40;
scrollbottom();
}
function topenter() {
bottomscrollvalue = -40;
scrollbottom();
}
function bottomout() {
bottomscrollvalue = 0;
}
function scrollbottom() {
if (bottomscrollvalue != 0) {
bottom = bottom + bottomscrollvalue;
console.log(bottom);
window.scrollTo(right, bottom);
setTimeout(function() {
scrollbottom();
}, 50);
}
}
您的设计使用大部分透明的大型重叠磁贴。这需要大量的CPU能力进行渲染,从而使游戏滞后。
我建议你把你的瓷砖变小,就像它们必须的大小一样大(所以图像的每个边缘都有一个不透明的像素),并对较大的瓷砖使用偏移,以便它们在正确的位置渲染。
相关文章:
- 幻灯片滚动javascript不起作用
- 将视口底部滚动到元素底部
- jQuery Lazy加载动画滚动
- 设置滑块分区上的滚动
- 结合jQuery和jetpack无限滚动
- 单击顶部导航时如何进行向下滚动效果(向下滑动).
- 滚动到容器中的下一个元素-几乎到了
- 只覆盖箭头键滚动事件
- Javascript如何找到滚动事件的来源
- Image赢得't隐藏在滚动jQuery上
- Safari(Mac OS)上的jQuery平滑滚动问题
- 滚动时导航滞后
- 滞后向左滚动Javascript
- 基于瓷砖的JavaScript游戏 - 滞后滚动
- 移动浏览器上的 scrollTop() 滞后/滚动分辨率问题
- fullpage.js滚动条使其在手机浏览器上显得滞后
- 具有视差的网页部分在滚动时会滞后
- 窗口滚动功能滞后
- 滚动是缓慢和滞后的,为什么?
- Chrome 24 vs IE10滚动可怕的滞后