Simple Image Animation

Discussion in 'iOS Programming' started by fox10078, Jun 15, 2010.

  1. fox10078 macrumors 6502

    fox10078

    Joined:
    Nov 6, 2009
    #1
    Ok so I'm creating my first app trying to create a simple animation, I thought I've done it all right I get no errors or anything of the sort But the app crashes in the simulator and on the device.

    Heres my code in the .h
    Code:
    @interface BBQViewController : UIViewController {
    	UIImageView *grill;
    
    }
    
    @end
    
    And the .m

    Code:
    
    - (void)viewDidLoad {
    
    	[super viewDidLoad];
    	
    	grill.animationImages = [NSArray arrayWithObjects:
    							 [UIImage imageNamed:@"bbq1.png"],
    							 [UIImage imageNamed:@"bbq2.png"],
    							 [UIImage imageNamed:@"bbq3.png"],nil];
    	grill.animationDuration = 2.00;
    	grill.animationRepeatCount = 0;
    	[grill startAnimating];
    	[self.view addSubview:grill];
    			
    	
    }
    
    Any help would be greatly appreciated.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Where does it crash (if you run it in debug mode you should get the exact line)?
     
  3. fox10078 thread starter macrumors 6502

    fox10078

    Joined:
    Nov 6, 2009
    #3
    The Debugger shows _kill everytime I run it, I hope this is what you meant if not ill continue to search.
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
  5. fox10078 thread starter macrumors 6502

    fox10078

    Joined:
    Nov 6, 2009
    #5
    This is the full line. That it highlights when I run in debugger.
    0x9160f132 <+0010> jae 0x9160f142 <__kill+26>
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    Post the whole debugger output. That line is totally and utterly unhelpful.
     
  7. fox10078 thread starter macrumors 6502

    fox10078

    Joined:
    Nov 6, 2009
    #7
    Code:
    [Session started at 2010-06-15 15:08:16 -0700.]
    GNU gdb 6.3.50-20050815 (Apple version gdb-1469) (Wed May  5 04:36:56 UTC 2010)
    Copyright 2004 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB.  Type "show warranty" for details.
    This GDB was configured as "x86_64-apple-darwin".sharedlibrary apply-load-rules all
    Attaching to process 34179.
    2010-06-15 15:08:18.796 BBQ[34179:207] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIApplication 0x5c094f0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key view.'
    *** Call stack at first throw:
    (
    	0   CoreFoundation                      0x0238c919 __exceptionPreprocess + 185
    	1   libobjc.A.dylib                     0x024da5de objc_exception_throw + 47
    	2   CoreFoundation                      0x0238c851 -[NSException raise] + 17
    	3   Foundation                          0x00028c2b _NSSetUsingKeyValueSetter + 135
    	4   Foundation                          0x00028b99 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 285
    	5   UIKit                               0x004a2d0a -[UIRuntimeOutletConnection connect] + 112
    	6   CoreFoundation                      0x02302b6f -[NSArray makeObjectsPerformSelector:] + 239
    	7   UIKit                               0x004a1721 -[UINib instantiateWithOwner:options:] + 1041
    	8   UIKit                               0x004a34b5 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 168
    	9   UIKit                               0x002b29bb -[UIApplication _loadMainNibFile] + 172
    	10  UIKit                               0x002b390d -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 198
    	11  UIKit                               0x002bd452 -[UIApplication handleEvent:withNewEvent:] + 1958
    	12  UIKit                               0x002b6074 -[UIApplication sendEvent:] + 71
    	13  UIKit                               0x002baac4 _UIApplicationHandleEvent + 7495
    	14  GraphicsServices                    0x02bf2afa PurpleEventCallback + 1578
    	15  CoreFoundation                      0x0236ddc4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    	16  CoreFoundation                      0x022ce737 __CFRunLoopDoSource1 + 215
    	17  CoreFoundation                      0x022cb9c3 __CFRunLoopRun + 979
    	18  CoreFoundation                      0x022cb280 CFRunLoopRunSpecific + 208
    	19  CoreFoundation                      0x022cb1a1 CFRunLoopRunInMode + 97
    	20  UIKit                               0x002b3226 -[UIApplication _run] + 625
    	21  UIKit                               0x002beb58 UIApplicationMain + 1160
    	22  BBQ                                 0x000021d8 main + 102
    	23  BBQ                                 0x00002169 start + 53
    )
    terminate called after throwing an instance of 'NSException'
    Program received signal:  “SIGABRT”.
    (gdb) 
    I Apologize, I haven't used Xcode before.
     
  8. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    So the exception is clearly at the top: "NSUnknownKeyException', reason: '[<UIApplication 0x5c094f0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key view.".

    It seems to crash as it's trying to connect the outlets in a nib/xib. What, exactly, is in the XIB file?
     
  9. fox10078 thread starter macrumors 6502

    fox10078

    Joined:
    Nov 6, 2009
  10. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #10
    And that .h file is exactly what you have? No IBOutlet?
     
  11. fox10078 thread starter macrumors 6502

    fox10078

    Joined:
    Nov 6, 2009
  12. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #12
    I've no idea as I don't know what your intentions are. Without one I don't see how you have connected the variable grill to anything in the xib file so it'll never be initialised to anything and be nil.
     
  13. fox10078 thread starter macrumors 6502

    fox10078

    Joined:
    Nov 6, 2009
    #13
    Well Im trying to cycle through the Images to create a look of a flame. I added the IBoutlet and connect the UIImageView to grill but still receiving the same message in debugger.
     
  14. NickVellios macrumors member

    Joined:
    Jun 6, 2010
    #14
    This may help you:
    http://www.vellios.com/2010/05/31/code-for-animated-sprites/

    You can then simply do the following:

    Code:
    Sprite *flame = [SpriteHelpers setupAnimatedSprite:self.view numFrames:3 withFilePrefix:@"bbq" withDuration:0.4 ofType:@"png" withValue:0];
    
    Change "withDuration:0.4" to the animation duration.
    Sprite inherits from UIImageView so anything you can do with UIImageView you can do with Sprite.
     
  15. fox10078 thread starter macrumors 6502

    fox10078

    Joined:
    Nov 6, 2009
    #15
    Thanks a lot, That last snippet of code you posted, Where would i Implement that?
     
  16. NickVellios macrumors member

    Joined:
    Jun 6, 2010
    #16
    That code will programatically create an animated UIImageView (or in this case, Sprite). You could put it in viewDidLoad.

    Then you can use [flame setCenter:CGPointMake(x,y)]; to set the position of the Sprite.
     
  17. fox10078 thread starter macrumors 6502

    fox10078

    Joined:
    Nov 6, 2009
    #17
    Ok, So if I'm understanding that line, it goes out and finds the files with the bbq prefix that are png format?
     
  18. NickVellios macrumors member

    Joined:
    Jun 6, 2010
    #18
    That is correct. Name them bbq1.png, bbq2.png, bbq3.png ..... and make sure you add the correct framecount.
     
  19. fox10078 thread starter macrumors 6502

    fox10078

    Joined:
    Nov 6, 2009
    #19
    I went through your tutorial and I got these errors

    This was in the spritehelpers.m and .h
    error: expected ')' before 'Sprite'

    This was in the spritehelpers.m
    error: expected ':' before ']' token



    Any ideas?
     
  20. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #20
    Any code?
     
  21. NickVellios macrumors member

    Joined:
    Jun 6, 2010
    #21
    Not enough info there for me to help you. Post where those errors are showing up at in the actual code.
     
  22. fox10078 thread starter macrumors 6502

    fox10078

    Joined:
    Nov 6, 2009
    #22
    Here are the errors for the Spritehelpers.m
    Code:
    #import "SpriteHelpers.h"
    
    
    @implementation SpriteHelpers
    
    [COLOR="Red"]+ (Sprite *) setupAnimatedSprite:(id)sender numFrames:(NSInteger)frames withFilePrefix:(NSString *)ext withValue:(NSInteger)val withCenter:(CGPoint *)center {  [expected ')' before 'Sprite'][/COLOR]
    	NSMutableArray *spriteArray = [[NSMutableArray alloc] initWithCapacity:0];
    
    	for (int i = 1; i < frames +1; i++) {
    [COLOR="Red"]		NSString *spritePath = [[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"%@%d", filePrefix, i] ofType:ext]; ['filePrefix' undeclared][/COLOR]
    		UIImage *sprite = [[UIImage alloc] initWithContentsOfFile: spritePath];
    		
    		[spriteArray addObject:sprite];
    	        [sprite release];
    
    
    
    	}
    	for (int i = frames - 1; i > 1; i--) {
    		NSString *spritePath = [[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"%@%d", filePrefix, i] ofType:ext];
    		UIImage *sprite = [[UIImage alloc] initWithContentsOfFile:spritePath];
    		
    		[spriteArray addObject:sprite];
    		[COLOR="Red"][sprite.release]; [expected ':' before ']' token][/COLOR]
    		
    	}
    
    Heres the .h Errors

    Code:
    
    @interface SpriteHelpers : NSObject { }
    
    [COLOR="Red"]+ (Sprite *)setupAnimatedSprite:(id)sender numFrames:(NSInteger)frames withFilePrefix:(NSString *)filePrefix withDuration:(CGFloat)duration ofType:(NSString *)ext withValue:(NSInteger)val withCenter:(CGPoint *)center; [expected ')' before 'Sprite']
    [/COLOR]
    
    @end
    
     
  23. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #23
    • Did you remember to import Sprite.h into SpriteHelpers.h?
    • You'll need to declare filePrefix
    • sprite has no release property; you want to be calling the release method
     
  24. fox10078 thread starter macrumors 6502

    fox10078

    Joined:
    Nov 6, 2009
    #24
    I'd do that by making another .h file correct?
     
  25. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #25
    No, you wouldn't. Based on this and your other issues, it doesn't seem like you have a grasp of the basics of Objective-C. I'd suggest you step back from the real coding and go (re)learn those.
     

Share This Page