Skip to content

Instantly share code, notes, and snippets.

@mohiji
Created December 7, 2012 21:16
Show Gist options
  • Save mohiji/4236599 to your computer and use it in GitHub Desktop.
Save mohiji/4236599 to your computer and use it in GitHub Desktop.
Constructing a SPDisplayObject
/* My character class handles switching animation frames based on time, whether it's
walking, and what direction it's facing. When the time comes to render, it returns
an SPImage set up like this.
*/
@interface Character : NSObject
{
SPImage *_image; // Generic image sprite
NSMutableArray *_frames; // array of texture frames
int _facing; // North, south, east or west
int _frameNumber;
CGPoint _position; // Where in the world is the character
CGSize _size; // width/height to draw at
...
}
@end
@implementation Character
- (SPDisplayObject*)displayObject
{
int frameNumber;
if (_laughing) {
frameNumber = _currentFrame + kLaughIndex;
} else {
frameNumber = _currentFrame + ((int)_facing * 4);
}
_image.texture = (SPTexture*)[_frames objectAtIndex:frameNumber];
_image.x = _position.x;
_image.y = _position.y;
_image.width = _size.width;
_image.height = _size.height;
// I want the characters drawn with the center of the character at their
// x position, so I need to muck with the _image's pivot point.
_image.pivotX = _size.width / 2.0f;
_image.pivotY = _size.height;
return _image;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment