0

我几乎完成了我目前正在制作的应用程序,但我很难找出我需要的一项功能。我有 5 个不同的精灵以随机的间隔和位置落在屏幕上。我的问题是我需要告诉我的代码在一定时间内,例如 20 秒,一次只会出现 1 个精灵。然后在 20 秒后,更多的会同时开始下降。这是我删除精灵的方法的样子:

//The init method
-(id) init
{
if((self=[super init])) {
    //Enable touch
    self.isTouchEnabled = YES;

    //Allocate and initialise
    sprites = [[NSMutableArray alloc]init];

    CGSize winSize = [[CCDirector sharedDirector]winSize];

    //Add the sprites
    [self schedule:@selector(scheduleDrop:)interval:2.0];
}
return self;
}

//Method to drop sprites
-(void)spriteDrop
{
    CGSize winSize = [[CCDirector sharedDirector]winSize];

int RandomX = (arc4random() % 200);

NSString *strSprite = @"1.png";

switch(arc4random() % 5){
    case 1:
        strSprite = @"1.png";
        break;
    case 2:
        strSprite = @"2.png";
        break;
    case 3:
        strSprite = @"3.png";
        break;
    case 4:
        strSprite = @"4.png";
        break;
    case 5:
        strSprite = @"5.png";
        break;
}

sprite = [CCSprite spriteWithFile:strSprite];
sprite.position = ccp(RandomX, 500);
sprite.scaleX = 40 / sprite.contentSize.width;
sprite.scaleY = 150 / winSize.height;

int posMinY = sprite.contentSize.width / 2;
int posMaxY = winSize.height - sprite.contentSize.height / 2;
int range = posMaxY - posMinY;
int actual = (arc4random()%range);
currentPos = actual;

[self addChild:baby];

int minDur = 2.0;
int maxDur = 5.0;
int rangeDur = maxDur - minDur;
int actualDur = (arc4random()%rangeDur) + minDur;

id drop = [CCMoveTo actionWithDuration:actualDur position:ccp(actual, -sprite.contentSize.height/2)];
id dropDone = [CCCallFuncN actionWithTarget:self selector:@selector(spriteDropDone:)];
[sprite runAction:[CCSequence actions:drop, dropDone, nil]];

sprite.tag = 1;
[sprites addObject: sprites];
}

//This is the method that schedules the drop
-(void)scheduleDrop:(ccTime)dt
{
[self spriteDrop];}

我希望有人能够帮助我解决这个问题。

4

1 回答 1

1

不确定您是否希望被丢弃的精灵数量或类型(视觉上)随着时间而改变。

一种解决方案是使用 ivar _numberOfSpritesToBeDropped。这可以在由调度程序以请求的时间间隔(例如 20 秒)控制的方法中增加。然后,您只需在控制精灵分散的方法中使用此 ivar...

如果您只是想每 20 次添加不同类型的精灵。几秒钟,您只需将随机语句更改为

switch(arc4random() % _numberOfSpritesToBeDropped)...

旁注:您的 spriteDrop 方法似乎不仅仅是简单地删除精灵,它肯定会被分成两种不同的方法。

根据我们下面的讨论,可能是这样的:

NSInteger _maximumCurrentNumberOfSpritesAllowed;

-(id) init{
    self = [super init];

    if(self) {
        [self setup];
    }
    return self;
}


-(void) setup{
    self.isTouchEnabled = YES;
    sprites = [[NSMutableArray alloc]init];
    _maximumCurrentNumberOfSpritesAllowed = 1;
    [self schedule:@selector(scheduleDrop:)interval:2.0];
}


-(void)spriteDrop{
    if (sprites.count < _maximumCurrentNumberOfSpritesAllowed) {

        CGSize winSize = [[CCDirector sharedDirector]winSize];

        NSString *spriteName = [self randomSpriteName]; // I'd consider doing something similar with the position etc. as well
        sprite = [CCSprite spriteWithFile:spriteName];

        int RandomX = (arc4random() % 200);
        sprite.position = ccp(RandomX, 500);
        sprite.scaleX = 40 / sprite.contentSize.width;
        sprite.scaleY = 150 / winSize.height;

        int posMinY = sprite.contentSize.width / 2;
        int posMaxY = winSize.height - sprite.contentSize.height / 2;
        int range = posMaxY - posMinY;
        int actual = (arc4random()%range);
        currentPos = actual;

        [self addChild:baby];

        int minDur = 2.0;
        int maxDur = 5.0;
        int rangeDur = maxDur - minDur;
        int actualDur = (arc4random()%rangeDur) + minDur;

        id drop = [CCMoveTo actionWithDuration:actualDur position:ccp(actual, -sprite.contentSize.height/2)];
        id dropDone = [CCCallFuncN actionWithTarget:self selector:@selector(spriteDropDone:)];
        [sprite runAction:[CCSequence actions:drop, dropDone, nil]];

        sprite.tag = 1;
        [sprites addObject: sprites];
    }
}


-(NSString *)randomSpriteName{
    NSString *strSprite; 
    switch(arc4random() % 5){
        case 1:
            strSprite = @"1.png";
            break;
        case 2:
            strSprite = @"2.png";
            break;
        case 3:
            strSprite = @"3.png";
            break;
        case 4:
            strSprite = @"4.png";
            break;
        case 5:
            strSprite = @"5.png";
            break;
        default:
            strSprite = @"1.png";
            break;
    }

    return strSprite;
}


// Call this method with a scheduler at whatever interval you'd like
-(void) increaseMaximumNumberOfSpritesAllowed{
    _maximumCurrentNumberOfSpritesAllowed++;
}

//This is the method that schedules the drop
-(void)scheduleDrop:(ccTime)dt{
    [self spriteDrop];
}
于 2013-01-24T08:00:11.397 回答