Krekin
Krekin

Reputation: 1562

Calling a instance method while using childNodeWithName

Is it possible to call an instance method without using an instance variable or @property?

Here is how I create an instance of a class. Within the method, I try to call on the class's instance movement method to force the instance to move:

-(void)createCharacterNPC
{
    int randomness = [self getRandomNumberBetweenMin:1 andMax:20];

    for (int i = 0; i < randomness; i += 1)
    {
        NSString *npcName = [NSString stringWithFormat:@"anNPC%i", randomness];

        NPCclass *NPC = [[NPCclass alloc] initWithName:npcName];
        NPC.position = CGPointMake(self.size.width/2, self.size.height/2);
        NPC.zPosition = 1.0;
        [_worldNode addChild:NPC];

        // THIS OBVIOUSLY WORKS. But I can't use this technique outside this method.
        [NPC beginMovement];

        // THIS IS WHAT I WANT, BUT XCODE DOESN'T ALLOW ME TO WRITE CODE THIS WAY.        
        [[_worldNode childNodeWithName:@"anNPC1"] beginMovement];
    }
}

Is there a way to allow [[_worldNode childNodeWithName:@"anNPC1"] beginMovement]; to work? Or some way similar to this so I wouldn't have to have create an instance variable of NPC (like so: _NPC)?

I'm asking because all of this is happening inside a mini-game scene and NPCclass will be initialized a random number amount of times (with arc4random() method). NPCclass moves on its own using vector (physics in a platformer) movement but I need to initialize its movement method right after creation then I need to periodically access each individually created instance of NPCclass using its name in other methods of the scene. Since I don't know how many NPCclass instances will be created each time the mini-game is played, I CAN'T use IVAR's or something like @property NPCclass *anNPC;

Please help.

Upvotes: 0

Views: 66

Answers (2)

CloakedEddy
CloakedEddy

Reputation: 1995

Xcode complains about

 [[_worldNode childNodeWithName:@"anNPC1"] beginMovement];

because the method -childNodeWithName returns an SKNode object. Instances of the SKNode class do not respond to the selector -beginMovement (or as Xcode puts it, no visible @interface declares the selector -beginMovement). Xcode shows this to you to force you to make sure you wrote what you wanted to write. Since you are sure, you can tell Xcode that the returned object is of the type NPCclass.

(NPCclass *)[_worldNode childNodeWithName:@"anNPC1"]

Now you can expand the statement to call -beginMovement.

[(NPCclass *)[_worldNode childNodeWithName:@"anNPC1"] beginMovement];

Note

There are a few concepts which you might be confusing. NPCclass is a class. +node is a class method of SKNode, which you can call with [NPCclass node];. -beginMovement is an instance method, called with:

NPCclass *npc = [NPCclass node];
[npc beginMovement];

Or:

[(NPCclass *)anyObject beginMovement]; 
// make sure anyObject responds to this selector though, or you app will crash.

Class methods are prefixed with a +, instance methods with -.

Upvotes: 1

zaph
zaph

Reputation: 112857

Class methods do not use an instance, just the class name.

As an example consider the

NSString` class method: `+ (id nullable)stringWithContentsOfFile:(NSString * nonnull)path

and a usage:

NSString *fileData = [NSString stringWithContentsOfFile:filePath];

Upvotes: 0

Related Questions