精灵套件NSArray的多个精灵?

时间:2014-01-19 02:32:17

标签: nsmutablearray sprite-kit sprite

我有6个精灵图像,我试图添加到我的场景中,添加它们中的每一个似乎都会减慢很多东西。我想我需要创建一个NSArray以帮助提高速度。这是我创建的数组,但它只添加第一个图像,如何才能将它添加到全部6?提前谢谢!

myArray的

NSArray *myArray = [NSArray arrayWithObjects:@"image1",@"image2",@"image3",@"image4",@"image5",@"image6", nil];

NSInteger count = [myArray count];
for (int i = 0; i < count; i++) {
    if (i > 5) {
        break;
    }
    result = [myArray objectAtIndex:i];
}

//Setting SKSpriteNodes from array.
dice = [SKSpriteNode spriteNodeWithImageNamed:[myArray objectAtIndex:result.intValue]];

1 个答案:

答案 0 :(得分:1)

在场景中定义属性:

@interface MyScene
@property (nonatomic) NSMutableArray *items;
@end

然后创建一个填充该数组的方法:

- (void)fillItems {

for (int i=0; i<10; i++) {
    SKSpriteNode *d1 = [SKSpriteNode spriteNodeWithImageNamed:@"Sprite1"];

    d1.position = CGPointMake(self.frame.size.width/4 + arc4random() % ((int)self.frame.size.width/2),
                              self.frame.size.height/2 + arc4random() % ((int)self.frame.size.height/2));

    d1.color = [self randomColor];
    d1.colorBlendFactor = 1.0;
    d1.xScale = 0.25;
    d1.yScale = 0.25;
    d1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:d1.frame.size];

    //Adding SpriteKit physicsBody for collision detection
    d1.physicsBody.categoryBitMask = diceCategory;
    d1.physicsBody.dynamic = YES;
    d1.physicsBody.contactTestBitMask = frameCategory;
    d1.physicsBody.collisionBitMask = diceCategory | frameCategory;
    d1.physicsBody.usesPreciseCollisionDetection = YES;
    d1.name = @"Sprite1";
    [self.items addObject:d1];
    [self addChild:d1];
}