【问题标题】:iOS SpriteKit creating and displaying objects from another classiOS SpriteKit 创建和显示来自另一个类的对象
【发布时间】:2014-06-13 00:04:34
【问题描述】:

我是 SpriteKit 的新手,我正在尝试显示在 MyScene.m 的类中创建的对象(圆形)。这就是我所拥有的:

我在 CreateNewCircle.h 中写道:

#import <SpriteKit/SpriteKit.h>

@interface CreateNewCircle : SKScene

-(void) addNewCircle;

@end

并在 .m (CreateNewCircle.m) 中:

#import "CreateNewCircle.h"

@implementation CreateNewCircle

-(void) addNewCircle {

    const CGSize size = self.frame.size;

    const CGPoint center = CGPointMake(size.width * 0.5, size.height * 0.5);

    CGRect box = CGRectMake(center.x - 40.0, center.y - 40.0, 80.0, 80.0);

    SKShapeNode *shapeNode = [[SKShapeNode alloc] init];
    shapeNode.path = [UIBezierPath bezierPathWithOvalInRect:box].CGPath;
    shapeNode.fillColor = [SKColor redColor];
    shapeNode.strokeColor = nil;
}

@end

现在我正在尝试从 MyScene.m 中调用此方法(在屏幕上显示一个圆圈):

MyScene.h:

#import <SpriteKit/SpriteKit.h>

@interface MyScene : SKScene

@end

MyScene.m:

#import "MyScene.h"
#import "CreateNewCircle.h"

@implementation MyScene {

}

-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */
        self.backgroundColor = [SKColor colorWithWhite:255 alpha:1.0];

        CreateNewCircle *circle1 = [[CreateNewCircle alloc] init];
        [circle1 addNewCircle];

        [self.scene addChild:circle1];

    }
    return self;
}

构建是好的,但是当我运行时,什么都没有出现。我尝试更改“addchild”行,例如:

[circle1 setName:@"circleOne"];

[self childNodeWithName:@"circleOne"];

但结果相同:空白屏幕。如果我直接在 MyScene.m 中编写相同的代码,就会出现圆圈,所以代码应该是正确的。有人可以帮我吗:-)?

谢谢。

【问题讨论】:

    标签: ios class sprite-kit addchild


    【解决方案1】:

    查看此示例,以便您可以对代码进行所需的更改:

    MyScene.h

    #import <SpriteKit/SpriteKit.h>
    
    @interface MyScene : SKScene
    
    @end
    

    MyScene.m

    #import "MyScene.h"
    #import "CreateNewSprite.h"
    
    @implementation MyScene
    {
        //
    }
    
    -(id)initWithSize:(CGSize)size
    {
        if (self = [super initWithSize:size])
        {
            CreateNewSprite *mySprite = [[CreateNewSprite alloc] init];
            [self addChild:mySprite];
        }
        return self;
    }
    
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        //
    }
    
    -(void)update:(CFTimeInterval)currentTime
    {
        //
    }
    
    @end
    

    CreateNewSprite.h

    #import <SpriteKit/SpriteKit.h>
    
    @interface CreateNewSprite : SKShapeNode
    
    @end
    

    CreateNewSprite.m

    #import "CreateNewSprite.h"
    
    @implementation CreateNewSprite
    
    - (instancetype)init
    {
        self = [super init];
        if (self)
        {
            SKShapeNode *shape = [SKShapeNode node];
            shape.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 20, 20, 20) cornerRadius:4].CGPath;
            shape.position = CGPointMake(100,100);
            shape.strokeColor = [SKColor redColor];
    
            [self addChild:shape];
        }
        return self;
    }
    
    @end
    

    【讨论】:

    • 最好解释一下 OP 遇到的问题,而不是仅仅转储大量代码。
    • @JesseRusak - 然后随时向他解释。我一直喜欢以身作则。
    • 好的,我尝试了一些更改,但仍然不起作用。现在我有了示例中的类:- (instancetype)init {if (self = [super init]) {................,我将 CreateNewCircle 类的子类更改为 SKSpriteNode。
    • @user3457502 - 很抱歉。我忘记了 SKShapeNode 没有 init 方法。更新后的代码向您展示了如何继承 SKShapeNode。
    猜你喜欢
    • 2018-10-28
    • 2017-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多