嗨,我是编程新手,我一直在学习 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 的循环,我看不到我如何将它们映射到位置......?