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 › (frenchBeginner)how to access to multiple instanc
Page Index Toggle Pages: 1
(frenchBeginner)how to access to multiple instanc? (Read 407 times)
(frenchBeginner)how to access to multiple instanc?
Dec 7th, 2007, 2:45pm
 
hello,

(french translation next for french readers - I'm french and I'm not so sure my english is correct !)
------------------------------
(I'm waiting for Casey Reas and Ben Fry ' book I've bought with Amazon some days ago Wink  )

My first real (very) little project is a memory game.

I'd like to create a class for the the deck'cards
Is it possible to access a class like an array or something ?

I mean may I name the instances like this :
card1, card2, card3... cardn or something and access it with something like that :

for f=first to last
somethingToDo(card[f]);
next

(yes, I know it isn't processing/java syntax. It's just to explain what I mean Wink  )

thanks

---------
je commence à utiliser Processing.
(j'attends avec impatience le livre de Casey Reas and Ben Fry que j'ai commandé sur Amazon !)

Mon premier véritable projet est un jeu du type memory.

Comme il ne s'agit pas pour moi seulement de fabriquer un jeu qui fonctionne mais aussi d'apprendre à programmer avec des objets, je voudrais utiliser une classe pour les cartes du memory.

je voudrais que des sous-programmes fassent les traitements mais pour cela il faudrait que je puisse écrire des trucs du genre :

for f=premièreCarte à Dernière carte
traitement de la carte f
next f

(je sais, ce n'est pas la syntaxe de processing Wink  )

Est-ce possible d'utiliser des objets de cette façon c'est à dire d'avoir comme des token dans le nom des instances ?

merci

P.S. J'ai déjà programmé il y a longtemps dans des langages procéduraux et j'aimerais me mettre à la programmation orienté objet.
Re: (frenchBeginner)how to access to multiple inst
Reply #1 - Dec 7th, 2007, 4:00pm
 
hi Eric

of course, you can create arrays of your own objects.

imagine you have a custom Card class :

Code:
class Card {

void doStuff() {
...
}

}


you simply declare your custom array of 52 cards via the syntax :

Code:
Card[] cards = new Card[52]; 



then instantiate each element (card) or your array :

Code:
cards[0] = new Card();
...
cards[51] = new Card();


finally, you can set or get your cards individually via the index number (from 0 to 51) :

Code:
for (int i = 0; i < cards.length; i++) {
cards[i].doStuff();
}


it is probably the simpliest way.

later, depending on your needs, you can use Java lists (ArrayList, etc) or Maps (HashMap, etc) to store/add/remove objects from a collection of (your own) objects.
Re: (frenchBeginner)how to access to multiple inst
Reply #2 - Dec 7th, 2007, 6:06pm
 
okkkkay :-D

thanks
Smiley

P.S. I don't know how to close a subject !
Page Index Toggle Pages: 1