Question

Does anyone know of an easy way to animate a CCMenuItem when the mouse passes over it.

I have read this thread:

Cocos2d CCMenuItem animation upon selection

which seems to cover Cocoa Touch but does not work for me with OS X.

The cocos2d reference lists this method of CCMenuItemImage: which I used this way:

CCMenuItem *beginButtonMenuItem = [CCMenuItemImage itemWithNormalImage:@"BeginButton3.png" 
                                                     selectedImage:@"BeginButtonSel3.png"
                                                            target:self
                                                        selector:@selector(beginButtonPressed:)];

However that menu item does not respond by changing the image when the mouse passes over, only when clicked.

the CCMenuItem reference has this method:

  • (void) selected, stating "The item was selected (not activated), similar to "mouse-over""

but has no sample code ...

Can anyone help implement this?

Thanks

Was it helpful?

Solution

I found it to be quite easy to make the CCMenu change any CCMenuItem to the selected state when the mouse passes over it, giving it a hover effect. First, make sure you've set [window_ setAcceptsMouseMovedEvents:YES];

Then add this function to CCMenu.m (perhaps just after ccMouseDragged definition):

- (BOOL) ccMouseMoved:(NSEvent *)event {
  if( ! _visible || ! _enabled)
    return NO;

  CCMenuItem *currentItem = [self itemForMouseEvent:event];
  if(!currentItem) {
    if(_highlightedItem != _selectedItem) {
      [_highlightedItem unselected];
    }
    [_highlightedItem release];
    _highlightedItem = nil;
    return NO;
  }

  if (currentItem != _highlightedItem) {
    [_highlightedItem unselected];
    _highlightedItem = currentItem;
    [_highlightedItem retain];
    [_highlightedItem selected];
  }

  return YES;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top