Welcome to the iMore Forums Create Your Account or Ask a Question Answers in 5 minutes - no registration required!
Results 1 to 2 of 2
  1. Thread AuthorThread Author   #1  
    kmadhukishore's Avatar
    iPhone Nanite

    1 Posts

    Default Creating a Sprite using UIImageView...

    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:
    #import <UIKit/UIKit.h>
    class SpriteObject
    	SpriteObject(UIView* parentView);
    	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"],
    	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:
    - (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....
  2. #2  
    Mugunth's Avatar
    iPhone Newbie

    18 Posts

    Thumbs up

    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...

Posting Permissions