没有已知的选择器'generatorWithWorld:'的类方法

时间:2014-07-24 21:10:53

标签: ios xcode class methods

我正在尝试在xcode上创建一个平台游戏,这是我的第一个项目,我遇到了这个错误,它说“没有已知的选择方法'generatorWithWorld:'”,我也得到了错误“'HSWorldGenerator'没有可见的@interface'声明选择器'populate'”

这是MyScene.h的代码:

//
//  MyScene.h
//  The Adventure of Colin The Sloth
//

//  Copyright (c) 2014 SunnersCorp. All rights reserved.
//

**#import <SpriteKit/SpriteKit.h>
#import "HSWorldGenerator.h"**

@interface MyScene : SKScene

@end

MyScence.m:

//
//  MyScene.m
//  The Adventure of Colin The Sloth
//
//  Created by Harpreet Sunner on 24/07/2014.
//  Copyright (c) 2014 SunnersCorp. All rights reserved.
//

#import "MyScene.h"
#import "HSSloth.h"


@interface MyScene()
@property BOOL isStarted;
@property BOOL isGameOver;
@end

@implementation MyScene
{
    HSSloth *Sloth;
    SKNode *world;
    HSWorldGenerator *generator;
}

-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */
        self.anchorPoint = CGPointMake (0.5, 0.5);
        self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];

        world = [SKNode node];
        [self addChild:world];

        generator = [HSWorldGenerator generatorWithWorld:world]; **THIS IS WHERE I GET THE FIRST ERROR**
        [self addChild:generator];
        [generator populate]; **THIS IS WHERE I GET THE SECOND ERROR**


        Sloth = [HSSloth Sloth];
        [world addChild:Sloth];
    }

    return self;
}

- (void)start
{
    self.isStarted = YES;
    [Sloth start];
}

-(void)clear
{
    NSLog(@"clear method called");
}

- (void)gameOver
{
    NSLog(@"gameOver method called");
}

- (void)didSimulatePhysics
{
    [self centerOrNode:Sloth];
}

- (void)centerOrNode:(SKNode *)node
{
    CGPoint postionInScene = [self convertPoint:node.position fromNode:node.parent];
    world.position = CGPointMake(world.position.x - postionInScene.x, world.position.y);
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if (!self.isStarted)
        [self start];
    else if (self.isGameOver)
        [self clear];
    else
        [Sloth jump];

}

-(void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */
}

@end

HSWorldGenerator.h:

//
//  HSWorldGenerator.h
//  The Adventure of Colin The Sloth
//
//  Created by Harpreet Sunner on 24/07/2014.
//  Copyright (c) 2014 SunnersCorp. All rights reserved.
//

#import <SpriteKit/SpriteKit.h>


@interface HSWorldGenerator : SKNode

+ (id)generatorWithWorld:(SKNode *)world;
- (void)populate;
- (void)generate;


@end

HSWorldGenerator.m:

//
//  HSWorldGenerator.m
//  The Adventure of Colin The Sloth
//
//  Created by Harpreet Sunner on 24/07/2014.
//  Copyright (c) 2014 SunnersCorp. All rights reserved.
//

#import "HSWorldGenerator.h"

@interface HSWorldGenerator ()
@property double currentGroundX;
@property double currentObstacleX;
@property SKNode *world;
@end

@implementation HSWorldGenerator

+ (id)generatorWithWorld:(SKNode *)world
{
    HSWorldGenerator *generator = [HSWorldGenerator node];
    generator.currentGroundX = 0;
    generator.currentObstacleX = 400;
    generator.world = world;
    return generator;
}

- (void)populate
{
    for (int i = 0; 1 <3; i++)
        [self generate];
}

- (void)generate
{
    SKSpriteNode *ground = [SKSpriteNode spriteNodeWithColor:[UIColor greenColor] size:CGSizeMake(self.scene.frame.size.width, 100)];
    ground.position = CGPointMake(self.currentGroundX, -self.scene.frame.size.height/2 + ground.frame.size.height/2);
    ground.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:ground.size];
    ground.physicsBody.dynamic = NO;
    [self.world addChild:ground];

    self.currentGroundX += ground.frame.size.width;

    SKSpriteNode *obstacle = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(40, 70)];
    obstacle.position = CGPointMake(self.currentObstacleX, ground.position.y + ground.frame.size.height/2 + obstacle.frame.size.height/2);
    obstacle.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:obstacle.size];
    obstacle.physicsBody.dynamic = NO;
    [self.world addChild:obstacle];

    self.currentObstacleX += 250;

}
@end

有谁能告诉我我做错了什么以及如何解决这个问题,我会非常感激。

谢谢!

1 个答案:

答案 0 :(得分:0)

检查#import HSWorldGenerator.h到MyScene.h文件的位置。 错误告诉您它不知道在何处找到类方法。 看起来您尝试在MyScene.h中导入标题的位置,您可能会有额外的注释掉该行。