目前,我有一个益智游戏,有些物品可以使用ccTouchesBegan&& amp; 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。
我的问题是,我可以增加对象的boundingBox的大小,这样如果用户“错过”那么触摸一下,它会帮助他们吗?这有点令人沮丧,要求你在触摸屏上如此精确地移动障碍物。
编辑:对于iPhonic:
我如何获得touchLocation,这是在ccTouchesBegan:
NSSet *touchSet = [event allTouches];
int numberOfTouches = [touchSet count];
CGPoint touchLocation = [self convertTouchToNodeSpace:[touches anyObject]];
触摸开始时会自动传递allTouches。
答案 0 :(得分:0)
-(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
}
}
}