Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  problem in array of objects  (Read 1332 times)
0 Members and 1 Guest are viewing this topic.
Offline guseppe

Junior Newbie




Java games rock!


« Posted 2005-04-12 10:45:44 »

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
1  
2  
3  
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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
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;
            }
      }
}
Offline Daire Quinlan

Junior Member





« Reply #1 - Posted 2005-04-12 15:28:47 »

I suspect its probably because of the way that you set your position

1  
2  
3  
4  
public void setPos(int[] _iPos)
{
  iPos = _iPos;
}


do you create a new int[] every time you set the position ?
you're only passing in the reference to the position array from the calling method.  Chances are you're assigning them all to the same array reference and continually changing the values if (as I surmise) you're creating your NPCs in a big loop somewhere in your intialisation/level loading code.

D.
Offline guseppe

Junior Newbie




Java games rock!


« Reply #2 - Posted 2005-04-15 17:24:53 »

oke thanks stupid i didnt think of that earlier  Embarrassed
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (23 views)
2014-09-12 09:08:26

Norakomi (56 views)
2014-09-10 13:57:51

TehJavaDev (69 views)
2014-09-10 06:39:09

Tekkerue (34 views)
2014-09-09 02:24:56

mitcheeb (56 views)
2014-09-08 06:06:29

BurntPizza (40 views)
2014-09-07 01:13:42

Longarmx (26 views)
2014-09-07 01:12:14

Longarmx (32 views)
2014-09-07 01:11:22

Longarmx (31 views)
2014-09-07 01:10:19

mitcheeb (39 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!