Game Development Reference
In-Depth Information
NSMutableSet* sorted = [actorClassToActorSet valueForKey:[[actor class] description]];
[sorted addObject:actor];
}
[actorsToBeAdded removeAllObjects];
}
-(void)doRemoveActors{
for (Actor* actor in actorsToBeRemoved){
UIView* view = [[actor representation] getViewForActor:actor In:self];
[view removeFromSuperview];
NSMutableSet* sorted = [actorClassToActorSet valueForKey:[[actor class] description]];
[sorted removeObject:actor];
[actors removeObject:actor];
}
[actorsToBeRemoved removeAllObjects];
doAddActors
iterates through all actors in the
NSMutableSet actorsToBeAdded
and adds
NSMutableSet
actors. Additionally, a
UIView
for each actor is retrieved from the
actorsView
. The property representation
NSObject
that conforms to the protocol
Representation
(as defined in
Actor.h
in Listing 6-9).
NSMutableSet
sorted that corresponds to the actor's class in
NSMutableDictionary actorClassToActorSet
. The actor is then added to the
NSMutableSet
NSMutableDictionary actorClassToActorSet
. The last step in
doAddActors
is to remove all of the
actors from the set
actorsToBeRemoved
.
In the preceding listing, we see the task
doRemoveActors
that parallels the task
doAddActors
. In
doRemoveActors
, each actor in the
NSMutableSet actorsToBeRemoved
has its associated
UIView
removed from its super-view and is removed from the corresponding
NSMutableSet
sorted. Each
actor is also removed from the
NSMutableSet
actors. Finally, the
NSMutableSet actorsToBeRemoved
is
cleared of all actors by calling
removeAllObjects
.
Adding and Removing Actors
To add or remove an actor from the game, we call
addActor
or
removeActor
, respectively, as shown
in Listing 6-6.
Listing 6-6. GameController.m (addActor and removeActor)
-(void)addActor:(Actor*)actor{
[actor setAdded:YES];
[actorsToBeAdded addObject:actor];
}
-(void)removeActor:(Actor*)actor{
[actor setRemoved:YES];
[actorsToBeRemoved addObject:actor];
}