Hi everyone, noobie type question here, I could even just use a link that says "read this for that topic" if it's long to explain.
I want to have a 2D simulation, with units moving around the screen. Each unit has the same variables as the other units, yet it will have its own values for the variables that can change over time and are unit specific (i.e., a unit runs into "energy" and the energy variable increases in value for that unit, but no other). From what I've read in "Programming in Objective-C, 2nd edition" I thought it would be ideal to have an object class for these units, and have each unit be an instance of that class. And for units that are defined from the start, it's looking good (nowhere near done programming yet).
However, I wanted the units to be able to "reproduce" (i.e., if the energy variable's value goes above 50, a new unit/object instance is created). Is this possible with each unit being an instance object? I'm asking because when I make a method that returns an object instance (new unit), it looks like I have to define the new instance ahead of time:
This works:
This obviously doesn't:
(error: You are too old to start learning programming and probably drunk)
But I can't have an alloc init for each new unit/instance that may or may not appear, because depending on how long the program runs it could have 5 new units or 50,000 new units.
Thanks in advance for any help
I want to have a 2D simulation, with units moving around the screen. Each unit has the same variables as the other units, yet it will have its own values for the variables that can change over time and are unit specific (i.e., a unit runs into "energy" and the energy variable increases in value for that unit, but no other). From what I've read in "Programming in Objective-C, 2nd edition" I thought it would be ideal to have an object class for these units, and have each unit be an instance of that class. And for units that are defined from the start, it's looking good (nowhere near done programming yet).
However, I wanted the units to be able to "reproduce" (i.e., if the energy variable's value goes above 50, a new unit/object instance is created). Is this possible with each unit being an instance object? I'm asking because when I make a method that returns an object instance (new unit), it looks like I have to define the new instance ahead of time:
This works:
Code:
UnitsClass *newObject = [[UnitsClass alloc] init];
newObject = [oldObject reproduce];
This obviously doesn't:
Code:
newObject = [oldObject reproduce];
But I can't have an alloc init for each new unit/instance that may or may not appear, because depending on how long the program runs it could have 5 new units or 50,000 new units.
Thanks in advance for any help
Last edited by a moderator: