【问题标题】:Improve Responsiveness of ccTouchesBegan提高 ccTouchesBegan 的响应能力
【发布时间】:2013-04-07 04:41:22
【问题描述】:

目前,我有一个益智游戏,其中一些对象可以使用 ccTouchesBegan && ccTouchesMoved 方法移动

在 ccTouchesBegan 中调用:

if (CGRectContainsPoint(newBoundingBox, touchLocation))
    {
        //Set the selectedSprite to the sprite with newBoundingBox
    }

然后在 ccTouchesMoved 我做:

CGPoint translation = ccpSub(touchLocation, oldTouchLocation);
newPos = ccpAdd(selSprite.position, translation);
selectedSprite.position = newPos;

现在这一切都很好而且花花公子,但是,我移动的一些对象大约是 140x40px(在 320x480 iPhone 上),有时 ccTouchesBegan 方法不会识别包含 touchLocation 的 boundingBox。

我的问题是,我可以增加对象的边界框的大小,这样如果用户“错过”触摸一点点,它会帮助他们吗?需要您在触摸屏上如此精确地移动障碍物,这有点令人沮丧。

编辑:对于 iPhone:

我如何获得 touchLocation,这是在 ccTouchesBegan 中:

NSSet *touchSet = [event allTouches];
int numberOfTouches = [touchSet count];
CGPoint touchLocation = [self convertTouchToNodeSpace:[touches anyObject]];

allTouches 在触摸开始时自动传递。

【问题讨论】:

  • 哦,最小的精灵是 98x28px,28px 太窄了,不能用手指连续移动
  • 我有点好奇,想知道您是如何获得 touchLocation 的?你能把代码..

标签: iphone ios xcode cocos2d-iphone box2d


【解决方案1】:
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint location = [self convertTouchToNodeSpace: touch];

    for (CCSprite *tempSprite in sprites )
    {
                //Increase the size of bounding box..
        CGRect rect=tempSprite.boundingBox;
                rect.size.height=rect.size.height + 20;
                rect.size.width=rect.size.width + 20;
        if (CGRectContainsPoint(rect, location))
        {
                   //Your stuffs
        }
    }
}

【讨论】:

  • 是的,newBoundingBox 手动设置为围绕中心点的位置 + x 和 y 单位,以解决该问题。
  • 我可以只增加图像之外的 x 和 y,这样如果他们稍微错过了图像,它仍然会捕捉到它吗?这会对屏幕上具有“溢出”boundingBoxes 的多个可移动 Sprite 产生其他影响吗?
猜你喜欢
  • 2016-04-03
  • 2011-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-09
  • 1970-01-01
  • 2019-06-07
相关资源
最近更新 更多