advertisement

Article:
  Animating Graphics in Cocoa, Part 1
Subject:   Some changes and questions
Date:   2002-01-07 02:30:53
From:   michele
Hello Mike,


I propose some changes to explanation and code, I have some question too.


First of all, we need to change slightly the code for bouncing since as it is built it appends within special conditions that the ball runs out of the view.


In fact, if the ball moves up when the user changes the speed of the ball and the bottom edge of the ball is already under the bottom edge of the view, inverting the translation means the ball goes further down, which is not what we intend to do. And this happens because of the delay.


Furthermore, we need it dramatically when we apply the scale transform.


New code in the first and second projects:


- (void)checkCollision
{
NSRect ballRect = [ball bounds];
NSRect viewRect = [self bounds];


// Bottom edge of the ball is below bottom edge of the view and translation is negative or
// top edge of the ball is above top edge of the view and translation is positive
if (((ballRect.origin.y < viewRect.origin.y) && dy < 0) ||
(((ballRect.origin.y + ballRect.size.height) > viewRect.size.height) && dy > 0))
{
// Inverse the translation
dy = - dy;
[at release];
at = [[NSAffineTransform transform] retain];
[at translateXBy:dx yBy:dy];
}
// left edge of the ball is left to left edge of the view and translation negative or
// right edge of the ball is right to right edge of the view and translation is positive
else if (((ballRect.origin.x < viewRect.origin.x) && dx < 0) ||
(((ballRect.origin.x + ballRect.size.width) > viewRect.size.width) && dx > 0))
{
// Inverse the translation
dx = - dx;


[at release];
at = [[NSAffineTransform transform] retain];
[at translateXBy:dx yBy:dy];
}
}


Change in the third project:


- (void)checkCollision
{
NSRect ballRect = [drawnBall bounds];
NSRect viewRect = [self bounds];


// Bottom edge of the ball is below bottom edge of the view and translation is negative or
// top edge of the ball is above top edge of the view and translation is positive
if (((ballRect.origin.y < viewRect.origin.y) && dy < 0) ||
(((ballRect.origin.y + ballRect.size.height) > viewRect.size.height) && dy > 0))
{
// Inverse the translation
dy = - dy;
}


// left edge of the ball is left to left edge of the view and translation negative or
// right edge of the ball is right to right edge of the view and translation is positive
else if (((ballRect.origin.x < viewRect.origin.x) && dx < 0) ||
(((ballRect.origin.x + ballRect.size.width) > viewRect.size.width) && dx > 0))
{
// Inverse the translation
dx = - dx;
}
}


Then, we need to #import "AnimationView.h" into AnimationController.m in second and third project in order to avoid build warnings.


Third, in the explanation, in Multithreaded Animation part, first paragraph, the code must be:


- (void) animate: (id) anObject
{
....
[self setNeedsDisplay: YES];
...
}


instead of setNeedsDisply: YES], pretty obvious, but when copying...


Fourth, is [NSThread exit] needed at the end of the animate function? It appears in the explanation, Multithreaded Animation part, fourth paragraph, but disappears in code.


Fifth, the animate function is slightly different between second and third project: change in the order of calls into while loop. I wonder if it is important.


In second project:


while (YES)
{
[self stepAnimation:nil];
[self setNeedsDisplay:YES];
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.04]];
}


and in third project:
while (YES)
{
[self stepAnimation:nil];
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.04]];
[self setNeedsDisplay:YES];
}


Seems to me that the second one is better.


I've added some code to dealloc too:


- (void) dealloc
{
[at release];
[st release]; // only in third project
[ball release];
[drawnBall release]; // only in third project


[super dealloc];
}


Hope, I've not omitted some object.


Could you, please, correct me if I'm wrong and go into further details about exiting the thread.


And many thanks for your explanation. I was completely lost at the end of Learning Cocoa book and your tutorial gave me enough energy to go on.


Michèle


1 to 2 of 2
  1. thanks #import "AnimationView.h", other insights
    2002-01-12 21:16:25  psheldon [View]

  2. Michael Beam photo Some changes and questions
    2002-01-09 11:34:51  Michael Beam | O'Reilly Author [View]

    • Some changes and questions
      2002-01-10 13:34:04  michele [View]

1 to 2 of 2