Instancing Woes

Nibbie
Posts: 2
Joined: 2009.04
Post: #1
I'm working on the iPhone to make my first game, and I've hit a brick wall. I've started off very simple, and I've made two buttons, each with their own attributes like attack rating and how far they can move, that sort of thing. And it all works.

I have a class called "Units" that has all the attributes each unit has, then I subclassed that for indvidual unit types like a bishop and knight. I initialize them in the VeiwController and I have an IBAction for all the things that the button can do (which right now is move and attack). The problem with my current setup is that I have to instantiate each unit that I want by hand and make their own UIButton and UIAction.

I would like to be able to not have to hard code in every "Unit" because it seems like a lot of unnessesary code, and it's not flexable. If down the road I want the player to "build" another unit, I couldn't do it.

Any help would be appreciated.
Quote this message in a reply
⌘-R in Chief
Posts: 1,260
Joined: 2002.05
Post: #2
To start off with - creating buttons for the game objects you're describing is out of the norm, but I'll run with it...

-----


Everything you do in IB you can do in code. When you connect an outlet, all you're doing is calling some setBlah: method on an object. When you connect an action, all you're doing is calling setAction: and setTarget:.

Just pretend IB doesn't exist.

If each unit is a button, as you say, just create it in code and add it to an array of units.

Code:
unit = [[Bishop] alloc] initWith...];
[unit setTarget:self];
[unit setAction:@selector(unitClicked:)];
[units addObject:unit];
[[viewController view] addSubview:unit];
[unit setFrame:...];

etc.


Handling which unit was actually clicked, you'd do something like...


Code:
- (void)unitClicked:(id)sender;
{
    Unit * unit = (Unit *)sender;
    ... do something with this unit ...
}



Etc.


Again, I can't say this is the "proper" way to write whatever you're really trying to write, but hopefully this at least helps answer your question.
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2009.04
Post: #3
I was thinking I had to do something with an array of "units". I should have been more clear, though. each unit isn't a UIButton in the code, it's just represented by a UIButton in view. Each unit code wise, is just an instance of a class that has variables for attributes like health and attack.

I do, however, declare the variables for the buttons in code, and then connect them through IB. And each unit has a method associated with it when it's button is clicked. So, for example, I have an instance from my Bishop class called "bishop1", then I make a UIButton variable called "bishop1Button" and then I have a method called "(IBAction) bishop1Clicked:".

Now that I think about it, what I would really want would be to have each unit class have all the necessary buttons and actions directly in it's .h and .m files, so all I would have to do is initialize it in the viewController and it pops up on the screen. I know that throws MVC completely out the window, but do games usually conform to MVC, anyway? ( I honostly don't know)

Thanks for the help!
Quote this message in a reply
Post Reply