Cocos2d、CCMenuSprite、缩放和定位问题

发布于 2024-11-29 00:09:24 字数 437 浏览 0 评论 0原文

我不知道如何在场景中正确定位一些使用缩放精灵创建的菜单按钮。

如果我创建精灵,缩放它们并设置它们的位置,一切都会顺利。

如果我创建精灵,缩放它们,设置它们的位置,然后使用它们创建 CCMenuItemSprite,它们都会向左和向下移动。

如果我创建精灵,使用它们创建 CCMenuItemSprite,缩放菜单项并设置菜单项位置,它们都会向右和向上移动。

当然,所有这些设置都是使用相同的比例因子和中心点坐标完成的。

我缺少什么?

谢谢!

编辑:解决了!

我明白了!!这完全取决于菜单的坐标系如何工作以及缩放菜单时它如何反应。

我现在不知道我是否对问题采取了不正确的方法(这很有可能),或者整个系统的思考方式是否存在一些棘手的问题。

我会深入研究它并尽快写一个小教程。

再次感谢:D

I can't figure out how to correctly position in the scene some menu buttons created with scaled sprites.

If I create the sprites, scale them, and set their position, it all goes fine.

If I create the sprites, scale them, set their position and then use them to create the CCMenuItemSprite they are all shifted left and down.

If I crete the sprites, use them to create the CCMenuItemSprite, scale the menu item and set the menu items position, they are all shifted right and up.

All those settings are done with the same scale factor and center point coordinates of course.

What am I missing??

Thanks!

EDIT: SOLVED!!

I got it figured out!! It was all due to how the coordinate system of the menu works and how it reacts when the menu is scaled.

I don't know right now it I was taking a non correct approach to the problem, which is very possible, or if there is something tricky in how all the system is thought.

I will dig into it an write a little tutorial ASAP.

Thanks again :D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧情别恋 2024-12-06 00:09:24

我知道这是一个非常古老的问题,但我只是想在这里分享发生在我身上的事情(我希望它有帮助)。我刚刚遇到了这个问题中描述的相同问题,正如@mokagio 上面所说,问题在于菜单的坐标系统如何工作。

首先,我用它创建了一个 Sprite 和一个 MenuItemSprite,但我永远无法正确定位它们。然后我意识到我应该基于 MenuItemSprite 而不是在 Sprite 中进行所有缩放和定位,并且一切正常!

我使用的代码是:

CCPoint backButtonPosition;
CCSprite *backButtonSprite = CCSprite::createWithSpriteFrameName("Back.png");
CCMenuItemSprite *backButton = CCMenuItemSprite::create(backButtonSprite, backButtonSprite, this, menu_selector(MyMenuClass::buttonPressedCallback));

// Do all the scaling and positioning using the CCMenuItemSprite and not the CCSprite
backButton->setScale(0.5);
backButtonPosition = ccp(backButton->boundingBox().size.width / 2, screenSize.height - backButton->boundingBox().size.height / 2);

// Position the button, but first convert the world coordinates to the parent menu local coordinates
backButton->setPosition(categorySelectMenu->convertToNodeSpace(backButtonPosition));
categorySelectMenu->addChild(backButton);

另外,请注意,根据菜单坐标系而不是世界坐标系设置按钮位置很重要。

I know this is a very old question, but I just wanted to share what happened to me here (I hope it is helpful). I just had the same problem described in this question and as @mokagio said above the problem is with how the coodinate system of the menu works.

First I created a Sprite and a MenuItemSprite with it, but I could never position them correctly. Then I realised that I should do all the scaling and positioning based on the MenuItemSprite and not in the Sprite and everything worked fine!

The code I used was:

CCPoint backButtonPosition;
CCSprite *backButtonSprite = CCSprite::createWithSpriteFrameName("Back.png");
CCMenuItemSprite *backButton = CCMenuItemSprite::create(backButtonSprite, backButtonSprite, this, menu_selector(MyMenuClass::buttonPressedCallback));

// Do all the scaling and positioning using the CCMenuItemSprite and not the CCSprite
backButton->setScale(0.5);
backButtonPosition = ccp(backButton->boundingBox().size.width / 2, screenSize.height - backButton->boundingBox().size.height / 2);

// Position the button, but first convert the world coordinates to the parent menu local coordinates
backButton->setPosition(categorySelectMenu->convertToNodeSpace(backButtonPosition));
categorySelectMenu->addChild(backButton);

Also, please note that it is important to set the button position according to the menu coordinate system and not to the world coordinate system.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文