Dynamic Textures

Sparrow lets you create custom textures directly at run-time. However, we didn’t reinvent the wheel for that feature — credit goes to the iPhone SDK, which already contains a powerful API called “Core Graphics”.

You access the drawing API with a special init-method of SPTexture, which takes a “block”-parameter you can fill with your drawing code. If you haven't heard of blocks before, that's because they are a relatively new feature on iOS. They are, however, very simple to use. Just think of a block as a function you can define inline — ActionScript developers are using them all the time.

Here's a sample of how to create a custom texture with Core Graphics in Sparrow:

SPTexture *customTexture = [[SPTexture alloc] initWithWidth:200 height:100
    draw:^(CGContextRef context)
    {
        // draw a string
        CGContextSetGrayFillColor(context, 1.0f, 1.0f);
        NSString *string = @"Hello Core Graphics";
        [string drawAtPoint:CGPointMake(20.0f, 20.0f)
                 withFont:[UIFont fontWithName:@"Arial" size:25]];
    }];

The thing starting with the caret (^) is the block. Place your drawing code in the block and it will appear on your texture later.

If you haven’t worked with Core Graphics before, there are numerous resources on the web that describe how to use them. Here is a good starting point to find out more about them.


Next section: Compiled Sprites

  manual_v1/dynamic_textures.txt · Last modified: 2013/03/05 10:19 (external edit)
 
Powered by DokuWiki