从skspritenode创建球对象时出错

时间:2015-01-14 22:06:07

标签: ios objective-c skspritenode

当我点击它时,我创建了一个球对象,以便稍后在屏幕上显示。但是,当我尝试这样做时,它给了我一个错误。不幸的是,我无法从中获取一些东西。

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

我的游戏场景中的触摸开始方法.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];


    }
}

非常感谢任何帮助。提前谢谢。

1 个答案:

答案 0 :(得分:0)

应该是:

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

在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];


    }
}