0

現在作成中のアプリはほぼ完成しましたが、必要な機能を 1 つ見つけようとして苦労しています。ランダムな間隔と位置で画面にドロップする 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

ドロップされるスプライトの数を変更したいのか、時間の経過とともに (視覚的に) タイプを変更したいのかわかりません。

1 つの解決策は、ivar _numberOfSpritesToBeDropped を持つことです。これは、要求された時間間隔 (たとえば 20 秒) でスケジューラによって制御される方法で増やすことができます。次に、スプ​​ライトの分散を制御するメソッドでこの ivar を使用するだけです...

20 日ごとに異なる種類のスプライトを単純に追加したい場合。ランダムステートメントを単に次のように変更する秒数

switch(arc4random() % _numberOfSpritesToBeDropped)...

補足: spriteDrop メソッドは単にスプライトをドロップするだけではないようで、2 つの異なるメソッドに分割する必要があります。

以下の議論に基づくと、おそらく次のようなものです。

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 に答える