0

嗨,我是编程新手,我一直在学习 SpriteKit。(尽管这是一个通用编程问题。)我有硬编码值,并一遍又一遍地重复使用相同的图像。据我了解,编码不。有人可以通过我假设的循环重构来帮助我学习这一点。

我正在创建一排 20 个虚线空卡片点,并一次将 4 个放置到屏幕底部居中的屏幕尺寸。我已将这些空白点(仅一张卡片图像反复使用)作为“孩子”添加到机架上。

旁注:如果您不熟悉 SpriteKit,这是一个通用编程问题,我的 CGPoints 映射到机架坐标系而不是屏幕坐标……如果这让您感到困惑……

我试图在底部创建重构代码,但我被卡住了,因为 node.positions 与 SKSpriteNodes 相关联并且循环似乎让我感到困惑。

这是我所拥有的:

-(SKSpriteNode*)createBottomRack
{
    self.rack = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor]   size:CGSizeMake(10240.0, 200)];
    self.rack.position = CGPointMake(0.0,150.0);
    self.rack.zPosition = 0;
    self.rack.name = @"bottomRack";

    // Screen 1
    SKSpriteNode *number0 = [SKSpriteNode spriteNodeWithImageNamed:@"dashedCard"];
    SKSpriteNode *number1 = [SKSpriteNode spriteNodeWithImageNamed:@"dashedCard"];
    SKSpriteNode *number2 = [SKSpriteNode spriteNodeWithImageNamed:@"dashedCard"];
    SKSpriteNode *number3 = [SKSpriteNode spriteNodeWithImageNamed:@"dashedCard"];
    // Screen 1 positions
    number0.position = CGPointMake(212, 0.0);
    number1.position = CGPointMake(412, 0.0);
    number2.position = CGPointMake(612, 0.0);
    number3.position = CGPointMake(812, 0.0);

    // Screen 2
    SKSpriteNode *number4 = [SKSpriteNode spriteNodeWithImageNamed:@"dashedCard"];
    SKSpriteNode *number5 = [SKSpriteNode spriteNodeWithImageNamed:@"dashedCard"];
    SKSpriteNode *number6 = [SKSpriteNode spriteNodeWithImageNamed:@"dashedCard"];
    SKSpriteNode *number7 = [SKSpriteNode spriteNodeWithImageNamed:@"dashedCard"];

    // Screen 2 positions
    number4.position =CGPointMake(1236.0, 0.0);
    number5.position =CGPointMake(1436.0, 0.0);
    number6.position =CGPointMake(1636.0, 0.0);
    number7.position =CGPointMake(1836.0, 0.0);

    // Screen 3
     SKSpriteNode *number8 = [SKSpriteNode spriteNodeWithImageNamed:@"dashedCard"];
     SKSpriteNode *number9 = [SKSpriteNode spriteNodeWithImageNamed:@"dashedCard"];
     SKSpriteNode *number10 = [SKSpriteNode spriteNodeWithImageNamed:@"dashedCard"];
     SKSpriteNode *number11 = [SKSpriteNode spriteNodeWithImageNamed:@"dashedCard"];

    // Screen 3 positions
    number8.position =CGPointMake(2260.0, 0.0);
    number9.position =CGPointMake(2460.0, 0.0);
    number10.position =CGPointMake(2660.0, 0.0);
    number11.position =CGPointMake(2860.0, 0.0);

    // Screen 4
    SKSpriteNode *number12 = [SKSpriteNode spriteNodeWithImageNamed:@"dashedCard"];
    SKSpriteNode *number13 = [SKSpriteNode spriteNodeWithImageNamed:@"dashedCard"];
    SKSpriteNode *number14 = [SKSpriteNode spriteNodeWithImageNamed:@"dashedCard"];
    SKSpriteNode *number15 = [SKSpriteNode spriteNodeWithImageNamed:@"dashedCard"];

    // Screen 4 positions
    number12.position =CGPointMake(3284.0, 0.0);
    number13.position =CGPointMake(3484.0, 0.0);
    number14.position =CGPointMake(3684.0, 0.0);
    number15.position =CGPointMake(3884.0, 0.0);

    // Screen 5
    SKSpriteNode *number16 = [SKSpriteNode spriteNodeWithImageNamed:@"dashedCard"];
    SKSpriteNode *number17 = [SKSpriteNode spriteNodeWithImageNamed:@"dashedCard"];
    SKSpriteNode *number18 = [SKSpriteNode spriteNodeWithImageNamed:@"dashedCard"];
    SKSpriteNode *number19 = [SKSpriteNode spriteNodeWithImageNamed:@"dashedCard"];

    // Screen 5 positions
    number16.position =CGPointMake(4308.0, 0.0);
    number17.position =CGPointMake(4508.0, 0.0);
    number18.position =CGPointMake(4708.0, 0.0);
    number19.position =CGPointMake(4908.0, 0.0);

    // Add dashedCards in position on screen
    [self.rack addChild:number0];
    [self.rack addChild:number1];
    [self.rack addChild:number2];
    [self.rack addChild:number3];
    [self.rack addChild:number4];
    [self.rack addChild:number5];
    [self.rack addChild:number6];
    [self.rack addChild:number7];
    [self.rack addChild:number8];
    [self.rack addChild:number9];
    [self.rack addChild:number10];
    [self.rack addChild:number11];
    [self.rack addChild:number12];
    [self.rack addChild:number13];
    [self.rack addChild:number14];
    [self.rack addChild:number15];
    [self.rack addChild:number16];
    [self.rack addChild:number17];
    [self.rack addChild:number18];
    [self.rack addChild:number19];

    return self.rack;
}

这是我目前正在重构的地方......

NSMutableArray *dashesArray = [NSMutableArray arrayWithCapacity:20];
for (int i = 0; i<=20; i++) {
    SKSpriteNode *dash = [SKSpriteNode spriteNodeWithImageNamed:@"dashedCard"];
    [dashesArray addObject:dash];

}

这是因为如果我制作一个包含通用编号 SKSpriteNodes 的循环,我看不到我如何将它们映射到位置......?

4

2 回答 2

0

由于您是编程新手,我建议您花一些时间了解数组的工作原理以及它们的使用方式:

http://en.wikipedia.org/wiki/Array_data_structure

以及他们的 iOS 实现:

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html

对于初学者,您可以声明两个数组,一个包含 SKSpriteNodes,另一个包含位置点:

   //Declare these in @interface
        @property (nonatomic) NSMutableArray *sprites;
        @property (nonatomic) NSMutableArray *points;

    //This will contain the SKSpriteNodes
        self.sprites =[[NSMutableArray alloc] init];

    //This will contain the points, we initialise it with the hard-coded values.
    //You have to wrap the values in an object type of NSValue of the array to accept it
        self.points = [[NSMutableArray alloc] initWithObjects:[NSValue valueWithCGPoint:CGPointMake(212, 0.0)],
                       [NSValue valueWithCGPoint:CGPointMake(412, 0.0)],
                       [NSValue valueWithCGPoint:CGPointMake(612, 0.0)],
                       [NSValue valueWithCGPoint:CGPointMake(812, 0.0)],
                       //Put the rest of the values here... for the moment it will do
                       nil];

    //Now for each declared point, we create a SKSpriteNode, insert it into the array and add it to "rack"

//for will loop over eacg point you have defined.
        for (NSValue *point in self.points){

//This will be a temporary object we create to add it into the array.
            SKSpriteNode *node =[SKSpriteNode spriteNodeWithImageNamed:@"dashedCard"];
            node.position = [point CGPointValue];
            [self.sprites addObject:node];
            [self.rack addChild:node];
        }

随着您获得更多经验,您会发现许多其他改进代码的方法,但请花时间研究代码和基本数据结构。祝你好运!

于 2013-10-16T20:47:06.023 回答
0

计算每个节点的x坐标position非常简单:

  • x从 212 开始
  • 后续节点在前一个节点右侧 200px
  • 每第 5 个节点在前一个节点的右侧 424 像素(而不是 200 像素)

代码如下所示:

- (SKSpriteNode *)createBottomRack
{
    self.rack = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(10240.0, 200)];
    self.rack.position = CGPointMake(0.0,150.0);
    self.rack.zPosition = 0;
    self.rack.name = @"bottomRack";

    float x = 212.0, y = 0.0;
    for (NSUInteger i = 0; i < 20; i++) {
        SKSpriteNode *dash = [SKSpriteNode spriteNodeWithImageNamed:@"dashedCard"];
        dash.position = CGPointMake(x, y);
        [self.rack addChild:dash];

        // Calculate the next x coordinate
        x += (i % 4 == 3) ? 424.0 : 200;
    }

    return self.rack;
}
于 2013-10-16T20:49:49.393 回答