【问题标题】:Calling Custom Method Inside SKSpriteNode Not Working在 SKSpriteNode 中调用自定义方法不起作用
【发布时间】:2015-11-08 21:30:39
【问题描述】:

我有一个自定义的 SKSpriteNode 对象,并想在其中调用自定义方法“设置默认值”。

#import <SpriteKit/SpriteKit.h>

@interface Platform : SKSpriteNode

- (instancetype)initWithDynamicPlatform;
- (void)setDefaults;

@end

在.m文件中

#import "Platform.h"

@implementation Platform

- (instancetype)initWithImageNamed:(NSString *)name {
if (self == [super initWithImageNamed:name]) {
    NSLog(@"Initiated Platform");
}
return self;
}

- (instancetype)initWithDynamicPlatform {

if (self == [super initWithImageNamed:@"Platform2"]) {
    NSLog(@"Initiated Platform");
}

[self setDefaults];

return self;
}

- (void)setDefaults {

/**
 * Set the name
 */

self.name = @"Platform";

/**
 * Set the effect of gravity on the platform
 */

self.physicsBody.affectedByGravity = NO;
self.physicsBody.dynamic = NO;


 }

 @end

问题是我无法从 SKScene 文件访问自定义方法。

- (void)loadDynamicPlatform {

SKSpriteNode *spritePlatform = [[Platform alloc] initWithDynamicPlatform];    

[spritePlatform setDefaults];

[self addChild:spritePlatform];
[self movePlatform:spritePlatform];

}

我收到错误消息

"/Users/****/Desktop/Apps/****/****/GameScene.m:142:21: 'SKSpriteNode' 没有可见的@interface 声明选择器 'setDefaults'"

知道为什么我无法访问它。我确定我设置正确。

【问题讨论】:

  • 你在#import "Platform.h" 中GameScene.m 吗?
  • 是的,在 GameScene.m 文件中

标签: ios objective-c sprite-kit skspritenode


【解决方案1】:

您必须声明一个“平台”变量或将“SKSpriteNode”转换为“平台”:

Platform *spritePlatform = [[Platform alloc] initWithDynamicPlatform];    

[spritePlatform setDefaults];

【讨论】:

  • 啊,谢谢。我现在更改了代码,它可以工作了。
  • 太好了。乐于助人
猜你喜欢
  • 1970-01-01
  • 2018-07-10
  • 2011-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多