【发布时间】:2013-05-13 15:17:51
【问题描述】:
我有一个精灵表,其中有四个图像(bat1,bat2,bat3.bat4),在图像中有一个拿着球棒的人,当所有图像在动画中组合时,它看起来就像在打棒球。 下面是我用来添加精灵表的代码。
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"baseball.plist"];
spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"baseball.plist"];
[self addChild:spriteSheet];
background = [CCSprite spriteWithSpriteFrameName:@"bat4.png"];
background.position = ccp(220, 185);
background.tag = 10;
[self addChild:background];
for(int i = 1; i < 5; i++) {
[walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"bat%d.png", i]]];
}
CCAnimation *walkAnim = [CCAnimation animationWithSpriteFrames:walkAnimFrames delay:5.0f];
self.walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim]];
[background runAction:_walkAction];
我正在检测球与球棒的碰撞。通过下面的代码
for (CCSprite *monster in _monsters) {
if (CGRectIntersectsRect(background.boundingBox, monster.boundingBox)) {
if (((background.position.x -5 > monster.position.x + (monster.contentSize.width/2)) && background.position.y > monster.position.y)) {
isCollision = 1;
[monstersToDelete addObject:monster];
}
// [monstersToDelete addObject:monster];
//[self addFishToBoat];
}
}
这里发生的情况是检测到碰撞,但它总是检测到与 bat4 的 rect 的碰撞。因为 bat 正在移动并且所有 bat 的 cgrect 都不同,所以即使 bas 离 bat1 很远,它也会检测到碰撞,因为球的矩形会与 bat4 的矩形相交。
但我希望只有当球与不同的球棒碰撞时才会检测到碰撞,我的意思是当球用 bat1、bat2、bat3、bat4 击中时,只有它会检测到碰撞而不是总是用 bat4 检测
【问题讨论】:
-
缩小bat4的bounding box试试,如果所有bat的bounding box都一样就可以了。
-
如何减少?还有一件事蝙蝠处于不同的角度假设当我轻触屏幕时 bat(bat1) 处于 90 度所以它的矩形会不同但是当我开始触摸然后通过 sprite sheet 另一个蝙蝠 (bat2) 来了bat2 的角度为 60 度,因此它的矩形会有所不同,以此类推,直到 0 度为止。所以所有的rect会有很多差异所以我想检测碰撞会纠正rect
-
CCSprite *bat1Sprite = [CCSprite spriteWithFile:@"bat.jpg" rect:CGRectMake(0, 0, 30, 10)];无论角度是什么,边界框始终保持不变。
-
但我希望它采用与蝙蝠碰撞的蝙蝠边界框
-
所有蝙蝠的图像都一样吗?我对吗?如果它是真的那么你需要改变边界框来获得碰撞,否则它们会在同一个位置重叠,你不能得到特定的碰撞
标签: iphone objective-c ios6 cocos2d-iphone