DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: World Interaction

  1. #1
    Join Date
    Jun 2004
    Posts
    3

    World Interaction

    I've recently begun programming my first simulation/RPG game, which is text-based at the moment. I would like advice as to how one creates the class to class interation which is required. In addition, I need help on part of a move method I have recently made.

    public void moveN(Terrain[][] t) {
    if ((Y + 1) <= (t.length - 1)) {
    if (t[X][Y + 1].pathable == true) {
    setXY(X,Y + 1);
    //t.occupy(this.Agent);
    System.out.println("moveN(): success");
    XY();
    }
    else {
    System.out.println("moveN(): failed");
    }
    }
    else {

    }
    }

    I apologize for the code spacing, this forum does't make editing text easy with this narrow box. Anyway, firstly I would like to know a way to use the above method without the Terrain[][] argument (I've seen other peoples methods before and know there is). Secondly, the commented code ( //t.occupy(this.Agent); ) requires referencing to the class it is in, Agent, for an argument of occupy(), but I don't know how to call the current class as argument. Any help would be greatly appreciated.

  2. #2
    Join Date
    May 2004
    Posts
    219
    Make your Terrain[][] a class variable. That is, within the class, but outside all methods. For example:
    Code:
    public class Blah
    {
       private Terrain[][] terrain;
       public Blah()
       {
          terrain = new Terrain[10][10];
       }
       public void moveN()
       {
          // now you can use terrain here
       } 
    }
    Is that what you're looking to do?

    For your other question, is this class of type Agent? If so, you don't need to add .Agent to the end, 'this' by itself references the current instance of the class you're in. Although, I don't really understand what you're trying to do with the t instance, since you have an array of Terrain objects, and you are calling the occupy method on an array? Perhaps you meant to do:
    Code:
    t[X][Y].occupy(this);
    If I'm wrong about something here, explain in more detail what you are trying to accomplish.

    Edit: If you have a calling (ie, main) method that created both the Agent and Terrain[][] objects, then you should have the pathing check there, instead of having to pass this Terrain array into the Agent object all the time.

  3. #3
    Join Date
    Jun 2004
    Posts
    3
    That would explain some of the errors I got, I, as you expected, forgot the [X][Y] argument. Also, the "this" compiled sucessfuly this time, so I'd like to know if that is what "this" by itself does (calls the current class), as seems to be the case.

    My program so far consits of Terrain, Agent, Object, Item, Ability, Driver, and possibly a World class to load into a future GUI (which will overwrite the Driver).

    I am interested in how best to meld one's classes and interfaces to act as a physical world simulation with good speed, funtionality, and efficentcy. At the moment, my Terrain class contains one null Agent variable and a null array of 10 items (all public, for access). 2 methods within the class are responsable for allowing other classes to read what other objects occupy it. occupy() sets the null Agent to the settings of the Agent it is given as a parameter. placeItems() allows up to 10 Item objects to be stored within, in the same basic way as occupy(). Since these are public, they may be seen by other classes and thus utilized. Is there a more real way to do this, to allow optimum capability?

  4. #4
    Join Date
    May 2004
    Posts
    219
    I'm not sure what else you could want. That sounds like it will work fine to me. What exactly bugs you about your current setup that you'd like to improve on?

    And yes, that's how 'this' works. Common use for 'this' is when a class variable name is the same as a local variable name.
    Code:
    public class Blah
    {
    	private int a;
    	private int b;
    
    	public Blah(int a, int b) 
    	{
    		this.a = a;
    		this.b = b;
    
    		// call moo
    		moo();
    		// same exact thing
    		this.moo();
    	}
    	private void moo()
    	{
    	   // stuff
    	}
    }

  5. #5
    Join Date
    Feb 2004
    Posts
    808
    quick note.. theres no point in saying:


    if ( <a boolean> == true)

    because if it's a boolean, its already false or true, you dont need to compare it with true, to see if it is true.. do you get me?


    boolean thisRocks = true;

    if(thisRocks == true)

    if(thisRocks)


    -

    and this is why you should try and name your booleans so they have an english meaning and the name makes a sentence:

    if(myFeetAreTired)
    massage();

    if(workIsNotGoingWell)
    quit();

    etc
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center