-1

I created a ball object to later show on screen when I tap it. However when I try to do that, it gives me an error. Unfortunately i can't make something out of it.

ballClass.h:

@import SpriteKit;
#import "ballClass.h"

@implementation ballClass

+ (SKSpriteNode*)ballNode   {

    SKSpriteNode* node = [SKSpriteNode spriteNodeWithImageNamed:@"Ball"];
    return node;
}
@end

ballClass.m:

#import <Foundation/Foundation.h>

@interface ballClass : NSObject

+ (SKSpriteNode*)ballNode;

@end

My touchesBegan method in my game scene.m:

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */

for (UITouch *touch in touches) {
    CGPoint location = [touch locationInNode:self];

    SKSpriteNode *ball = [ballClass ballNode];
    ball.position = location;
    [self addChild:ball];


    }
}

Any help would be greatly appreciated. Thanks in advance.

Sievajet
  • 3,443
  • 2
  • 18
  • 22

1 Answers1

0

It should be:

ballClass.h

@import SpriteKit;
#import <Foundation/Foundation.h>

@interface ballClass : NSObject
+ (SKSpriteNode*)ballNode;
@end

ballClass.m

#import "ballClass.h"

    @implementation ballClass
    + (SKSpriteNode*)ballNode   {

        SKSpriteNode* node = [SKSpriteNode spriteNodeWithImageNamed:@"Ball"];
        return node;
    }
    @end

In the YourScene.m:

//at the beginning of the file
#import ballClass.h

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];

        SKSpriteNode *ball = [ballClass ballNode];
        ball.position = location;
        [self addChild:ball];


    }
}
Whirlwind
  • 14,286
  • 11
  • 68
  • 157
  • Hi! Thanks for your reaction. I only pasted the touchesbegan method to make my post shorter. In my originl code i already imported all mij headers and frameworks. So basically the code you wrote on this post is really the same as mine. Do you have an idea what else might be the cause of my problem? – Gill Lambrigts Jan 16 '15 at 09:20
  • It is not the same if you look better. I suppose there is a typo in your code. Look at your post, you are importing ballClass.h in ballClass.h file. Basically what is defined in your ballClass.h should be defined in your ballClass.m file and vice versa. Also you have to import ballClass.h in your scene file where touchesBegan is defined. I produced good results with my example, try just copy and paste as it is. – Whirlwind Jan 16 '15 at 11:35
  • Hi, Whirlwind. I reviewed my post and noticed that I must have switched them around. However the error that seemed to keep me from running it properly, was that I imported SpriteKit inside ballClass.m, instead of ballClass.h. Thanks a lot! Such a little mistake, yet such a big impact. – Gill Lambrigts Jan 16 '15 at 18:41