Game Development Reference
In-Depth Information
The goal here is to establish the correct name of the image that should be used to represent
the actor. The final image name will be stored in the variable
imageName
. The first thing we
do is to establish the
baseImageName
. This can be either the
baseImageName
property of the
ImageRepresentation
or from the delegate.
The next step is to get the string representation of the actor's variation. We test to see whether the
delegate responds to the task
getNameForVariant
. If it does, we assign variant to the result. We
follow the same pattern to get a string representation for state.
The last thing we need the delegate for is to establish whether this actor is represented by a single
image or by a sequence of images for this particular state and variation. If the delegate responds
to
getFrameCountForVariation:AndState
:, we record the value in
frameCount
. If the delegate does
not respond to
getFrameCountForVariation:AndState
:, or if
getFrameCountForVariation:AndState
:
returned zero, this indicates that we should not include a frame number in the final image name.
After calling
getFrameCountForVariation:AndState
:, we have all of the information we require to
establish the final name of the image. If the string representation of variant is not nil, we append
“_” and variation to the
NSString imageName
. Similarly, if state is not nil, we append “_” and
state to
imageName
. Finally, if
frameCount
is not zero, we append the
currentFrame
number to the
image name. We don't have to append the file extension to the image name because
UIImage
will automatically find the correct image based on the name. This task will find image files of the
following patterns:
baseImageName.png
baseImageName_variant.png
baseImageName_state.png
baseImageName_variant_state.png
baseImageName_00FN.png
baseImageName_state_00FN.png
baseImageName_variant_00FN.png
Now you know how
ImageRepresentation
and
ImageRepresentationDelegate
are used to specify
which image should be used for a particular actor at a given time. Looking back at Listing 6-8, you
can see that an actor's representation is responsible for creating and updating the
UIView
associated
with the actor.
baseImageName_variant_state_00FN.png
Creating the
UIImageView
for an Actor
The ultimate goal of
ImageRepresentation
is to create a
UIView
that represents the actor. This is
done by instantiating a
UIImageView
that uses the correct image to represent the actor, given its
type, state, and variation. We have reviewed how to find the correct image. All that is left is to create
UIImageView
. This work is done in the task
getViewForActor:In
:, shown in Listing 6-17.