views:

1712

answers:

3

I'd like to animate multiple pieces for a game and i'd like to have those pieces move in different directions so my current method of animating an entire view will no longer work.

I'm wondering what is the best practice for animating multiple items at once. (Note i'm rendering in 2d so i'm not currently interested in 3d soltuions.)

Thanks!

A: 

Did you look at the Cocos2D-iphone project?

I find it very easy to use and takes a lot of work out of your hands.

Cocos2D iPhone

It also does a lot of animation and you can create sequences of animations which you can tie together.

Wim Haanstra
+7  A: 

It's relatively easy to animate multiple 2-D objects at once. There are two ways that you can go about it: animate UIViews or animate CALayers. In my testing, I've found little difference in performance when animating the two (both seem to support ~50 objects moving around at the same time at 60 FPS on the iPhone).

To coordinate the animations of a series of UIViews, you'll want to wrap any changes to the view geometry in a begin / commit animation block:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:ANIMATIONDURATIONINSECONDS];

view1.frame = CGRectMake(newX1, newY1, view1Width, view1Height); 
view2.frame = CGRectMake(newX2, newY2, view2Width, view2Height); 
// Add other views here

[UIView commitAnimations];

This will coordinate the motion of two or more views in a straight line to their new position over a duration of ANIMATIONDURATIONINSECONDS seconds. If you want the views to move independently of one another, create separate animation blocks for each of them.

For more control over your animations, or to create cross-platform Mac / iPhone UI code, you may wish to use Core Animation CALayers. CALayers animate by default, so by setting

layer.position = CGPointMake(newX, newY);

your layer will automatically animate to the new position. Note that by default the layer's position is relative to its center, you can change that to the normal origin position by setting its anchorPoint property to 0,0.

You can coordinate layer animations using a CATransaction:

[CATransaction begin];
[CATransaction setValue:[NSNumber numberWithFloat:ANIMATIONDURATIONINSECONDS] forKey:kCATransactionAnimationDuration];   
layer1.position = CGPointMake(newX1, newY1);
layer2.position = CGPointMake(newX2, newY2);
[CATransaction commit];

For more advanced animations, you can create CABasicAnimations and CAKeyframeAnimations and apply those to your layers to move them along complex paths with specific timing and acceleration / deceleration.

Brad Larson
Thanks Brad, that's very helfpful!
Skyler
A: 

A sample on creating waves 3d using cocos2d for iphone http://cocoabugs.blogspot.com/2010/09/creating-ccwaves3d-animation-for-iphone.html

jeeva