나는이 같은 배경 스프라이트에 배치 여러 스프라이트 :적인 Cocos2D 스프라이트 충돌
//my background
CCSprite *bg = [CCSprite spriteWithFile:@"imageName.png"];
[self addchild:bg];
그리고 내가
//this is how i add my items
CCSprite *items = [CCSprite spriteWithFile:@"itemName.png"];
[bg addchild:items];
오 아니라 내 차 스프라이트를 잊어 BG에 내 항목을 추가
//my car
CCSprite *car = [CCSprite spriteWithFile:@"car.png"];
[self addchild:car];
루프를 사용하여 여러 스프라이트를 bg에 추가합니다.
이제 자동차가 내가 bg에 놓은 여러 스프라이트와 충돌했는지 어떻게 알 수 있습니까?
CGRectIntersectsRect를 사용해 보았는데 작동하지 않습니다.
pythagoras 정리 방법을 사용해 보았지만 다시 작동하지 않습니다.
NSMutableArray에 항목 스프라이트를 추가하는 방법이 있지만 작동하지 않습니다.
누구나 내가 시도 할 수있는 방법을 제안 할 수 있습니까?
추가 코드 :
-(void) initializeCarAndItems
{
car = [CCSprite spriteWithFile:@"android.png"];
car.position = ccp(screenSize.width/2, screenSize.height * 0.30);
[self addChild:car z:1];
carRect = [car boundingBox];
}
-(void) initializeMap
{
bg1 = [CCSprite spriteWithFile:@"racingBG.png"];
bg1.anchorPoint = ccp(0, 0);
bg1.position = ccp(0, 0);
[self addChild:bg1 z:-1];
bg2 = [CCSprite spriteWithFile:@"racingBG2.png"];
bg2.anchorPoint = ccp(0,0);
bg2.position = ccp(0, bg1.boundingBox.size.height - 1);
[self addChild:bg2 z:-1];
convertedWidth = (int)bg1.boundingBox.size.width;
convertedHeight = (int)bg1.boundingBox.size.height;
for (y = 0; y < 15; y++)
{
positionX = arc4random()%convertedWidth;
positionY = arc4random()%convertedHeight;
items = [CCSprite spriteWithFile:@"item.png"];
items.position = ccp(positionX, positionY + 300);
[bg1 addChild:items z:100];
[itemsArray addObject:items];
}
for (y = 0; y < 15; y++)
{
positionX = arc4random()%convertedWidth;
positionY = arc4random()%convertedHeight;
items = [CCSprite spriteWithFile:@"item.png"];
items.position = ccp(positionX, positionY);
[bg2 addChild:items z:100];
[itemsArray addObject:items];
}
}
-(void) accelerate
{
bg1.position = ccp(0, bg1.position.y - accelerateNumber);
bg2.position = ccp(0, bg2.position.y - accelerateNumber);
if (bg1.position.y < -bg1.boundingBox.size.height)
{
questionCount++;
bg1.position = ccp(0, bg2.position.y + bg2.boundingBox.size.height - 1);
[self question];
[bg1 removeAllChildrenWithCleanup:YES];
for (y = 0; y < 15; y++)
{
positionY = arc4random()%convertedHeight;
positionX = arc4random()%convertedWidth;
items.position = ccp(positionX, positionY);
items = [CCSprite spriteWithFile:@"item.png"];
[bg1 addChild:items z:100];
[itemsArray addObject:items];
}
}
else if (bg2.position.y < -bg2.boundingBox.size.height)
{
questionCount++;
bg2.position = ccp(0, bg1.position.y + bg1.boundingBox.size.height - 1);
[self question];
[bg2 removeAllChildrenWithCleanup:YES];
for (y = 0; y < 15; y++)
{
positionY = arc4random()%convertedHeight;
positionX = arc4random()%convertedWidth;
items.position = ccp(positionX, positionY);
items = [CCSprite spriteWithFile:@"item.png"];
[bg2 addChild:items z:100];
[itemsArray addObject:items];
}
}
}
-(void) update:(ccTime)deltaTime
{
[self ifEdgeOfScreen];
[self accelerate];
for (CCSprite *itemFromArray in itemsArray)
{
CGRect itemRect = [itemFromArray boundingBox];
if (CGRectIntersectsRect(carRect, itemRect))
{
NSLog(@"Collision!");
}
}
if (leftButton.active == TRUE)
{
[self moveLeftRight:1];
}
else if (rightButton.active == TRUE)
{
[self moveLeftRight:2];
}
}
UPDATE :
그것은 고정 된 것 :
-(void) update:(ccTime)deltaTime
{
car = [car boundingbox];
[self ifEdgeOfScreen];
[self accelerate];
for (CCSprite *itemFromArray in itemsArray)
{
if (CGRectIntersectsRect(carRect, [itemFromArray boundingbox]))
{
NSLog(@"Collision!");
}
}
if (leftButton.active == TRUE)
{
[self moveLeftRight:1];
}
else if (rightButton.active == TRUE)
{
[self moveLeftRight:2];
}
}
당신은 또한 교차로에 대한 코드를 작성할 수 있습니다? 객체에 어떻게 액세스하고 있습니까? 그것이 당신이 충돌을 다루고있는 방법을 보여줄 것이기 때문에 ... :) –
안녕 Nikhil Aneja! 질문에 더 많은 코드를 추가했습니다 :) 미리 감사드립니다! – Yongzheng