【问题标题】:How to add multiple sprites with time interval of 3 seconds and run same CCAction for all in cocos2d如何以 3 秒的时间间隔添加多个精灵并在 cocos2d 中为所有人运行相同的 CCAction
【发布时间】:2014-08-11 10:37:06
【问题描述】:

请帮助我,因为我是搜索和 cocos 2d 的新手。如果我安排 addRed 并且我希望所有精灵在屏幕中随机移动,那么只有我得到的是最后一个精灵移动。任何帮助都将不胜感激并提前致谢。

-(void)addRed 
{
redSprite = [CCSprite spriteWithImageNamed:@"Red.png"];;
CGSize screenSize = [[CCDirector sharedDirector] viewSize];
[redSprite setPosition:CGPointMake(screenSize.width/2, screenSize.height/2)];
[self resizeSprite:redSprite toWidth:80 toHeight:80];
[self addChild:redSprite z:10];

[self gameStart];
}

- (void)gameStart {
    // Create the actions
CGSize result = [[UIScreen mainScreen] bounds].size;
CGPoint nextPoint;

if (redSprite.position.x == result.width-40.0) {
    nextPoint.x = redSprite.position.x - kAccelXAxis;
    backx = YES;
}
else {
    if (redSprite.position.x == 40.0) {
        nextPoint.x = redSprite.position.x + kAccelXAxis;
        backx = NO;

    }
    else {
        if (backx) {

            nextPoint.x = redSprite.position.x - kAccelXAxis;
        }
        else
        {
            nextPoint.x = redSprite.position.x + kAccelXAxis;
        }
    }
}

if (redSprite.position.y == 40.0) {
    nextPoint.y = redSprite.position.y + kAccelYAxis;
    backy = YES;

}
else {
    if (redSprite.position.y == result.height-40.0) {
        nextPoint.y = redSprite.position.y - kAccelYAxis;
        backy = NO;

    }
    else {
        if (backy) {
            nextPoint.y = redSprite.position.y + kAccelYAxis;
        }
        else
        {
            nextPoint.y = redSprite.position.y - kAccelYAxis;
        }
    }
}

    CCAction *myAction = [CCActionSequence actions:[CCActionMoveTo actionWithDuration:0.01 position:nextPoint], [CCActionCallFunc actionWithTarget:self selector:@selector(gameStart)], [CCActionCallFunc actionWithTarget:self selector:@selector(updateCol:)],nil];
[myAction setTag:10];

[redSprite runAction:myAction];

}

【问题讨论】:

  • 这不是关于 cocos2d 而是关于编程。对OOP和objective-c有一些基本的了解,你会感觉好多了。您在 addRed 中使用的 redSprite 变量包含对最后创建的 CCSprite 的引用。时期。就是这样。它按规定工作。

标签: cocos2d-iphone ccsprite ccaction


【解决方案1】:

尝试创建一个间隔为 3 秒的更新程序来创建您的精灵。对于您创建的精灵,使用 CCRepeatForever,它将为您的精灵生成下一个位置。

对于您的情况,我还考虑过使用自定义 CCSprite 来存储您要移动的下一个位置。

你需要类似...

   [self schedule:@selector(createSprite) interval:(3)]; //to schedule your sprite generator

// Your sprite generator with action
-(void)createSprite{
    CCCustomRed *red = [CCCustomRed spriteWithFile:@"red.png"];
    [self addChild:red];
    CCCallFuncN *changePos = [CCCallFuncN actionWithTarget:self selector:@selector(setRandomPos:)];
    CCMoveTo *move = [CCMoveTo actionWithDuration:1 position:red.nextPosition];
    CCDelayTime *delay = [CCDelayTime actionWithDuration:1.0];
    [red runAction:[CCRepeatForever actionWithAction:[CCSequence actions:move,changePos,delay,nil]]];
}

//Generate a random pos as your sprite next move
-(void)setRandomPos:(id)sender{
    CCCustomRed *red = (CCCustomRed *) sender;
    CGSize screenSize = [[CCDirector sharedDirector] winSize];
    red.nextPosition = ccp(arc4random()%screenSize.width,arc4random()%screenSize.height);
}

希望对你有帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-22
    • 2012-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多