Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.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
Objective c 如何延迟精灵的出现?_Objective C_Xcode_Cocos2d Iphone - Fatal编程技术网

Objective c 如何延迟精灵的出现?

Objective c 如何延迟精灵的出现?,objective-c,xcode,cocos2d-iphone,Objective C,Xcode,Cocos2d Iphone,我几乎完成了我目前正在制作的应用程序,但我很难找到一个我需要的功能。我有5个不同的精灵,以随机的间隔和位置出现在屏幕上。我的问题是我需要告诉我的代码,在一定的时间内,例如20秒,一次只会出现一个精灵。然后在20秒后,更多的会同时开始下降。下面是我删除精灵的方法: //The init method -(id) init { if((self=[super init])) { //Enable touch self.isTouchEnabled = YES; //Allo

我几乎完成了我目前正在制作的应用程序,但我很难找到一个我需要的功能。我有5个不同的精灵,以随机的间隔和位置出现在屏幕上。我的问题是我需要告诉我的代码,在一定的时间内,例如20秒,一次只会出现一个精灵。然后在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];}

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

不确定您是否希望随着时间的推移删除精灵的数量或类型(视觉)发生变化

一种解决方案是删除一个ivar编号的SPRITESTOBE。在调度程序控制的方法中,这可以在请求的时间间隔(例如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];
}
NSInteger\u允许的最大CurrentNumberOfSprites;
-(id)init{
self=[super init];
如果(自我){
[自我设置];
}
回归自我;
}
-(无效)设置{
self.isTouchEnabled=是;
sprites=[[NSMutableArray alloc]init];
_允许的最大CurrentNumberOfSprites=1;
[自调度:@selector(scheduleDrop:)间隔:2.0];
}
-(无效)spriteDrop{
如果(sprites.count<_允许的最大当前sprites数){
CGSize winSize=[[CCDirector sharedDirector]winSize];
NSPLS*SPRITENAM= = [自随机SPRITENAME],我也会考虑做类似的事情。
sprite=[CCSprite spriteWithFile:spriteName];
int RandomX=(arc4random()%200);
sprite.position=ccp(随机x,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实际值=(arc4random()%范围);
currentPos=实际值;
[自我添加的孩子:宝贝];
int minDur=2.0;
int maxDur=5.0;
int rangeDur=maxDur-minDur;
int actualDur=(arc4random()%rangeDur)+minDur;
id drop=[CCMoveTo actionWithDuration:actualDur位置:ccp(实际,-sprite.contentSize.height/2)];
id dropDone=[CCCallFuncN actionWithTarget:self-selector:@selector(spriteDropDone:)];
[sprite runAction:[CCSequence actions:drop、dropDone、nil];
sprite.tag=1;
[精灵添加对象:精灵];
}
}
-(NSString*)随机spriteName{
NSString*strSprite;
开关(arc4random()%5){
案例1:
strSprite=@“1.png”;
打破
案例2:
strSprite=@“2.png”;
打破
案例3:
strSprite=@“3.png”;
打破
案例4:
strSprite=@“4.png”;
打破
案例5:
strSprite=@“5.png”;
打破
违约:
strSprite=@“1.png”;
打破
}
返回strSprite;
}
//使用调度程序以任意间隔调用此方法
-(void)增加允许的最大数量{
_允许的最大CurrentNumberOfSprites++;
}
//这是安排放置的方法
-(无效)计划删除:(ccTime)dt{
[自喷水推进器];
}

我要更改的是掉落的精灵数量。我还在学习cocos2d和objective-c,所以我不知道如何使用ivar。你能展示一个例子或者一个链接,其中有一个很好的例子在使用它吗?ivar是一个类范围内的变量。在任何方法之前,只需在实现文件中定义一个NSInteger,如:NSInteger\u nameOfIvar;据我所见,根本没有精灵的编号。您只需始终每2秒创建一个新的。哦,spriteDropDone方法做什么?spriteDropDone方法会在精灵到达屏幕y轴上的某个点时移除精灵。好的,现在您正在调用该方法,每2秒显示和移动精灵一次。精灵离开屏幕需要2-5秒(?)这意味着有时屏幕上会有多个精灵(如果前一个还没有离开)。对吗?如果在spriteDropDone方法中从精灵数组中删除精灵,则可以在设置新精灵之前,检查此数组是否已包含足够的精灵。。。