CCMotionStreak working with CCMoveTo
I'm trying to use the CCMotionStreak to draw a path with a CCSprite.
Init:
CCMotionStreak* streak;
streak = [CCMotionStreak streakWithFade:100 minSeg:1 image:@"streak.png" width:5
length:3 color:ccc4(0, 255, 255, 255)];streak.position = self.theHero.heroSprite.position;
When a touch ends:
-(void) ccTouchEnded:(UITouch*)touch withEvent:(UIEvent *)event
{
[self unschedule:@selector(doStep:)];
CGPoint touchLocation = [touch locationInView: [touch view]];
CGPoint curPosition = [[CCDirector sharedDirector] convertToGL:touchLocation];
[self.theHero.heroSprite stopAllActions];
//cal the duration, speed is set to 85.0f
float du = ccpDistance(self.theHero.heroSprite.position, curPosition) / 85.0f;
[self.theHero.heroSprite runAction:[CCMoveTo actionWithDuration:du
position:curPosition]];
[self schedule:@selector(doStep:)];
}
- (void)doStep:(ccTime)delta
{
//update the position
[streak setPosition:self.t开发者_Python百科heHero.heroSprite.position];
}
When I run the demo, the CCMotionStreak draws a line beautifully the first time I touched the screen, then when the sprite stops, I tried to touch somewhere else on the screen and the CCMotionStreak draws a second line successfully but I noticed that the CCMotionStreak lines "shakes" a little bit(offset a little bit) during the sprite moving and then "shakes back to normal position" when the sprite stops moving.
I hope someone can give me a hint, thanks :)
I suspect it's a timing bug. If the sequence is like the following, your CCMotionStreak will always be a little bit off:
[streak setPosition:self.theHero.heroSprite.position]
-- Streak is at Hero's position[CCMoveTo actionWithDuration:du ...
-- Hero moves- Render, but hero and streak are at different positions
when you stop moving, they equalize.
I would suggest updating the motion streak within the Hero class, or use the scheduler to set priority so the [streak setPosition
call happens after the CCMoveTo
action updates to move the Hero.
Likely, the first move has the priorities correct and it works.
精彩评论