자바 스크립트 타워 방어를 프로그래밍하기 시작했습니다. 지금까지 저는 궤적을 통해 부하들의 움직임을 보았습니다. 하지만 큰 문제가있어 갑자기 2 초 동안 게임이 멈 춥니 다. 나는 가비지 컬렉터가 그 일을하고있는 것 같아요. 어떻게하면이 문제를 해결할 수 있을지에 대한 아이디어가 게임에 요소를 더 많이 추가 할 계획을 세우고 코딩을 계속하기를 원하지 않기 때문에 매우 좋을 것입니다. 이 완벽하게 흐르는!자바 스크립트 게임; 속도를 늦추고 얼리십시오! 그것을 해결하는 방법?
코드는 매우 간단합니다. particuar 시간 값에, 당신은 위쪽 및 왼쪽 위치를 설정하려고하면
<html>
<head>
<style>
#game{
background:red;
width:500px;
height:500px;
position:relative;
}
.mostro {
background:black;
width:15px;
height:15px;
position:absolute;
}
</style>
</head>
<body>
<div id="game">
<script type="text/javascript">
waypoint_x = [40, 140, 140, 220, 220, 80, 80, 340, 340, 420, 420];
waypoint_y = [140, 140, 60, 60, 240, 240, 320, 320, 100, 100, -20];
delay = 25;
new_monster = 0;
monsters_placed = 0;
monsters = [];
var d = new Date();
dist_x = 0;
dist_y = 0;
angle = 0;
mostro="";
total_monsters = 5;
function runGame() {
if (monsters_placed<total_monsters) {
new_monster++;
}
if (new_monster == delay) {
new_monster = 0;
document.getElementById("game").innerHTML = document.getElementById("game").innerHTML + '<div class="mostro" id="mostro-'+monsters_placed+'"></div>';
monsters_placed++;
}
for (i=0;i<monsters_placed;i=i+1) {
mostro = monsters[i];
dist_x = waypoint_x[mostro.point_to_reach] - mostro._x;
dist_y = waypoint_y[mostro.point_to_reach] - mostro._y;
if ((Math.abs(dist_x) + Math.abs(dist_y)) < 1) {
monsters[i].point_to_reach++;
}
angle = Math.atan2(dist_y, dist_x);
mostro._x = mostro._x + mostro.speed * Math.cos(angle);
mostro._y = mostro._y + mostro.speed * Math.sin(angle);
monsters[i]._rotation = angle/Math.PI*180-90
document.getElementById("mostro-"+i).style.left = Math.ceil(mostro._x) + "px";
document.getElementById("mostro-"+i).style.top = Math.ceil(mostro._y) + "px";
}
}
function setUpGame(){
for(i=0;i<=total_monsters;i++){
monsters[i] = new Object();
monsters[i].point_to_reach = 0;
monsters[i].speed = 1;
monsters[i]._x = 0;
monsters[i]._y = 0;
}
}
setUpGame();
setInterval(runGame,10);
</script>
</body>
</html>
간격을 30과 같이 늘리려고 했습니까? – cloudhead
예. 1000 초 간격으로도 발생합니다. – DFectuoso