나는 프로그래밍에 익숙하지 않지만 충돌이있는 타일 맵 시스템을 만들었습니다. 나는 최적화에 대한 조언을 찾고 있는데, 아니면 내가하는 일이 완전히 어리 석다면 더 나은 방법을 알려주 길 바란다. 자바 스크립트 타일 맵 충돌 최적화
https://jsfiddle.net/19ed4sm0/
는 기본적으로 나는지도 배열을 통해 루프 기능을 만들고, 이동 키가 다운 될 때마다 호출된다. 이 시스템은 8 방향 이동으로 작동합니다. 즉, 계속 유지하고 싶습니다. 실행 시간을 측정함으로써//Collision with map tiles
function checkMove(px, py, pw, ph, pd) {
for(var y=0; y < map.length; y+=1) {
for(var x=0; x <map[y].length; x+=1) {
var tileX = x * 32;
var tileY = y * 32;
if (map[y][x] === 1) {
if (px < tileX+32 && px + pw > tileX && py < tileY+32 && py + ph > tileY) {
if (pd === 'right' && px+pw > tileX) {
_player.x = tileX - pw - _player.speed;
}
if (pd === 'left' && px < tileX+32) {
_player.x = tileX+32 + _player.speed;
}
if (pd === 'up' && py+ph > tileY) {
_player.y = tileY + ph + _player.speed;
}
if (pd === 'down' && py < tileY+32) {
_player.y = tileY-32 - _player.speed;
}
}
}
}
}
}
function playerInit() {
this.width = 32;
this.height = 32;
this.x = cWidth/2-16;
this.y = cHeight-96;
this.speed = 4;
this.gravity = 6;
this.color = '#ffb5e2'
this.update = function() {
//movement
if (keydown.up === true) {
checkMove(this.x, this.y - this.speed, tileSize, tileSize, 'up');
this.y -= this.speed;
}
if (keydown.left === true) {
checkMove(this.x-this.speed, this.y, tileSize, tileSize, 'left');
this.x -= this.speed;
}
if (keydown.right === true) {
checkMove(this.x+this.speed, this.y, tileSize, tileSize, 'right');
this.x += this.speed;
}
if (keydown.down === true) {
checkMove(this.x, this.y+this.speed, tileSize, tileSize, 'down');
this.y += this.speed;
}
//canvas border collision
if (this.x < 0) {
this.x = 0;
}
if (this.y < 0) {
this.y = 0;
}
if (this.x > cWidth - this.width) {
this.x = cWidth - this.width;
}
if (this.y > cHeight - this.height) {
this.y = cHeight - this.height;
}
}
this.render = function() {
c.fillStyle = this.color;
c.fillRect(this.x, this.y, this.width, this.height);
}
}
지도 크기를 늘리는 등이 게임을 확장하기 시작한 경우 좋은 성능을 유지하는 가장 좋은 방법은 배열의 화면 크기 (캔버스 크기) 버전을 반복하는 것입니까? – stackHD
Calvinxiao 답변을 확인하십시오! –
조숙 한 최적화에주의하십시오. 필요하지 않은 것을 최적화하려고 시도하지 마십시오. 뭔가가 느릴 것이라고 생각한다면 => 그것을 측정하십시오. 불필요한 최적화로 바로 넘어 가지 마십시오. –