Hi There,
I'm fairly new to iPhone development, coming from a background of Flash development, and am struggling with something I can easily achieve with no performance issues at all in Flash.
I am creating multiple instances of a class which loads a UIImageView. The code I use to create the multiple instances of this class is below:
Code:
for (NSInteger i=0; i<20; i++) {
FireBall *fireBall = [FireBall alloc];
fireBall.vx = power;
fireBall.vy = power * 0.75;
fireBall.gravity = 0.7;
fireBall.baseClass = self;
UIImageView *fireballImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Fireball_1.png"]];
fireballImageView.frame = CGRectMake(70, 205, 23, 23);
[self.view addSubview:fireballImageView];
fireBall.fireballImageView = fireballImageView;
[fireBall startFireBall];
}
When I create these an fire them off in one go the app slows dopwn horrendously.
The code within the actual class file itself is below:
FireBall.m:
Code:
#import "FireBall.h"
#import "WizardViewController.h"
@implementation FireBall
@synthesize vx;
@synthesize vy;
@synthesize gravity;
@synthesize moveTimer;
@synthesize fireballImageView;
@synthesize baseClass;
- (void)startFireBall {
moveTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 / 30.0 target:self selector:@selector(moveFireBall:) userInfo:nil repeats:YES];
}
- (void)moveFireBall:(NSTimer *)timer {
vy -= gravity;
fireballImageView.center = CGPointMake(fireballImageView.center.x + vx, fireballImageView.center.y - vy);
if (fireballImageView.center.x > 500) {
[self removeFireBall];
} else if (fireballImageView.center.y > 260) {
[(WizardViewController*)baseClass createExplosion:fireballImageView.center];
[self removeFireBall];
}
}
- (void)removeFireBall {
// remove the image itself
[fireballImageView release];
[fireballImageView removeFromSuperview];
// stop the timer
[(NSTimer*)moveTimer invalidate];
[self release];
}
@end
So I'm passing a reference to my main view controller class (baseClass) through and also a reference to the UIImageView I'm associating with the class file through too. I'm then moving the UIImageView 30 times a second for each of the 20 UIImageViews.
What am I doing to make it so horribly slow. It works fine with one or two of these going, but with 20 it's horrible and jerky.
Thanks for any help anyone can offer. I'm really stuck.
Cheers,
Ian