2012-09-06 2 views
1

잠시 동안 내 머리를 두드리는 소리가났습니다. 노드가 연결된 목록에서 제거되지 않는 이유를 파악할 수 없습니다. 나는 연결된 각 이미지를 저장하고 링크 된 목록을 가지고있다. 문제는 여전히 렌더링되고 제거되지 않는 것입니다. 내 코드에 문제가 있습니까? 내 코드는 자바 스크립트에서 연결된 목록의 다른 모든 유형에 대해 동일하게 보입니다.자바 스크립트 연결된 목록 삭제 문제

if (current != null) { 
    if (previous) { 
     previous.next = current.next; 
    } 
    if (current.next) { 
     current.next.previous = previous; 
    } 
    if (current == this._head) { // if the first node is removed, reset head to the next node 
     this._head = current.next; 
    } 
    return current; 
} 

add_node 방법 내부 :

var object_action_holder = function() { 
    this.skill_id =  0; 
    this.skill_type = 0; 
    this.image_src = 0; 
    this.x_pos =  0; 
    this.y_pos =  0; 
    this.turn_off =  0; 
    this._head =  null; 
}; 
object_action_holder.prototype = { 

add: function (skill_id , skill_type , image_src , x_pos , y_pos) { 

    var node = { 
     skill_id:skill_id, 
     skill_type:skill_type, 
     image_src:image_src, 
     x_pos:x_pos, 
     y_pos:y_pos, 
     next:null 
    }, 
    current; 

     if (this._head === null) { 
      this._head = node; 
     } else { 
      current = this._head; 

      while (current.next) { 
       current = current.next; 
      } 
      current.next = node; 
     } 

     this.skill_id = skill_id; 
     this.skill_type = skill_type; 
     this.image_src = image_src; 
     this.x_pos = x_pos; 
     this.y_pos = y_pos; 

     }, 

remove_node: function (skill_id) { 
    var current = this._head, previous; 
    if (skill_id != null && current != null) { 
     while (current.skill_id != skill_id) { 
      previous = current; 
      current = current.next; 
     } 

     if (current.skill_id == skill_id) 
      console.log('found the skill_id'); 
     if (current != null) { 
      if (current.next != null) { 
       previous.next = current.next; 
       return current; 
      }else { 
       previous = null; 
       current = null; 
       return current; 
      } 
     } 
    } 
    return null; 
}, 

get_action_holder: function() { 
    var current = this._head; 

    var object_array = []; 
    var i = 0; 
    while (current != null) { 
     object_array[i] = current; 
     i++; 
     current = current.next; 
    } 
    return object_array; 
}, 
} 

이 시도

var action_image = main.action_holder.get_action_holder(); 
     for(var i = 0; i < action_image.length; i++) { 
      main.game_handle.drawImage (action_image[i].image_src , ( action_image[i].x_pos * 16) + main.player_x - (main.game_x_pos * 16) , (action_image[i].y_pos * 16) + main.player_y - (main.game_y_pos * 16)); 
      if (action_image[i].turn_off == true) 
       delete main.action_holder.remove_node(action_image[i].skill_id); 
     } 
+0

왜 당신이 연결하고있는 목록은 기본적으로 배열을 할 수 있습니까? 또한 렌더링으로 무엇을 의미합니까? 귀하의 코드에 DOM 관련 내용이 표시되지 않습니까? 여기에없는 렌더링 함수가 있나요? – Joseph

+0

@JosephtheDreamer 웹 소켓 연결에서 정보를 받고 lls에 데이터를 저장하기 때문에 lls를 사용하고 있습니다. 그런 다음 데이터를 렌더링 한 다음 데이터를 제거합니다. 또한 다른 데이터에 대해 ll을 검사하여 렌더링이 괜찮은지 확인하십시오. 네가 더 좋은 제안이 있다면 나는 모두 귀이다. 나는 js 프로그래밍에 새로운 경험이있다. – User

+0

@JosephtheDreamer 전체 코드가 추가되었습니다. 텍스트의 벽에 유감스럽게 생각합니다! – User

답변

2

렌더링 : 그것은 유용 할 수 있기 때문에 전체 코드를 추가

편집 :

JS에서
if (this._head === null) { 
    this._head = node; 
} else { 
    current = this._head; 

    while (current.next) { 
     current = current.next; 
    } 
    if (current != node) { // avoid circular reference 
     current.next = node; 
     node.previous = current; // set previous of the new node 
    } 
} 

Test

+0

첫 번째 노드를 제거하려고하면 노드가 작동하지 않지만 다른 노드가 추가되면 제대로 삭제됩니다. – User

+0

어떤 오류가 발생합니까? – timidboy

+0

오류가 발생하지 않습니다. – User