ios - Cocos2d,CCMenuSprite,缩放和定位问题

标签 ios cocos2d-iphone positioning scale

我无法弄清楚如何在场景中正确定位一些使用缩放 Sprite 创建的菜单按钮。

如果我创建 Sprite 、缩放它们并设置它们的位置,一切都会好起来的。

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

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

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

我错过了什么??

谢谢!

编辑:解决了!!

我想通了!!这完全是由于菜单的坐标系是如何工作的,以及它在缩放菜单时是如何 react 的。

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

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

再次感谢 :D

最佳答案

我知道这是一个非常古老的问题,但我只是想在这里分享发生在我身上的事情(我希望它会有所帮助)。我刚刚遇到了这个问题中描述的同样的问题,正如@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);

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

关于ios - Cocos2d,CCMenuSprite,缩放和定位问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7037296/

相关文章:

ios - Restkit addFetchRequestBlock 崩溃

ios - Instagram 过滤 RGBA 值

ios - Swift UIImage 扩展

iphone - Cocos2D旋转和 anchor

ios - 无法访问 Sprite 的用户数据属性

css - 在 Chrome 上缩放会导致 CSS 定位问题

ios - Swift 从 UIView 层获取 UIImage

jQuery:将 <div> 切换到其原始位置,如 CSS 中的 div ID 所述

CSS:如何使 div block *不*占用其父容器中的空间

ios - Box2d 接触式传感器 COCOS2D