Animates off but doesn't seem to remove from polynomials


#1

Animation happens but sometimes it removes polynomial that isn’t there.
Also when count down to two it never removes the last polynomial.
but code Looks Like the book I think…

just getting back into it after long break please to excuse stupid mistake.
thanks
Ian

ps. downloaded example code works fine…

[code]- (IBAction)deleteRandomPolynomial:(id)sender
{
NSArray *polynomialLayers = [[self layer] sublayers];

// are there no polynomials to remove?  //
if ([polynomialLayers count] == 0) {
	NSBeep();
	return;
	}

// pick a random layer //
int i = random() % [polynomialLayers count];
CALayer *layerToPull = [polynomialLayers objectAtIndex:i];

NSLog(@"Removing %d Number of layers %d",i,[polynomialLayers count]);

// Choose a point to drag off to //
NSPoint randPoint = [self randomOffViewPosition];

// Create the Animation that will drive the motion offscreen //
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];

// The Animations will let you stuff anything you want into its dictionary. //
// At the end oof the Animation, you will wwant to know which polynomial was Dragged off //

[anim setValue:layerToPull forKey:@"representedPolynomialLayer"];
[anim setFromValue:[NSValue  valueWithPoint:NSMakePoint(MARGIN,MARGIN)]];
[anim setToValue:[NSValue valueWithPoint:randPoint]];
[anim setDuration: 1.0];
CAMediaTimingFunction *f = 
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[anim setTimingFunction:f];

// You need a callback when the animation is done //
[anim setDelegate:self];
[layerToPull addAnimation:anim forKey:@"whatever"];

// Position during the animation is temporary. Without next line, //
// the deleted polynomial flashed before being removed //
[layerToPull setPosition:CGPointMake(randPoint.x, randPoint.y)];

}

  • (void)animatonDidStop:(CAAnimation *)anim finished:(BOOL)flag
    {

    CALayer *layerToPull = [anim valueForKey: @“representedPolynomialLayer”];
    Polynomial *p = [layerToPull delegate];
    [polynomials removeObjectIdenticalTo:p];
    [layerToPull removeFromSuperlayer];
    }[/code]


#2

[quote=“IanDMah”]Animation happens but sometimes it removes polynomial that isn’t there.
Also when count down to two it never removes the last polynomial.
but code Looks Like the book I think…

just getting back into it after long break please to excuse stupid mistake.
thanks
Ian

ps. downloaded example code works fine…
[/quote]

(loooong lines of code snipped)

You can compare your own code with the one that works using various tools, like diff (TextMate comes with a diff bundle) or /Developer/Applications/Utilities/FileMerge.app or via… well I guess your favorite search engine knows some more

I’m pretty sure you’ll find what’s wrong with those tools :slight_smile:

PS: paying attention to compiler warnings can help, too ; I’d turn on the “Build Results Window” (you can do this via Xcode Prefs -> Building -> Build Results Window: -> Open during builds: On Issues)
PPS: any reason why your comments look like “// comment //”? you don’t have to close “//” comments (like you had to with “/* comment */” …)


#3

I think the following method is spelled incorrectly:

  • (void)animatonDidStop:(CAAnimation *)anim finished:(BOOL)flag
    I think “animation” is missing the second ‘i’.

If this was the cause of your problem, the polynomials would just be animating off screen and never actually getting removed from the array. The following line of logging code that you added to your implementation should therefore show the overall count of layers not going down:
NSLog(@“Removing %d Number of layers %d”,i,[polynomialLayers count]);

Hope this helps.


#4

Thanks.

Funny I thought it was an autocorrect but guess I should learn to spell !!!

in terms of the // some comment // about my double ‘//’ I just like the way they look…
find it easier to read.

just finished the book last week … got to say: if it wasn’t for this forum and the older version of it I would still be on chapter 1.

thanks all
Ian