gpt4 book ai didi

ios - 在 Xcode cocos2d 中添加多个物理对象时遇到问题

转载 作者:行者123 更新时间:2023-11-29 12:40:29 25 4
gpt4 key购买 nike

我是 cocos2d 和 objective-c 的新手。我知道我在这里遗漏了一些东西,但我就是找不到解决方案。希望有人能帮忙....

我的目标是能够点击我放置在屏幕上的任何 Sprite 。首先,我在屏幕上放了 2 个 Sprite 。 (每个 Sprite 都是星形的)。

问题是,只有放在屏幕上的第二个 Sprite 是可点击的。我的猜测是,当我调用 addNewStar 时,它会将 _star 替换为最新的星形 Sprite ,并将之前的 _star 从物理节点中取出。我希望我添加的所有星星都在物理节点中并且可以点击。不知道如何做到这一点。

这是我的代码...希望有人能指出我的错误!

@implementation MainScene {
CCSprite *_star;
CCPhysicsNode *_physicsNode;
CCNode *_ground;
CCLabelTTF *_scoreLabel;
BOOL _gameOver;
CCButton *_restartButton;
}

- (void)didLoadFromCCB {
self.userInteractionEnabled = TRUE;
[self addNewStar];
[self addNewStar];
// set collision txpe
_ground.physicsBody.collisionType = @"level";
// set this class as delegate
_physicsNode.collisionDelegate = self;
}

-(void)addNewStar {

//This successfully loads my star onto the screen
_star = (Star *)[CCBReader load:@"Star"];
_star.physicsBody.collisionGroup = @"starGroup";
_star.physicsBody.collisionType = @"star";
_star.position = ccp(200,300);
[_physicsNode addChild:_star];
}

-(BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair star:(CCNode *)star level:(CCNode *)level {
[self gameOver];
return TRUE;
}

- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {

float ranNum1 = (arc4random_uniform(10000));
float ranNum2 = (arc4random_uniform(10000));
float sideForce = ranNum1 - ranNum2;


if (!_gameOver) {

CGPoint touchLocation = [touch locationInNode:_physicsNode];

if(CGRectContainsPoint([_star boundingBox], touchLocation))
{
[_star.physicsBody applyImpulse:ccp(sideForce, 1000.f)];
[_star.physicsBody applyAngularImpulse:2500.f];

}

}

}

- (void)restart {
CCScene *scene = [CCBReader loadAsScene:@"MainScene"];
[[CCDirector sharedDirector] replaceScene:scene];
}

- (void)gameOver {
if (!_gameOver) {

_gameOver = TRUE;
_restartButton.visible = TRUE;
_star.rotation = 90.f;
_star.physicsBody.allowsRotation = FALSE;
[_star stopAllActions];

CCActionMoveBy *moveBy = [CCActionMoveBy actionWithDuration:0.2f position:ccp(-2, 2)];
CCActionInterval *reverseMovement = [moveBy reverse];
CCActionSequence *shakeSequence = [CCActionSequence actionWithArray:@[moveBy, reverseMovement]];
CCActionEaseBounce *bounce = [CCActionEaseBounce actionWithAction:shakeSequence];
[self runAction:bounce];
}
}


@end

最佳答案

您只有一个指向一颗星的指针。因此,当您在 touches begin 方法中测试星星是否被触摸时,当然只有第二个会做任何事情,因为您的唯一指针仅指向最近创建的星星。

要回答您的问题,您可以采取的简单方法是将每颗星添加到 NSArray。然后在您的 touches begin 方法中,您可以遍历数组并查看哪颗星被触摸了。

例子:

// Recommend using properties over i-vars
@property (nonatomic, strong) NSMutableArray* starList;

// In init or onEnter...
self.starList = [NSMutableArray array];

// Each time you create a new star...
Star* star = ...;

[self addChild:star];
[self.starList addObject:star];

// In your touches began...
- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
...

for (Star* star in self.starList)
{
// If star is touched...

// ...add your impulses, etc
}
}

关于ios - 在 Xcode cocos2d 中添加多个物理对象时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25088829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com