Strange behavior of convertToWorldSpace for MenuItems in CCMenu

170 views Asked by At

I have a menu attached to the layer . In the menu 4 items aligned vertically. I decleare the menu in this way (Warning! This is Javascript. Using COCOS2D-Html5, the framework is the same as per iphone):

        menuHeight = sumofMenuItemsHeight(CategoryMiniSpriteArray);
        var newMenu = cc.Menu.create(CategoryMiniSpriteArray);

        newMenu.setAnchorPoint(cc.PointMake(1,1));
        newMenu.setPosition(cc.PointMake( 
                            ScreenSize.width, ScreenSize.height - (menuHeight / 2)
                            ));
        newMenu.alignItemsVertically();

After that at some point in the program I wanna to get the absolute coordinates of the menuitems relative to the screen. And I execute this code:

var itemPosition = miniSprite.convertToWorldSpace(miniSprite.getPosition());

I get a very strange behavior. The X coordinate returned perfectly matches the real position. If I try to place this way:

sprite.setPosition(itemPosition);

another sprite to that coordinates X is perfectly aligned with the menuitems. The problem is the Y. For every MenuItem I get a shifted Y but that's not all. The difference of Y between the menuitems is 2x times the menuitem height. So not only shifted Y but even shifted between items. What I'm doing wrong ? This is not the WorldSpace.

1

There are 1 answers

0
Claudio Ferraro On

So the problem was that I'm calling convertToWorldSpace in the wrong way:

var itemPosition = miniSprite.convertToWorldSpace(miniSprite.getPosition());

Should be:

var itemPosition = miniSprite.getParent().convertToWorldSpace(miniSprite.getPosition());

I needed to call convertToWorldSpace from the CCMenu. From the parent and not from the child.