views:

26

answers:

0

I'm getting random flickers from the following animation -- it looks like the picture is centering itself for about 1 frame or so before continuing with what it's supposed to be doing, and I don't know why.

-(void)generateWander{
//NSLog(@"generateWander");

if(targetChange==0)
{
    targetChange=TARGET_LENGTH+1;
    float destinationAngle=((float)random()/RAND_MAX)*2*M_PI;
    NSLog(@"new destination:  %f",destinationAngle);
    targetY=sin(destinationAngle)*TARGET_RADIUS;
    targetX=cos(destinationAngle)*TARGET_RADIUS;
    NSLog(@"new target is: %f %f",targetX,targetY);
}
targetChange--;
float newVectorX=(targetX-currentX)*VECTOR_SCALAR;
float newVectorY=(targetY-currentY)*VECTOR_SCALAR;
vectorX+=newVectorX;
vectorY+=newVectorY;
if (pow((vectorX*vectorX+vectorY*vectorY),.5f)>MAX_SPEED) {
    vectorX*=.5;
    vectorY*=.5;
}
float newX=currentX+vectorX;
float newY=currentY+vectorY;
//NSLog(@"New position is: %f %f",newX,newY);
self.wander=[CAKeyframeAnimation 
             animationWithKeyPath:@"transform"];
NSValue *initial = [NSValue valueWithCATransform3D:
                    CATransform3DMakeTranslation(currentX, currentY, 0.0)];
NSValue *final = [NSValue valueWithCATransform3D:
                  CATransform3DMakeTranslation(newX, newY, 0.0)];
self.wander.values = [NSArray arrayWithObjects:initial, final, nil];
wander.removedOnCompletion=NO;
wander.duration = ANIMATION_TIME;
[self.wander setDelegate:self];
[wander setDelegate:self];
[self.icon removeAnimationForKey:@"wander"];
[self.icon addAnimation:self.wander forKey:@"wander"];
currentX=newX;
currentY=newY;
/*currentX=0.0F;
currentY=0.0F;
vectorX=0.0F;
vectorY=0.0F;
targetX=0.0F;
targetY=0.0F;
targetChange=0;*/   
}

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
//NSLog(@"Animation Did Stop was called");
if (theAnimation==[backgroundRays animationForKey:@"rotate"]) {
    //NSLog(@"rotate was detected");
    [backgroundRays removeAnimationForKey:@"rotate"];//May not be necessary, but I think it helps prevent a memory leak
    [self.backgroundRays addAnimation:self.rotate forKey:@"rotate"];
}
else if(theAnimation==[icon animationForKey:@"wander"]){
    [self generateWander];
}
}