0

请帮助我,因为我已经接受了搜索和 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];

}
4

1 回答 1

0

尝试以 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);
}

希望能帮助到你 :)

于 2014-08-11T16:31:08.683 回答