개체 배열이 있습니다. 그러나 이전에 추가 한 객체를 삽입하면 이전 객체를 덮어 씁니다. 어떻게 해결할 수 있습니까?왜 내가 자바 스크립트 배열에 같은 요소를 삽입 할 수 없습니다?
플레이어라는 개체가 있습니다. 플레이어에는 onHandWeapon이라는 하나의 배열과 onFieldWeapon이라는 배열이 있습니다. 그것들은 무기 객체의 배열입니다.
function player(lp){
this.lp = lp;
this.onFieldWeapon = new Array();
this.onHandWeapon = new Array();
}
function weapon(id, heart, bullet, src){
this.id = id;
this.heart = heart;
this.bullet = bullet;
this.src = src;
this.location;
this.name;
this.discription;
this.bufferBullet = bullet;
}
I는 onHandWeapon 어레이 세 더미 오브젝트를 설정했다. 그런 다음 그 중 하나를 무작위로 선택하여 onFieldWeapon에 배치하고 임의의 위치를 할당하려고합니다.
function aiCreateWeapon(){
var b = Math.floor(Math.random()*ai.onHandWeapon.length);
$('#console').append(' ' + b + ' ');
var ip = 100;
while($('#'+ip).attr('class') != 'enemyField'){
ip = Math.floor(Math.random()*48);
}
encurrentWeapon = ai.onHandWeapon[b];
var source = encurrentWeapon.src;
var oImg = document.createElement("img");
oImg.setAttribute('src', source);
oImg.setAttribute('height', '60px');
oImg.setAttribute('width', '60px');
$('#'+ip).append(oImg).show('explode','slow');
encurrentWeapon.location = ip;
ai.onFieldWeapon.push(encurrentWeapon);
$('#console').append(' ' + ai.onFieldWeapon[0].location + ' ');
}
aiCreateWeapon은 버튼에 바인딩되는 기능입니다. 그것을 클릭하면 ai.onFieldWeapon [0] .location은 변경 될 때까지 고정 된 위치입니다. 첫 번째 요소와 동일한 개체가 onFieldWeapon 배열에 추가 될 때마다 첫 번째 요소의 데이터를 재정의한다는 것을 확인했습니다.
문제를 이해할 수 있을지 모르겠지만 배열에 encurrentWeapon을 밀어 넣고 항상 0 인덱스의 항목을 콘솔에 씁니다. 아마도 ai.onFieldWeapon [ai.onFieldWeapon.length - 1] 항목에 액세스해야합니다. – Krzysztof
나는 이것을 시도했지만 효과가 없을 것이다. 어쨌든 고마워요 – Newbie
플레이어 개체를 생성하는 코드를 게시 할 수 있습니까? – Krzysztof