【问题标题】:Having trouble adding multiple physics objects in Xcode cocos2d在 Xcode cocos2d 中添加多个物理对象时遇到问题
【发布时间】:2014-08-01 21:17:46
【问题描述】:

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

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

问题是,只有放置在屏幕上的第二个精灵是可点击的。我的猜测是,当我调用 addNewStar 时,它会将 _star 替换为最新的星精灵,并将之前的 _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

【问题讨论】:

    标签: ios objective-c xcode cocos2d-iphone physics


    【解决方案1】:

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

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

    例子:

    // 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
        }
    }
    

    【讨论】:

    • 太棒了!非常感谢。像魅力一样工作。你是最棒的。
    【解决方案2】:

    这是我的项目中的 touchBegan。也许您可以根据需要稍微更改代码。你在这里能看到什么?在获取触摸代码的位置后,搜索被触摸的 b2Body。在您的示例中,您需要在 _physicsNode 中搜索被触摸的 _child。

    for(UITouch *touch in allTouches)
    {
        CGPoint location = [touch locationInView:touch.view];
    
        location = [[CCDirector sharedDirector] convertToGL:location];
        b2Vec2 worldLoc = b2Vec2(ptm(location.x), ptm(location.y));
    
     // SEARCH YOUR CHILD FROM NODE HERE
    
        for (b = world->GetBodyList(); b; b = b->GetNext())
        {
            if (b->GetType() == b2_dynamicBody)
    
    
                if (b->IsBullet() == false)
                {
                for (b2Fixture *f = b->GetFixtureList(); f; f = f->GetNext())
                {
    
                    // Hit!
                    if (f->TestPoint(worldLoc))
                    {
    
                      //do stuff
    
                        }
                        break;
                    }
                }
                }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-23
      • 2011-06-10
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2019-12-03
      • 2023-03-23
      相关资源
      最近更新 更多