0
votes

Currently i have two colors cirlces (red and green) moving from the right side of my screen to the left. Now these circles or nodes are being called using this function

-(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);

}

I have it set up thanks to Roecrew (touchesMoved on a specific moving object) That when I touch the green circle it will move with my finger.

My Problem: Whenever a green node is created after a green node or a red node is created after a red node, the previously created node looses all its attributes and can no longer be touched/moved or be removed when it hits the end of the screen which it is programed to do ([_enemy removeFromParent]) When a red node follows a green node or visversa everything works fine. Its only when the same color proceeds the same color. Is there anyway that I can create multiples of the same node but have them each be independent? Thanks!

Ps. the line that says [_enemies addObject:_enemy]; Doesn't seem to work because its not adding them to the array.

@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
Can I see you interface code?maelswarm
Is that what you were looking for?huddie96
Nah, I mean your @interface.maelswarm
KK I added it to the bottomhuddie96
Make NSMutableArray *_enemies; a propertymaelswarm

1 Answers

0
votes

The _enemy and _enemyy variables can point to only one node at a time. Hence, you cannot access the previous node when another node is created. When another green node is created, the _enemy variable points to the new node and the previously created node loses any way to be able to access it. Here are a few ways you can use to be able to access all nodes:

1 - Use the _enemies array properly.

The _enemies array is not working because it is probably not being initialised at the start of the scene. You need to call

_enemies = [NSMutableArray new];

in the -initWithSize method.

2 - Set the name for each node

Instead of adding each node to an array, you can simply set the name of each node. Like so:

_enemy.name = @"greenCircle"

Then, to access all nodes you can use the -enumerateChildNodesWithName: method.