We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Dynamic object name creation
Page Index Toggle Pages: 1
Dynamic object name creation (Read 1031 times)
Dynamic object name creation
Apr 6th, 2010, 7:24am
 
Hi!

I have 9 objects: obj1, obj2, obj3, ...., obj9. They all belong to the same class.

If I press 1, I need to execute a method from obj1, if I press2, I need to execute a method from obj2 and so on. The simplest way to do this is to write if (key=='1') obj1...  or with cases.

But can I concatenate the key variable to the end of obj, so I need to write this only once, like objkey... But this doesn't work. Is it possible?

Thanks in advance!
Re: Dynamic object name creation
Reply #1 - Apr 6th, 2010, 7:35am
 
Karlis wrote on Apr 6th, 2010, 7:24am:
But can I concatenate the key variable to the end of obj, so I need to write this only once, like objkey... But this doesn't work. Is it possible


No, not easily (if at all - one of the Java experts can clarify if need be).

The simpler alternative is to store your objects in an array.  Then you simply reference the index of the array...  That's the tried and tested approach Wink
Re: Dynamic object name creation
Reply #2 - Apr 6th, 2010, 8:32am
 
I did a Java course last year and I remember asking my professor this exact question - and I think he said it couldn't be done... or not easily (like Blindfish said).
Re: Dynamic object name creation
Reply #3 - Apr 7th, 2010, 9:50am
 
One way around, make an array, and index it by the key press.

e.g.

Code:
obj[] objects=new obj[9];
obj[0]=new obj(.....etc....);

...

if(keypressed)
{
if(key>='1' && <='9')
{
obj[key-'1'].doSomething();
//characters can be added to and subtracted so '1'-'1' == 0, '3'-'1'=2
// so obj[0] will get it's doSomething(); run if you press '1' etc
}
}


Re: Dynamic object name creation
Reply #4 - Apr 7th, 2010, 10:13am
 
I used a different method in my JumpMan game.
( http://www.openprocessing.org/visuals/?visualID=8637 )
The class contains a char variable and which can be set f.e. during object initialization. The main program then checks the key-variable against the objects-char variable. Done.  Wink

The following code demonstrates this in simple form:
Quote:
ArrayList MyList;
MyObject M;

void setup()
{
  MyList = new ArrayList();  // I like arraylists better than fixed size arrays...
  MyList.add(new MyObject('a'));
  MyList.add(new MyObject('b'));
  MyList.add(new MyObject('c'));
}

void draw(){}  // Nothing drawn here. Just using the console output...

void keyReleased()
  {
    for (int i=0; i<MyList.size();i++)
      {
        M = (MyObject) MyList.get(i);
        if (key==M.HotKey) M.DOSOMETHING();      
      }
  }


class MyObject

  char HotKey;
  MyObject(char HotKin)
  {
    HotKey=HotKin;
  }
  void DOSOMETHING()
  {
    println(" You pressed <"+HotKey+">");
  }
}

Page Index Toggle Pages: 1