First (pathetic) attempt at dual-weilding classes

Member
Posts: 116
Joined: 2002.04
Post: #16
Quote:i changed all the - (SomeMethod) to + (someMethod) in Troll.h and Troll.m exept -(id)init

You can't run around changing instance methods to class methods and making other random changes to try to get your program to work.

You need to study and understand why things don't work and why you're making changes.

Wade
Quote this message in a reply
Member
Posts: 567
Joined: 2004.07
Post: #17
put this in the interface:

Code:
Troll *troll;

anf this above the interface:
Code:
@class Troll

then, in awakeFromNib or whatever, call
Code:
troll = [[Troll alloc] init];

then, whenever you call an instance (-) method in Troll, call it with
Code:
[troll doSomething];
not
Code:
[Troll doSomething];

in your dealloc method, call a
Code:
[troll release];

that should solve your problems. Wink

It's not magic, it's Ruby.
Quote this message in a reply
Member
Posts: 89
Joined: 2002.04
Post: #18
You may want to read up on the Objective-C Language document. This will explain the differences between a class and instance method. Put simply a class method would be like this:

Code:
[Troll attack]

while an instance method would require another step:

Code:
Troll *t1 = [[Troll alloc] init];
  [t1 attack];

NOTE: You can not call a class method from an instance... The compiler will give you a warning message.

From an object oriented standpoint you may wish to reconsider your Troll class. The Troll class you have created is more like a Trolls or TrollManager class meaning that the class manages X number of trolls. An alternative would be to create a Troll class that contains things like x and y position, health, facing direction, etc... Then create a TrollManager class which will manage a set of Troll objects. Troll Manager could be a Singleton where you would do something like the following:

Code:
TrollManager *trollMgr = [TrollManager getInstance];
   Troll *newTroll = [trollMgr createTrollAtX: 10 Y:30];
   [newTroll setX: 10 Y: 30];

   NSArray *trollList = [trollMgr getTrolls];
   // Here you can iterate through all the Troll objects displaying them at X and Y coordinates

   Troll *troll = [trollMgr getTrollAtIndex: 1];
   Troll *anotherTroll = [trollMgr getTrollAtX: 10 Y:30];
   Troll *deadTroll = [trollMgr killTrollAtIndex: 1];
etc...

In this example, TrollManager would create, retain and manage all the Troll objects for you.
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #19
i got it working sorta... works unexpectedly.

say each level has a varialbe number of trolls on it... in the openLevel method of how should i do Trol *t1, t2 t3 and so on? if there is a variable number of trolls it cant be fixed...

how bout:

Code:
int x;
for (x = 0;x < numOfTrolls ;x ++ )  {
NSString *variable = [NSString stringWithFormat:@"T%d", x];
Troll * variable;
}


that wont work cuz that will try to create, say 5 troll objects called varialbe....
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #20
Blink Blink Blink Blink Blink Blink Blink Blink

When you left the IRC yesterday I thought you were in the right track. I was SO wrong! Blush

Read the docs!!!!
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #21
no the part in the irc i got to work fine... its just the THINGY! they way i was trying to do it was wrong... and its hard to explain.. i explained it bad..nevermind ill figure it out.... WOOT thanks for the help
Quote this message in a reply
Member
Posts: 116
Joined: 2002.04
Post: #22
Rather than that, I'd recommend learning about allocating object dynamically and using arrays to store the references to them.

Wade
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Dual Kinect Cameras and iPiSoft basic mrbones 1 3,609 Nov 23, 2011 03:39 AM
Last Post: vincentroberts
  C++ Interlocking Classes merrill541 1 2,701 Jan 25, 2009 08:43 PM
Last Post: akb825
  Noob: Accessing Structures from Cocoa Classes MikeC 15 7,497 Oct 19, 2007 02:42 PM
Last Post: MikeC
  Trouble With Template Classes in C++ Nick 4 3,286 Nov 21, 2006 10:25 AM
Last Post: DoG
  Trouble with template classes ermitgilsukaru 2 2,744 Aug 11, 2006 02:00 PM
Last Post: ermitgilsukaru