繁体   English   中英

如何调用超类表单子类SpriteKit的方法

[英]How to call a method of super class form subclass SpriteKit

我将SKSpriteNode子类SKButtonSKButton.h创建SKButton.h

@interface SKButton : SKSpriteNode

现在我想通过SKButton.m的方法更改按钮图像

self = [SKSpriteNode spriteNodeWithImageNamed:image];

这里的自我是SKButton但它给了我错误

无法在init系列中的方法之外分配给'self'不兼容的指针类型从'SKSpriteNode *'分配'SKButton *'

我也在SKButton.m试过这个

self = [self spriteNodeWithImageNamed:image];

它给了我错误

'SKButton'没有可见的@interface声明选择器'spriteNodeWithImagedNamed:'

spriteNodeWithImageNamed:是一类方法SKSpriteNode ,使用所提供的图像返回一个新SKSpriteNode。 基本上,你要用这条线做什么 -

self = [SKSpriteNode spriteNodeWithImageNamed:image];

将当前对象更改为新对象 - 您无法做到。 即使你可以,它也将是一个新的SKSpriteNode ,而不是一个新的SKButton

你需要做的是操纵节点的texture属性 -

self.texture=[SKTexture textureWithImageNamed:image];

在这一行:

self = [SKSpriteNode spriteNodeWithImageNamed:image];

你是一个向下转换SKSpriteNodeSKButton为这是不正确SKSpriteNode实例没有通过SKButton提供的额外实施。

由于SKButton是一个SKSpriteNode子类,它包含了它的所有功能,所以你可以这样做:

- (void)buttonClicked {
   [self setTexture:[SKTexture textureWithImageNamed:"buttonClicked.png"]];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM