0

I'm still fairly new to programming and what I tried to do is the following: So I separated my Enemies from my game scene to a different class. In the Enemy class.m file I declared 6 methods. Every method represents a new level, which will get called from the game scene. So in the methods I declare the sprite's image, path, shooting particle type, etc.. Here's an example of the level 1 method in the EnemyClass.m file:

@implementation EnemyClass


+(void)enemiesLevel1
{
EnemyName = @"enemy1";
SKSpriteNode* enemy = [SKSpriteNode spriteNodeWithImageNamed:EnemyName];
pathSpeed = 3;
CGPathRef path = CGPathCreateWithEllipseInRect(CGRectMake(0,0,400,400), NULL);
SKAction *followTrack = [SKAction followPath:path
                                    asOffset:NO
                                orientToPath:YES
                                    duration:pathSpeed];

SKAction *forever = [SKAction repeatActionForever:followTrack];
SKAction *addEnemy = [SKAction runBlock:^{
    [self addChild: enemy];
}];

SKAction *enemySequence = [SKAction sequence:@[addEnemy, forever]];

[self runAction: enemySequence];
}

However, Xcode is states two issues:

No known class method for selector "addChild"

and

No known class method for selector "runAction"

I'm calling the method from GameScene.m with:

[EnemyClass enemiesLevel1]

Here's EnemyClass.h if anyone's wondering:

@interface EnemyClass : NSObject

+(void)enemiesLevel1;
+(void)enemiesLevel2;
+(void)enemiesLevel3;
+(void)enemiesLevel4;
+(void)enemiesLevel5;
+(void)enemiesLevel6;
@end

It may seem like a dumb question, but I am still new and I would greatly appreciate any help! BTW: I did import the sprite kit framework.

user3545063
  • 681
  • 8
  • 17

2 Answers2

0

Your EnemyClass doesn't have addChild or runAction methods, as it doesn't inherit from SKNode or a subclass of SKNode.

change :

@interface EnemyClass : NSObject

to

@interface EnemyClass : SKNode

Update :

Also, you have enemiesLevel1 etc defined as class methods. They need to be instance methods if you intend to sue addChild with them.

Here's a question that might help you :

What is the difference between class and instance methods?

Be sure to read all the answers, as a few of them have good info you should be aware of.

Community
  • 1
  • 1
prototypical
  • 6,731
  • 3
  • 24
  • 34
  • You also have it as a class method. Not an instance method. Do you understand what that means ? – prototypical Jul 26 '14 at 22:20
  • Updated the answer with another flaw in your code. Also, this is as far as I'm going to go with this answer, take the time to learn some of these concepts and apply them. – prototypical Jul 26 '14 at 22:27
  • 1
    One thing you may want to consider is if EnemyClass "is a" SKNode or if it "has a" SKNode. As prototypical already cited, you are doing this in a class method, and hence need a class method equivalent to addChild (self refers to a Class in this case). You may want to consider a "has a" versus "is a", which actually a bit more natural, since you really don't want to tightly couple your "model" with your graphic representation. In this case, you will need to write a wrapper method to call addChild for you. – Mobile Ben Jul 26 '14 at 22:28
0

Change

+(void)enemiesLevel1
{
    ....
}

To

-(void)enemiesLevel1
{
    ....
}

The + in front of a method means it is a class method, and the - means it is an instance method. An instance method can not be called from a class method. Please read the link prototypical provided for you so you can better understand the differences between them.

pkatsourakis
  • 1,024
  • 7
  • 20
  • It is not quite as simple as that. He will need a proper means to initialize an instance of EnemyClass. What he's sort of trying to do is create a factory method to initialize an instance through a class method. He probably really wants to do something like + (instancetype)enemyLevel1. Note I am not condoning his methodology, just guessing what his intent is. Also note how I made this singular, as EnemyClass sounds like a singular object and his code looks like it is a single object. He will still also need an initialization method as well. – Mobile Ben Jul 26 '14 at 23:43