Possible bug with CCMenuItemSprite and scaling

Possible bug with CCMenuItemSprite and scaling
0.0 0



There might be a possible bug with the CCMenuItemSprite, scaling, and how the touch delegate interacts with the CCMenuItemSprite object.

Steps to recreate:
1.) Create the unselected CCSprite using CCSprite::spriteWithSpriteFrameName(NameOfSprite); //This is a 200 by 200 image
2.) Set scale using >setScale; //The image is scaled to a 40x40 image
3.) Create the selected CCSprite using CCSprite::spriteWithSpriteFrameName; //This is the same 200 by 200 image used in step 1 loaded as another CCSprite
4.) Set the scale of the selected sprite using
>setScale( ) //The selected image is scaled to 60x60
5.) Create a CCMenuItemSprite using CCMenuItemSprite::itemFromNormalSprite(UnselectedSprite, SelectedSprite, Selector, Callback);
6.) Add CCMenuItemSprite to a CCMenu object
7.) Add CCMenu object to parent for displaying to screen

The results:
The image is scaled properly on the screen, but when the user clicks on the CCMenuItemSprite created in step 5, the button responds to touch inputs as if it were a 200x200 button instead of the scaled down version.



I think you can not use it like this.
When you call setScale(), sprite’s content size is not changed.
The engine invoke opengl es function scales the sprite every frame when the sprite is drawn.
The engine uses sprite’s content size to determine whether an item is touched.
So it also use 200*200.


Hey Minggo,

You are correct, it can’t be used like this. The solution was to add the unscaled sprites to the CCMenuItemSprite, then add the unscaled CCMenuItemSprite to the CCMenu object, and THEN scale the CCMenu object. Scaling the CCMenu object must also scale the touch delegate coordinates for CCMenu. My initial assumption was that the CCMenuSpriteItem had its own touch delegate that would scale, but this is not the case. However, CCMenu does behave this way.

No bug report needed.



Austin, you are the man. Thanks for documenting that! I know this was around 2.5 years ago but I’m just starting out and I was having exactly the same problem! Your analysis fixed it for me!

Now the problem I’m having is trying to position the menu in the center of the screen. Because of the scaling, it seems to be messing up the positioning. The menu is offset to the right and higher than the center of the screen, probably by the amount of the scaled menu.

Any ideas?


I solved following this suggestion: http://stackoverflow.com/questions/11998612/scaling-ccmenuitemsprite-items-in-a-ccmenu