3
votes

I have a class named Node,

Node is a subclass of SKNode,

I have created and added a SKShapeNode object as a child in the init method of the Node object,

But when i try to modify this child object after adding it as a child, nothing happens.

@interface Node ()
@property (nonatomic, strong) SKShapeNode *circle;
@end

@implementation Node

- (id)initWithRadius:(float)radius{

    if (self = [super init]) {
        _circle = [SKShapeNode shapeNodeWithCircleOfRadius:radius];
        _circle.fillColor = [UIColor whiteColor];
        _circle.name = @"c";
        [self addChild:_circle];
    }
    return self;
}

//1st type of handling child object
- (void)setHighlighted{

    NSLog(@"Called");
    SKShapeNode *circ = (SKShapeNode *)[self childNodeWithName:@"/c"];
    circ.fillColor = [SKColor redColor];
}

I also tried changing attribute of the property directly

- (void)setHighlighted{

   _circle.fillColor = [SKColor redColor];
}

The setHighlighted method is being called for sure.

I even tried [_circle removeFromParent] and set position, alpha, isHidden attributes but nothing happens!!

What is the problem here?

Thanks

EDIT:

There is no problem with the code above!

Sorry the problem was: I have NSArray *nodes I was populating nodes and as they are being created I stored them in a dummy NSMutableArray *mutableNodesArray at the same time I was adding them as a child to the scene; and after the loop finished, I "copied" objects from the mutableNodesArray to the nodesArray,

so when i was trying to do operations on a Node object stored inside the nodesArray, methods were being called but these methods did not belong to the Node objects added as a child...

My mistake...

2
you should be using the second method anyway since circle is global, more efficient than the first - Knight0fDragon

2 Answers

3
votes

I suppose your problem is in the line:

SKShapeNode *circ = (SKShapeNode *)[self childNodeWithName:@"/c"];

that could be:

SKShapeNode *circ = (SKShapeNode *)[self childNodeWithName:@"//c"];

Differences according to API reference:

/ :

When placed at the start of the search string, this indicates that the search should be performed on the tree’s root node. When placed anywhere but the start of the search string, this indicates that the search should move to the node’s children.

// :

When placed at the start of the search string, this specifies that the search should begin at the root node and be performed recursively across the entire node tree. Otherwise, it performs a recursive search from its current position.

You could try to put also a breakpoint to the line below and check to the debug console the circ properties with po circ.debugDescription :

(breakpoint to the line:) -> circ.fillColor = [SKColor redColor];

You could try also to use a customActionWithDuration:

SKAction* circleColor = [SKAction customActionWithDuration:0.5f actionBlock:^(SKNode *node, CGFloat elapsedTime)
{
    circ.fillColor = [UIColor colorWithRed:1.0f green:0.0f blue:0.0f alpha:1.0f];
    NSLog(@"changed to red");
}];
[circ runAction:circleColor];
0
votes

Here is the problem:

- (void)setupNodes{

    NSMutableArray *mutableNodesArray = [[NSMutableArray alloc] init];
    for (int i=0; i<6; i++) {
        Node *node = [[Node alloc] initWithRadius:nodeRadius];
        [self addChild:node];
        [mutableNodesArray addObject:node];
    }

    //Problematic Statement!
    _nodes = [[NSArray alloc] initWithArray:mutableNodesArray copyItems:YES];
}

- (void)operation{
    //This is not the node that is visible on the scene!
    [(Node*)[_nodes objectAtIndex:0] setHighlighted:YES];
}

And the solution is:

_nodes = [[NSArray alloc] initWithArray:mutableNodesArray];