CCMotionStreak works with CCMoveTo

I am trying to use CCMotionStreak to draw a path using 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 the contact 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.theHero.heroSprite.position]; } 

When I start the demo, CCMotionStreak draws a line beautifully for the first time. I touched the screen, and then, when the sprite stopped, I tried to touch another place on the screen, and CCMotionStreak successfully executed the second line, but I noticed that the CCMotionStreak lines are โ€œa little shakenโ€ ( slightly shifted) while moving the sprite, and then โ€œshake to normalโ€ when the sprite stops moving.

I hope someone can give me a hint, thanks :)

+4
source share
1 answer

I suspect this is a time error. If the sequence is similar to the following, your CCMotionStreak will always be off a bit:

  • [streak setPosition:self.theHero.heroSprite.position] - Streak is in Hero position
  • [CCMoveTo actionWithDuration:du ... - The hero moves
  • Render, but the hero and the strip are in different positions.

when you stop moving, they are aligned.

I would suggest updating the lane in the Hero class or using the scheduler to set the priority so that the [streak setPosition occurs after updating CCMoveTo actions to move the hero.

Probably the first step has the right priorities and works.

0
source

All Articles