Sign In/My Account | View Cart  

advertisement

AddThis Social Bookmark Button

Article:
  Animating Graphics in Cocoa, Part 1
Subject:   Animating several balls
Date:   2002-01-15 06:50:16
From:   destanley
Wow! Great. This is exactly the kind of stuff I'm interested in. Now if Mike can only attack video and sound (hint hint).


Anyway, I tweaked the exercises in order to better understand Cocoa animation, had a lot of fun, and hit a wall when trying to animate several balls. I think the question is: how should I attack things in Cocoa from an object-oriented viewpoint. I'm a procedural kind of guy and I'm having problems figuring out exactly when to subclass, what to subclass and where (in a view, in an NSObject, etc), when to make my own classes, and when to use Apple's. I really get all the Model-Controller-View stuff theoretically, but I don't see it really in action here.


It seemed to me the most logical thing would be to make several "ball" objects, each one containing 'dx', 'dy', their NSBezierPath and their own NSAffineTransform. Then an array (mutable just for fun) would hold all those objects and some controller object would allow you to affect their dx and dy values while they looped, drawing themselves so to speak.


Well, the problem is of course that there is only one NSView while there are several objects, and... well you see... I still haven't fully *gotten* Cocoa...


Full Threads Oldest First

Showing messages 1 through 1 of 1.

  • Michael Beam photo Animating several balls
    2002-01-20 11:12:41  Michael Beam | O'Reilly Author [View]

    Yeah, animating several balls is a problem that i've not yet tackled. Your proposed solution is often the one pops into my head when i think about it. Instead of having a controller object alter their values of dx and dy you caould have the custom view take care of that. At each animation step the view could tell each ball in the array (via an enumerator) to move forward a step, and then in the drawRect method you could again enumerate your array of balls and send -draw messages to each ball which would tell them to draw themselves. Something along those lines.

    If you're interested in learning more about how to build objects, when to build them, what they should be, what should be subclassed, etc. you should look into Design Patterns (the book), and more generally design patterns the concept.

    You should approve of the next column :-)