多个相同的节点但没有连接它们

时间:2014-03-30 03:27:17

标签: xcode sprite-kit nodes

目前我有两种颜色的旋风(红色和绿色)从我的屏幕右侧向左移动。现在使用此函数调用这些圆或节点

-(void)move {
if(_dead)
    return;
int random = rand() % 2;

NSLog(@"int:%d",random);

if (random == 0) {
_enemy = [SKSpriteNode spriteNodeWithImageNamed:@"greeny"];


       _enemy.position = CGPointMake(CGRectGetMidX(self.frame)+200,
                                      CGRectGetMidY(self.frame));
    _enemy.size = CGSizeMake(70, 70);
    _enemy.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:35];
    _enemy.physicsBody.mass = 0;
    _enemy.physicsBody.categoryBitMask = Collisiongreen;


        [_enemies addObject:_enemy];
        NSLog(@"Enemies%lu",(unsigned long)_enemies.count);
          [self addChild:_enemy];
[_enemy runAction:[SKAction moveByX:-900 y:0 duration:4]];
    [self runAction:[SKAction playSoundFileNamed:@"Spawn.wav" waitForCompletion:NO]];
    [self runAction:[SKAction sequence:@[
                                         [SKAction waitForDuration:1.4],
                                         [SKAction performSelector:@selector(move) onTarget:self],

                                       ]]];
        NSLog(@"Enemies%lu",(unsigned long)_enemies.count);

    }
    if (random == 1) {
        _enemyy = [SKSpriteNode spriteNodeWithImageNamed:@"redy"];
        _enemyy.position = CGPointMake(CGRectGetMidX(self.frame)+200,
                                       CGRectGetMidY(self.frame));
        _enemyy.size = CGSizeMake(70, 70);
        _enemyy.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:35];
        _enemyy.physicsBody.mass = 0;
        _enemyy.physicsBody.categoryBitMask = Collisionred;


        [_enemies addObject:_enemyy];
        NSLog(@"Enemies%lu",(unsigned long)_enemies.count);
        [self addChild:_enemyy];
        [_enemyy runAction:[SKAction moveByX:-900 y:0 duration:4]];
        [self runAction:[SKAction playSoundFileNamed:@"Spawn.wav" waitForCompletion:NO]];
        [self runAction:[SKAction sequence:@[
                                             [SKAction waitForDuration:1.4],
                                             [SKAction performSelector:@selector(move) onTarget:self],



                                         ]]];
    NSLog(@"Enemies%lu",(unsigned long)_enemies.count);

}

感谢Roecrew(touchesMoved on a specific moving object),我设置了它。当我触摸绿色圆圈时,它会用我的手指移动。

我的问题:每当在红色节点之后创建绿色节点或红色节点之后创建绿色节点时,先前创建的节点将丢失其所有属性,并且无法再触摸/移动或在其到达时移除它被编程执行的屏幕结束([_enemy removeFromParent]) 当红色节点跟随绿色节点或视觉时,一切正常。它只有当相同的颜色进行相同的颜色时。无论如何,我可以创建相同节点的多个但是每个节点都是独立的吗?谢谢!

聚苯乙烯。这句话说[_enemies addObject:_enemy];似乎没有用,因为它没有将它们添加到数组中。

@interface MyScene : SKScene <UIGestureRecognizerDelegate,SKPhysicsContactDelegate>
  {

      bool *someBool;

}
@property (strong, nonatomic) SKSpriteNode *wall;

@property (strong, nonatomic) SKSpriteNode *node;
@property (strong, nonatomic) SKSpriteNode *field;
@property (strong, nonatomic) SKSpriteNode *okfield;
@property (strong, nonatomic) SKSpriteNode *board;
@property (strong, nonatomic) SKLabelNode *seconds;
@property (strong, nonatomic) SKSpriteNode *enemy;
@property (strong, nonatomic) SKSpriteNode *enemyy;
@property (strong, nonatomic) SKSpriteNode *enemy1;
@property (strong, nonatomic) SKSpriteNode *enemy2;
@property (strong, nonatomic) SKSpriteNode *enemy3;
@property (strong, nonatomic) SKSpriteNode *enemy4;
@property (strong, nonatomic) SKSpriteNode *enemy5;
@property (strong, nonatomic) SKSpriteNode *enemy6;
@property (nonatomic, strong) TCProgressTimerNode *progressTimerNode1;
@property (nonatomic, strong) TCProgressTimerNode *progressTimerNode2;
@property (nonatomic, strong) TCProgressTimerNode *progressTimerNode3;
@property (nonatomic) NSTimeInterval startTime;




@end
@interface SKEmitterNode (fromFile)
+ (instancetype)orb_emitterNamed:(NSString*)name;
@end





@implementation MyScene{
    BOOL _dead;
    SKNode *_player;
    enum {

    Collisionbordered = 1<<1,
    Collisiongreen = 1<<2,
    Collisionred = 1<<3,
    Collisionbordergreen = 1<<4,


};
NSTimeInterval _startTime;
NSTimer *timerr;
 NSTimer *timer;
int currMinute;
int currSeconds;
SKLabelNode *myLabel;
int i;
int level;
 NSMutableArray *_enemies;
SKAction *go;
SKLabelNode *startlabel;
float changeInX;
float changeInY;
float lastX;
float lastY;
float changeInXX;
float changeInYY;
float lastXX;
float lastYY;}

1 个答案:

答案 0 :(得分:0)

_enemy和_enemyy变量一次只能指向一个节点。因此,在创建另一个节点时,您无法访问上一个节点。当创建另一个绿色节点时,_enemy变量指向新节点,并且先前创建的节点无法以任何方式访问它。您可以使用以下几种方法来访问所有节点:

1 - 正确使用_enemies数组。

_enemies数组无效,因为它可能未在场景开始时初始化。你需要打电话

_enemies = [NSMutableArray new];

-initWithSize方法中。

2 - 设置每个节点的名称

您可以简单地设置每个节点的名称,而不是将每个节点添加到数组中。像这样:

_enemy.name = @"greenCircle"

然后,要访问所有节点,您可以使用-enumerateChildNodesWithName:方法。