This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorials:drawing_trails [2011/03/22 08:26] – pixelrevision | tutorials:drawing_trails [2013/03/05 10:19] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Drawing Trails ====== | ||
+ | |||
+ | |||
+ | You can use Sparrow' | ||
+ | |||
+ | Try it with particles for some serious fun. | ||
+ | |||
+ | <code objc> | ||
+ | #import < | ||
+ | #import " | ||
+ | |||
+ | @interface SparrowTrails : SPStage{ | ||
+ | CGPoint lastTouch; | ||
+ | CGPoint newTouch; | ||
+ | SPImage *follower; | ||
+ | SPQuad *fadeColor; | ||
+ | SPRenderTexture *fadeTexture; | ||
+ | SPImage *fadeImage; | ||
+ | } | ||
+ | |||
+ | - (void)update: | ||
+ | - (void)touched: | ||
+ | |||
+ | @end | ||
+ | </ | ||
+ | |||
+ | <code objc> | ||
+ | #import " | ||
+ | |||
+ | @implementation SparrowTrails | ||
+ | |||
+ | - (id)initWithWidth: | ||
+ | self = [super initWithWidth: | ||
+ | |||
+ | // create the render texture to draw on | ||
+ | fadeTexture = [[SPRenderTexture alloc] initWithWidth: | ||
+ | [fadeTexture clearWithColor: | ||
+ | fadeImage = [SPImage imageWithTexture: | ||
+ | [self addChild: | ||
+ | |||
+ | // create the object we are going to draw on the texture | ||
+ | follower = [SPImage imageWithContentsOfFile: | ||
+ | [self addChild: | ||
+ | |||
+ | // create something to log our touches | ||
+ | lastTouch = CGPointMake(0, | ||
+ | newTouch = CGPointMake(0, | ||
+ | |||
+ | // this is the fade color we will use to fade out the trail | ||
+ | fadeColor = [[SPQuad alloc] initWithWidth: | ||
+ | fadeColor.color = 0x000000; | ||
+ | fadeColor.alpha = .1; | ||
+ | |||
+ | // add event listeners | ||
+ | [self addEventListener: | ||
+ | [self addEventListener: | ||
+ | |||
+ | return self; | ||
+ | } | ||
+ | |||
+ | - (void)update: | ||
+ | // check to see if we have a new touch and draw the follower | ||
+ | if(newTouch.x != lastTouch.x || newTouch.y != lastTouch.y){ | ||
+ | follower.x = newTouch.x - (follower.width/ | ||
+ | follower.y = newTouch.y - (follower.height/ | ||
+ | [fadeTexture drawObject: | ||
+ | lastTouch = CGPointMake(newTouch.x, | ||
+ | } | ||
+ | // fade out the follower | ||
+ | [fadeTexture drawObject: | ||
+ | } | ||
+ | |||
+ | - (void)touched: | ||
+ | // let's log our touches for the next enterframe event | ||
+ | SPTouch *touchMove = [[event touchesWithTarget: | ||
+ | if(touchMove){ | ||
+ | SPPoint *touchPos = [touchMove locationInSpace: | ||
+ | newTouch = CGPointMake(touchPos.x, | ||
+ | } | ||
+ | } | ||
+ | |||
+ | - (void)dealloc{ | ||
+ | // cleanup | ||
+ | [self removeEventListener: | ||
+ | [self removeEventListener: | ||
+ | [fadeTexture release]; | ||
+ | [self removeChild: | ||
+ | [self removeChild: | ||
+ | [self removeChild: | ||
+ | [fadeColor dealloc]; | ||
+ | [super dealloc]; | ||
+ | } | ||
+ | |||
+ | @end | ||
+ | </ | ||