同时移动两个具有相同名称的精灵

时间:2015-03-05 02:02:21

标签: objective-c cocos2d-iphone sprite

我正试图移动火箭和附加在精灵上的单词同时向上移动。但是,我把它设置成只有单词飞起而不是火箭。我正在考虑将火箭(用于单词)重命名为其他东西,但我觉得这将是一种非常低效的方式。这是我到目前为止所拥有的

- (id)init
{
    // Apple recommend assigning self with supers return value
    self = [super init];
    if (!self) return(nil);

    // Enable touch handling on scene node
    self.userInteractionEnabled = YES;

    //Add a sprite
    rocket[0] = [CCSprite spriteWithImageNamed:@"rocket_base_blue.png"];
    rocket[0].scale = (0.15f);
    rocket[0].positionType = CCPositionTypeNormalized;
    rocket[0].position  = ccp(0.5f,0.23f);

    rocket[1] = [CCSprite spriteWithImageNamed:@"rocket_base_red.png"];
    rocket[1].scale = (0.15f);
    rocket[1].positionType = CCPositionTypeNormalized;
    rocket[1].position  = ccp(0.7f,0.23f);

    rocket[2] = [CCSprite spriteWithImageNamed:@"rocket_base_green.png"];
    rocket[2].scale = (0.15f);
    rocket[2].positionType = CCPositionTypeNormalized;
    rocket[2].position  = ccp(0.9f,0.23f);

    [self addChild:rocket[0]];
    [self addChild:rocket[1]];
    [self addChild:rocket[2]];
    [self setupWordRockets:2];
    [self intro];

    // done
    return self;
}

// -----------------------------------------------------------------------
#pragma mark - Game Events
// -----------------------------------------------------------------------

- (void)setupWordRockets:(int) wordLength
{

    //randomizes the words
    NSInteger rando = arc4random() % 2;
    if (rando == 1) {
    // Add a sprite
    rocket[0] = [CCSprite spriteWithImageNamed:@"b.png"];
    rocket[0].scale = (0.16f);
    rocket[0].positionType = CCPositionTypeNormalized;
    rocket[0].position  = ccp(0.5f,0.2f);
    rocket[1] = [CCSprite spriteWithImageNamed:@"a.png"];
    rocket[1].scale = (.16f);
    rocket[1].positionType = CCPositionTypeNormalized;
    rocket[1].position  = ccp(0.7f,0.2f);
    rocket[2] = [CCSprite spriteWithImageNamed:@"g.png"];
    rocket[2].scale = (.16f);
    rocket[2].positionType = CCPositionTypeNormalized;
    rocket[2].position  = ccp(0.9f,0.2f);
    } else {
    rocket[0] = [CCSprite spriteWithImageNamed:@"g.png"];
    rocket[0].scale = (0.16f);
    rocket[0].positionType = CCPositionTypeNormalized;
    rocket[0].position  = ccp(0.5f,0.2f);
    rocket[1] = [CCSprite spriteWithImageNamed:@"a.png"];
    rocket[1].scale = (.16f);
    rocket[1].positionType = CCPositionTypeNormalized;
    rocket[1].position  = ccp(0.7f,0.2f);
    rocket[2] = [CCSprite spriteWithImageNamed:@"p.png"];
    rocket[2].scale = (.16f);
    rocket[2].positionType = CCPositionTypeNormalized;
    rocket[2].position  = ccp(0.9f,0.2f);
    }
    [self addChild:rocket[0]];
    [self addChild:rocket[1]];
    [self addChild:rocket[2]];

}

- (void)launchRocket
{
    CCActionMoveTo *actionMove = [CCActionMoveTo actionWithDuration:1.0f position:ccp(0.5f, 0.9f)];
    [rocket[0] runAction:actionMove];
    CCActionMoveTo *actionMove2 = [CCActionMoveTo actionWithDuration:1.0f position:ccp(0.5f, 0.9f)];
    [rocket[1] runAction:actionMove2];
    CCActionMoveTo *actionMove3 = [CCActionMoveTo actionWithDuration:1.0f position:ccp(0.5f, 0.9f)];
    [rocket[2] runAction:actionMove3];
    [self scheduleOnce:@selector(explode:) delay:1];
}

我也想知道是否还有一种更有效的随机化方法,因为我觉得这是蛮力方法,或者至少是一种更乏味的方法。任何反馈将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

这是因为当setupWordRockets函数工作时,你将单词存储在rocket数组中,当你调用launchRocket时,它会移动单词。

你应该将它们分开并使用2个阵列(一个用于火箭精灵的第一个),所以你也可以给火箭做适当的动作。