如何在sprite-kit中50点后向spawning方法添加新对象

时间:2016-01-31 23:24:43

标签: ios objective-c sprite-kit 2d-games

我在游戏中有6种不同的敌人,我想开始随机产生一个,直到我得到50分,然后添加一个新的随机。我想这样做,直到所有的敌人都在玩耍。在这一刻,所有的敌人都从一开始就在玩耍。

我有一个arc4random switch语句叫做wenn我的移动地面处于某个位置。我读到NSMutableArray可能是一个解决方案,但它只能包含对象。

这是我的代码:

// The method that is called to add an random enemy:
-(void)addRandomEnemy {

switch (arc4random() % 12) {
    case 0:
        [self addEnemy1];
        break;
    case 1:
        [self addEnemy2];
        break;
    case 2:
        [self addEnemy3];
        break;
    case 3:
        [self addEnemy4];
        break;
    case 4:
        [self addEnemy5];
        break;
    case 5:
        [self addEnemy6];
        break;
    }
}

// Inside the update method:
if (ground.position.x < 333 && ground.position.x > 328) {

        [self addRandomEnemy];
    }
if (ground.position.x < 666 && ground.position.x > 661) {

        [self addRandomEnemy];
    }

我的问题是,如何在特定数量的点之后为产卵周期添加新的敌人?

谢谢你们! (在Objective-c中的代码请)

编辑更新代码:

-(void)addRandomEnemy:(int)number {

// Array
NSArray *numberArray = @[@0, @1, @2, @3, @4 ,@5, @6]; 
NSMutableArray *numbers = [[NSMutableArray alloc] initWithArray:numberArray];

// shuffle
NSUInteger count = [numbers count];
for (NSUInteger i = 0; i < count; ++i) {

    int nElements = count - i;
    int n = (arc4random() % nElements) + i;
    [numbers exchangeObjectAtIndex:i withObjectAtIndex:n];
}

// Here I can't replace the case 0 with numbers[0]
switch (arc4random() % number) {
case 0:
    [self addEnemy1];
    break;
case 1:
    [self addEnemy2];
    break;
case 2:
    [self addEnemy3];
    break;
case 3:
    [self addEnemy4];
    break;
case 4:
    [self addEnemy5];
    break;
case 5:
    [self addEnemy6];
    break;
   }
}

它现在说:“表达式不是整数常量表达式”

1 个答案:

答案 0 :(得分:1)

//This 2 arrays you should initialize like properties, not in the update method, because otherwise result array will be always with 1 random element 
NSMutableArray *numberArray = [NSMutableArray arrayWithArray:@[@0,@1,@2,@3,@4,@5]];
NSMutableArray *resultArr = [[NSMutableArray alloc] initWithCapacity:6];

NSNumber *randomType = [[NSNumber alloc] initWithDouble:(arc4random() % number)];

        if ([numberArray containsObject:randomType]) {
            [numberArray removeObject:randomType];
            if (![resultArr containsObject:randomType]) {
                [resultArr addObject:randomType];
            }
        }

        NSNumber *enemyTypeIndex = [[NSNumber alloc] initWithDouble:arc4random() % [resultArr count]];
       [self addSomeEnemy:[resultArr objectAtIndex:[enemyTypeIndex integerValue]]];

那就是你的adSomeEnemy方法(取消注释方法调用):

- (void)addSomeEnemy:(NSNumber *)enemy {

    switch ([enemy integerValue]) {

        case 0:
            //[self addEnemy1];
            break;
        case 1:
           // [self addEnemy2];
            break;
        case 2:
            //[self addEnemy3];
            break;
        case 3:
           // [self addEnemy4];
            break;
        case 4:
           // [self addEnemy5];
            break;
        case 5:
           // [self addEnemy6];
            break;
        }
    }

只有你现在应该做的事情 - 将2个第一个数组作为属性,在你想要的地方(但在第一个更新调用之前)初始化它们,例如在initWithSize方法中,并替换&#34; numberArray&#34; /&# 34; resultArr&#34;与&#34; self.numberArray&#34; /&#34; self.resultArr&#34;