Hi. First of all, let me introduce myself. My name is Manuel, I am a college student from Argentina, and I am quite a beginner in iPhone programming. I have been benefiting from this forum for a few weeks, and I think that I am ready to make my first "real" iPhone app. I have come across a problem that I cannot solve. Could you guys take a look at it?
I am building a game which has a vertically scrolling background image. This image is quite large, about 2000 pixels high. If I load it using a simple UIImageView and then move its center around, the animation is far from smooth. So what I did was slice the image and ended up with 8 slices, called
BG2.u@2x.jpg, with u being 1,2, ... , 8. What I am currently doing is loading them up dynamically as I need them (when a slice goes offscreen, I change the center and the image of that UIImageView to reuse it). The problem is that I still can't make it smooth. It looks jerky, and stops for a few instants every few seconds. I know that this can be done on the iPhone because I have seen it on countless other games, but for some reason I can't get it to work. Can you guys take a look at the code that I have come up with?
This is the .h file:
Code:
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface ScrollBG2ViewController : UIViewController {
IBOutlet NSMutableArray *bgArray;
int bgIndex;
}
@property (nonatomic, retain) IBOutlet NSMutableArray *bgArray;
@property int bgIndex;
@end
This is the relevant part of the .m file:
Code:
- (void)viewDidLoad
{
[super viewDidLoad];
if (!self.bgArray) bgArray = [[NSMutableArray alloc] init];
bgIndex = 8;
int numOfSlices = ceil((self.view.bounds.size.height / kBGSliceHeight)) + 2;
for (int i = 1; i <= numOfSlices; i++) {
NSString *bgImageName = [NSString stringWithFormat:@"BG2.%d.jpg", bgIndex];
UIImage *bgImage = [UIImage imageNamed:bgImageName];
UIImageView *bgImageView = [[UIImageView alloc] initWithImage:bgImage];
CGPoint bgFrameOrigin = CGPointMake(0.0f, self.view.bounds.size.height - i * kBGSliceHeight);
CGPoint bgCenter = CGPointMake(bgFrameOrigin.x + bgImageView.frame.size.width/2.0f,
bgFrameOrigin.y + bgImageView.frame.size.height/2.0f);
bgImageView.center = bgCenter;
[self.bgArray addObject:bgImageView];
[self.view addSubview:bgImageView];
[bgImageView release];
bgIndex -= 1;
if (bgIndex < 1) bgIndex = 8;
}
CADisplayLink *aDisplayLink = [[UIScreen mainScreen] displayLinkWithTarget:self selector:@selector(gameLoop)];
[aDisplayLink setFrameInterval:2];
[aDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
- (void)gameLoop
{
for (UIImageView *bgImageView in self.bgArray) {
CGPoint bgImageCenter = CGPointMake(bgImageView.center.x, bgImageView.center.y + 2.0f);
bgImageView.center = bgImageCenter;
if (bgImageCenter.y - kBGSliceHeight/2.0f > self.view.bounds.size.height) {
[self.bgArray removeObject:bgImageView];
UIImageView *bgImageViewLast = [self.bgArray lastObject];
bgImageCenter = CGPointMake(bgImageViewLast.center.x, bgImageViewLast.center.y - kBGSliceHeight);
bgImageView.center = bgImageCenter;
NSString *bgImageName = [NSString stringWithFormat:@"BG2.%d.jpg", bgIndex];
UIImage *bgImage = [UIImage imageNamed:bgImageName];
bgImageView.image = bgImage;
bgIndex -= 1;
if (bgIndex < 1) bgIndex = 8;
[self.bgArray addObject:bgImageView];
}
}
}
I know that you guys are busy with your stuff, and I appreciate you taking your time to take a look at the code and help me out. If you need any other piece of information, just let me know, and I will upload it as soon as possible.
BTW, sorry about my English. Spanish is my first language.
Thanks again for your help,
Manuel