Creating a Sprite using UIImageView...

kmadhukishore

New member
Aug 28, 2009
1
0
0
Visit site
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.
 

Mugunth

Member
Sep 30, 2008
18
0
0
mugunthkumar.com
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...
 

Latest posts

Trending Posts

Members online

Forum statistics

Threads
260,328
Messages
1,766,436
Members
441,237
Latest member
INTERNET BUNDLE NOW