Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-11-2011, 05:44 PM   #1 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 2
manuMenzella is on a distinguished road
Question Scrolling Large Background Image

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
manuMenzella is offline   Reply With Quote
Old 07-14-2011, 09:13 PM   #2 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 2
manuMenzella is on a distinguished road
Default

Still couldn't solve this. It seriously is driving me crazy. Do you guys have any idea?
Thanks again.
manuMenzella is offline   Reply With Quote
Reply

Bookmarks

Tags
animation, large, scrolling, smooth, uiimageview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 370
9 members and 361 guests
apatsufas, chemistry, Kirkout, leostc, lzwasyc, MarkC, Sami Gh, SamorodovAlex, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,664
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Leslie80
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:38 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0