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 Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-02-2010, 11:26 AM   #1 (permalink)
Registered Member
 
swestland's Avatar
 
Join Date: Apr 2010
Location: Leeds UK
Posts: 30
swestland is on a distinguished road
Unhappy problem with NSTimer

Hi

I am writing an Iphone game. It's going well but I am having problems with the game loop. I am trying to use NSTimer to invoke the game loop so many times per second.

I invoke the NSTimer like this in my viewController.m

-(IBAction) createStoryid)sender {

NSLog(@"abpout to make timer");

timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(gameloop) userinfo:nil repeats:YES];

}

In viewController.h I have declared the timer thus:
@interface FieldButtonFunViewController : UIViewController {
IBOutlet UIButton *generateStory;
NSMutableArray *_them;
NSTimer *timer;
}

I have a gameloop that looks like this:

-(void) gameloop {
NSLog(@"in gameloop");
// my other code here
}

and declare gameloop in the .h file thus:

- (void) gameloop;

When I build this I get a warning

/Users/steve/Documents/FieldButtonFun/Classes/FieldButtonFunViewController.m:80: warning: 'NSTimer' may not respond to '+scheduledTimerWithTimeInterval:target:selector:u serinfo:repeats:'

I have been trying to fix it for about 4 hours. No luck. When I run the program things work until the timer is fired then it crashes.

The following appears in the console:

2010-04-02 16:47:22.804 FieldButtonFun[6129:207] *** +[NSTimer scheduledTimerWithTimeInterval:target:selector:use rinfo:]: unrecognized selector sent to class 0x1c844a0
2010-04-02 16:47:22.805 FieldButtonFun[6129:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSTimer scheduledTimerWithTimeInterval:target:selector:use rinfo:]: unrecognized selector sent to class 0x1c844a0'
2010-04-02 16:47:22.806 FieldButtonFun[6129:207] Stack: (
29295707,
2467112201,

Any ideas ... please.

Best
Steve
swestland is offline   Reply With Quote
Old 04-02-2010, 11:31 AM   #2 (permalink)
Obj-C Learner
 
Join Date: Apr 2009
Location: Manchester, UK
Posts: 1,030
ZunePod is on a distinguished road
Send a message via MSN to ZunePod Send a message via Yahoo to ZunePod
Default

I had this one yesterday, I can't remember what I did, but it's something to do with the selector field.
__________________
Will code for food
ZunePod is offline   Reply With Quote
Old 04-02-2010, 11:36 AM   #3 (permalink)
Registered Member
 
swestland's Avatar
 
Join Date: Apr 2010
Location: Leeds UK
Posts: 30
swestland is on a distinguished road
Default

Quote:
Originally Posted by ZunePod View Post
I had this one yesterday, I can't remember what I did, but it's something to do with the selector field.
Yes, the info in the console would suggest that the selector is not working properly..... but how to fix it?
S
swestland is offline   Reply With Quote
Old 04-02-2010, 11:37 AM   #4 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,176
Kalimba is on a distinguished road
Default

The method that you're referencing does not exist in the NSTimer class. Check the reference manual here: Mac Dev Center: NSTimer Class Reference and see if you can figure out what you're doing wrong.
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Old 04-02-2010, 11:38 AM   #5 (permalink)
Obj-C Learner
 
Join Date: Apr 2009
Location: Manchester, UK
Posts: 1,030
ZunePod is on a distinguished road
Send a message via MSN to ZunePod Send a message via Yahoo to ZunePod
Default

It might be the space that you have

Code:
- (void) gameloop
between the ) and the g.
__________________
Will code for food
ZunePod is offline   Reply With Quote
Old 04-02-2010, 11:55 AM   #6 (permalink)
Registered Member
 
swestland's Avatar
 
Join Date: Apr 2010
Location: Leeds UK
Posts: 30
swestland is on a distinguished road
Default

Thanks Kalimba

I can see now that I should declare the method I want to be called as

- (void) gameloopNSTimer*)timer;

because the timer gets passed to the game loop as an argument.

But I still can't get it to work.

Steve
swestland is offline   Reply With Quote
Old 04-02-2010, 12:04 PM   #7 (permalink)
Registered Member
 
swestland's Avatar
 
Join Date: Apr 2010
Location: Leeds UK
Posts: 30
swestland is on a distinguished road
Default

Think I have fixed it.

In addition to making my gameloop take an argument (the timer) I also realised I had mis-spelled userinfo - it should be userInfo thus

timer = [NSTimer scheduledTimerWithTimeInterval: (1.0f / 30.0f) target: self selector:@selector(gameloop userInfo:self repeats:YES];

It now builds without warning. Thanks!!!

I really appreciate the help.
Steve
swestland is offline   Reply With Quote
Old 04-02-2010, 12:09 PM   #8 (permalink)
Obj-C Learner
 
Join Date: Apr 2009
Location: Manchester, UK
Posts: 1,030
ZunePod is on a distinguished road
Send a message via MSN to ZunePod Send a message via Yahoo to ZunePod
Default

Quote:
Originally Posted by swestland View Post
Think I have fixed it.

In addition to making my gameloop take an argument (the timer) I also realised I had mis-spelled userinfo - it should be userInfo thus

timer = [NSTimer scheduledTimerWithTimeInterval: (1.0f / 30.0f) target: self selector:@selector(gameloop userInfo:self repeats:YES];

It now builds without warning. Thanks!!!

I really appreciate the help.
Steve
BTW, you don't have to have

Code:
- (void)gameloop;
in your .h and the method doesn't need to have the NSTimer thing in there.
__________________
Will code for food
ZunePod is offline   Reply With Quote
Old 04-02-2010, 12:13 PM   #9 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,176
Kalimba is on a distinguished road
Default

Quote:
Originally Posted by swestland View Post
Thanks Kalimba

I can see now that I should declare the method I want to be called as

- (void) gameloopNSTimer*)timer;

because the timer gets passed to the game loop as an argument.

But I still can't get it to work.

Steve
Re-read the warning you get when you build, and examine the error you get when you crash.

Quote:
Originally Posted by swestland View Post
When I build this I get a warning

/Users/steve/Documents/FieldButtonFun/Classes/FieldButtonFunViewController.m:80: warning: 'NSTimer' may not respond to '+scheduledTimerWithTimeInterval:target:selector:u serinfo:repeats:'
Quote:
Originally Posted by swestland View Post
2010-04-02 16:47:22.804 FieldButtonFun[6129:207] *** +[NSTimer scheduledTimerWithTimeInterval:target:selector:use rinfo:]: unrecognized selector sent to class 0x1c844a0
2010-04-02 16:47:22.805 FieldButtonFun[6129:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSTimer scheduledTimerWithTimeInterval:target:selector:use rinfo:]: unrecognized selector sent to class 0x1c844a0'
These messages essentially indicate what the problem is. The compiler is saying that your code is trying to do something specific with NSTimer class that, according to its knowledge of the class, does not exist in the class. Later on, when you invoke that method (that the compiler warned you did not exist), the app crashes because it does not recognize the method as you've typed it in your code.

So, re-read the NSTimer docs for the method that you think you're calling, and look closely at your code. Keep in mind that C/Obj-C is a case-sensitive language.

EDIT: I guess it took me a while to type this reply... Glad you figured it out.
__________________
~~ Word Flurry ~~ App Store / Website / Facebook

Last edited by Kalimba; 04-02-2010 at 12:14 PM. Reason: Jeez, I can't believe it took me over 9 minutes to type this reply... :)
Kalimba is offline   Reply With Quote
Reply

Bookmarks

Tags
game loop, nstimer

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: 404
8 members and 396 guests
iOS.Lover, JackReidy, jeroenkeij, Leslie80, Sami Gh, Wikiboo, Yosh_K
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,671
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, JackReidy
Powered by vBadvanced CMPS v3.1.0

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