I am working on a Mac application with Xcode (using cocos2d), trying to configure the cursor, and the "set" method does not seem to have any effect in some cases...
I already had a hard time to set my cursor at the launch of the application (NSTimer to set it after the application was really launched), and now I just want it to display another image when the user clicks; I use NSNotification for that, my cursor class receives the notification, then it is supposed to set the new image, then... nothing.
Here is some code which may help:
-(void) click
{
CCLOG(@"Click");
NSString *path = [[NSBundle mainBundle] pathForResource:@"point_pressed" ofType:@"png"];
NSImage *cursorImage = [[[NSImage alloc] initByReferencingFile:path] autorelease];
NSCursor *cursor = [[NSCursor alloc] initWithImage:cursorImage hotSpot:[[NSCursor currentCursor] hotSpot]];
[cursor set];
}
In the initialization:
[NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(updateCursor:) userInfo:nil repeats:NO];
And the method:
-(void) updateCursor:(id)sender
{
CCLOG(@"Update cursor");
[[self.cursorsDict objectForKey:@"point"] set];
}
The "updateCursor" method is also called when the application becomes active, and then it works fine, the correct cursor is displayed.
I have tried many things, pop and push methods, "setOnMouseEnter" (although I don't use rect yet), but no result...
Has anyone a clue about this?
Edit:
Stranger, I wrote the appWake method:
-(void) appWake
{
int i = rand()%3;
if(i==0)
[[self.cursorsDict objectForKey:@"point"] set];
else if(i==1)
[[self.cursorsDict objectForKey:@"point_pressed"] set];
else if(i==2)
[[self.cursorsDict objectForKey:@"open"] set];
self.state = ECursorState_Point;
[NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(appWake) userInfo:nil repeats:NO];
}
Which is called by a notification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWake) name:EVENT_APPLICATION_DID_BECOME_ACTIVE object:nil];
Set in the appDelegate by:
-(void) applicationDidBecomeActive:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] postNotificationName:EVENT_APPLICATION_DID_BECOME_ACTIVE object:nil];
}
And when it's called by this notification, it works fine, the cursor changes randomly; but if I remove the notification in applicationDidBecomeActive and call it somewhere else in my code, then it does not do anything (although I checked that it is called)...