1. kmadhukishore's Avatar
    Hi all,
    I am planning to write a game based on Quartz 2D..
    And following is the code for managing sprites,Of course constructor I will change accordingly for dynamic behavior by passing animation ID and manipulating the data:
    Code:
    #import <UIKit/UIKit.h>
    
    class SpriteObject
    {
    public:
    	SpriteObject(UIView* parentView);
    private:
    	UIImageView* spriteView;
    };
    
    
    #import "SpriteObject.h"
    
    SpriteObject::SpriteObject(UIView* parentView)
    {
    	spriteView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0,100,100)];
    	NSArray* myImageArray = [[NSArray alloc] initWithObjects:
    							 [UIImage imageNamed:@"1.png"],
    							 [UIImage imageNamed:@"2.png"],
    							 [UIImage imageNamed:@"3.png"],
    							 [UIImage imageNamed:@"4.png"],
    							 nil];
    	spriteView.animationImages = myImageArray;
    	spriteView.animationDuration = 2.0;
    	spriteView.contentMode = UIViewContentModeCenter;
    	[parentView addSubview:spriteView];
    	[spriteView startAnimating];
    }
    And in the main view controller class i am doing this:
    Code:
    - (void)viewDidLoad {
        [super viewDidLoad];
         
         //Creating animation	
         new SpriteObject(self.view);
    
         [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
    }
    
    -(void) onTimer
    {
            if(/*Some condition*/)
    {
    [spriteView stopAnimating];
    }
    
    	mySprite.center = CGPointMake(mySprite.center.x+pos.x,mySprite.center.y+pos.y);
    	
    	if(mySprite.center.x > 320 || mySprite.center.x < 0)
    		pos.x = -pos.x;
    	if(mySprite.center.y > 460 || mySprite.center.y < 0)
    		pos.y = -pos.y;
    }
    And I will
    1)Create all Spites in the beginning of the game and I will add them to my View.
    2)And each sprite is a UIImageViews etc.,.

    Is this an efficient way??Or is there another nice way for managing sprites??

    If I am not clear please tell me...

    Thanks in advance....
    Madhu.
    08-28-2009 03:32 AM
  2. Mugunth's Avatar
    I used this technique.. and it worked pretty well...
    Using a GIF is not very efficient on iPhone. Apple recommends only PNGs as the quarz composer is optimized for PNGs...
    09-12-2009 08:43 AM
LINK TO POST COPIED TO CLIPBOARD