Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocos2d iphone 如何以3秒的时间间隔添加多个精灵,并在cocos2d中为所有精灵运行相同的CCAction_Cocos2d Iphone_Ccsprite_Ccaction - Fatal编程技术网

Cocos2d iphone 如何以3秒的时间间隔添加多个精灵,并在cocos2d中为所有精灵运行相同的CCAction

Cocos2d iphone 如何以3秒的时间间隔添加多个精灵,并在cocos2d中为所有精灵运行相同的CCAction,cocos2d-iphone,ccsprite,ccaction,Cocos2d Iphone,Ccsprite,Ccaction,请帮助我,因为我对搜索和Cocos2D的新体验感到厌倦。只有我得到的是最后一个精灵得到移动,如果我计划addRed,我希望所有精灵在屏幕上随机移动。如有任何帮助,我们将不胜感激 -(void)addRed { redSprite = [CCSprite spriteWithImageNamed:@"Red.png"];; CGSize screenSize = [[CCDirector sharedDirector] viewSize]; [redSprite setPosition:CGPo

请帮助我,因为我对搜索和Cocos2D的新体验感到厌倦。只有我得到的是最后一个精灵得到移动,如果我计划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];

}

尝试创建一个间隔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);
}

希望有帮助:

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