in my game i create an array of objects (one for npc one for static objects) when i load for example 5 npc’s in the array i can draw all of them in a loop like
for(int = i;i<npcarraay.length;i++){
g.drawnpc(npcarray[i].getpos());//getpos is a function from the object npc to retrive the position i use setpos to set the position and the getpos returns an int[]
}
but when i try to fill the array the position from all npc objects in the array have the same position as the last npc i added here is the code for the npc class
import java.io.*;
import java.lang.*;
public class NPC implements Serializable, Runnable{
Thread tProces = new Thread(this);
protected int iLvl;
public boolean death = false;
protected String sName;
protected int[] iPos;
protected Boolean bSolid = true;
public NPC(int _iLvl){
sName = "";
iPos = new int[]{0,0};
iLvl = _iLvl;
tProces.start();
}
public NPC(String _sName, int _iLvl){
sName = _sName;
iPos = new int[]{0,0};
iLvl = _iLvl;
tProces.start();
}
public NPC(int[] _iPos, int _iLvl){
sName = "";
iPos = _iPos;
iLvl = _iLvl;
tProces.start();
}
public NPC(String _sName, int[] _iPos, int _iLvl){
sName = _sName;
iPos = _iPos;
iLvl = _iLvl;
tProces.start();
}
public void run(){
while(true){
try{Thread.sleep(1000);}catch(InterruptedException ie){}
Wander();
}
}
public String getName(){return sName;}
public int[] getPos(){return iPos;}
public Boolean isSolid(){return bSolid;}
public void setName(String _sName){
sName = _sName;
}
public void setPos(int[] _iPos){
iPos = _iPos;
}
public void walkN(){
for(int i = 0; i < 10; i++){
iPos[1] -=1;
try{Thread.sleep(100);}catch(InterruptedException ie){}
}
}
public void walkO(){
for(int i = 0; i < 10; i++){
iPos[0] +=1;
try{Thread.sleep(100);}catch(InterruptedException ie){}
}
}
public void walkZ(){
for(int i = 0; i < 10; i++){
iPos[1] +=1;
try{Thread.sleep(100);}catch(InterruptedException ie){}
}
}
public void walkW(){
for(int i = 0; i < 10; i++){
iPos[0] -=1;
try{Thread.sleep(100);}catch(InterruptedException ie){}
}
}
public void Wander(){
int calc = (int) (Math.random()*4)+1;
switch(calc){
case 1:walkO();
break;
case 2:walkW();
break;
case 3:walkZ();
break;
case 4:walkN();
break;
}
}
}