简体   繁体   English

Sprite Kit - Objective c,同时移动多个 SKNode

[英]Sprite Kit - Objective c, move multiple SKNode at the same time

i have this method that makes the Lines我有这种制作线条的方法

    lineaGuida_Img = [SKTexture textureWithImageNamed:@"LineaGuida copia.jpeg"];  
_Linea = [SKNode node];
_Linea.position = CGPointMake((self.frame.size.width / 7 ) * 2, self.frame.size.height / 2 );
_Linea.zPosition = -10;

SKSpriteNode * linea = [SKSpriteNode spriteNodeWithTexture:lineaGuida_Img];
linea.position = CGPointMake(0,0);
linea.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:linea.size];
linea.physicsBody.dynamic = FALSE;
linea.physicsBody.collisionBitMask = 0;
linea.physicsBody.categoryBitMask = CollisionEnemy;
[_Linea addChild:linea];

[self addChild:_Linea];

In the touchesBegan method when i touch the screen, the player is always on the center of screen, and those lines behind him have to move by -x.在 touchesBegan 方法中,当我触摸屏幕时,玩家始终位于屏幕中央,而他身后的那些线必须移动 -x。

  [_Linea runAction:[SKAction moveByX: -(self.size.width/8) y:0 duration:0.1]];
  [self spawn_Lines];

But this action is only executed by the last SKNode.但是这个动作只由最后一个SKNode执行。 I need to apply this to all Lines simultaneously.我需要同时将其应用于所有行。 After that, when the line's position is less then the player's position, the line must be deleted.之后,当线的位置小于玩家的位置时,必须删除该线。

SpriteKit uses a tree based model, so whatever the parent node does, the children fall suit. SpriteKit 使用基于树的模型,因此无论父节点做什么,子节点都适合。 Create an SKNode to house all of your lines, add all of your lines to this SKNode, then apply the actions to the SKNode, not the lines.创建一个 SKNode 来容纳您的所有线路,将您的所有线路添加到此 SKNode,然后将操作应用于 SKNode,而不是线路。

Add them to NSMutableArray to keep the reference and use for each loop to make each one run the action.将它们添加到 NSMutableArray 以保留引用并用于每个循环以使每个循环运行操作。 I would recommend you to use NSTimer to provide [SKAction moveByX: -1 y:0 duration:0]] with constant speed which will result in the same motion as you already used.我建议您使用 NSTimer 以恒定速度提供 [SKAction moveByX: -1 y:0 duration:0]] ,这将导致与您已经使用的运动相同的运动。 Each time this NSTimer execute you will check all positions of object from your NSMutableArray and than delete it if it fits your conditions.每次执行此 NSTimer 时,您将从 NSMutableArray 中检查对象的所有位置,如果符合您的条件,则将其删除。 Be careful when you want to lose the reference completely use [object removeFromParent];当你想完全丢失引用时要小心使用 [object removeFromParent]; and remove it also from your NSMutableArray to avoid lose of performance later on.并将其从您的 NSMutableArray 中删除,以避免以后失去性能。 For this I use rather forLoop with continue method为此,我使用 forLoop 和 continue 方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM