ios - 'Player' 没有可见的@interface 声明选择器 'walkRight'

标签 ios objective-c

//播放器.m

#import "Player.h"
@implementation Player

+(id)player{

    Player *player = [Player spriteNodeWithColor:[UIColor brownColor] size:CGSizeMake(32, 32)];

    player.name = @"player";

    player.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:player.size];

    return player;

}



- (void)walkRight  {

    SKAction *incRight = [SKAction moveByX:10 y:0 duration:0];

    [self runAction:incRight];   

}


@end

//MyScene.h

#import "MyScene.h"
#import "Player.h"



@implementation MyScene

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



    SKSpriteNode *ground = [SKSpriteNode spriteNodeWithColor:[UIColor greenColor] size:CGSizeMake(self.frame.size.width, 30)];

    ground.position = CGPointMake(0, -self.frame.size.height/2 + ground.frame.size.height/2);

    ground.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:ground.size];

    ground.physicsBody.dynamic = NO;

    [self addChild:ground];



    Player *player = [Player player];

    [self addChild:player];

}

return self;

}



-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

Player *player = (Player *)[self childNodeWithName:@"player"];

[player walkRight];



 }



-(void)update:(CFTimeInterval)currentTime {

/* Called before each frame is rendered */

}



@end

我是 objective-c 的新手。好吧,我尝试构建上面的代码并得到错误“'Player'的不可见@interface声明选择器'walkRight'” 我真的不知道为什么。如果有人能帮助我,我将不胜感激。谢谢。

最佳答案

确保您已在 Player.h 文件中声明了 walkRight 方法。这应该在该文件中的接口(interface)声明之后的某个地方:

-(void)walkRight;

这允许导入 Player.h 的其他文件知道 Player.m 实现了 walkRight 方法。

关于ios - 'Player' 没有可见的@interface 声明选择器 'walkRight',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24377901/

相关文章:

ios - 发送到实例的 UITableViewCell copyWithZone 无法识别的选择器

ios - UIImage 调整纵横比填充和裁剪

ios - 选项卡栏 Controller - 在选项卡栏中添加更多项目看起来很乱

ios - 如何修改 IBOutletCollection 中标签的颜色

ios - 使用 CocoaPods 添加 CFNetwork

ios - UIImage 在滚动之前不会显示在 TableView 单元格中

android - 寻找适用于 iOS 和 Android 的基本 2D/3D 图形的跨平台方法

ios - 如何在 UISearchBar TextField 周围添加 1 像素的灰色边框

ios - 圆弧 : how to inject custom dealloc IMP in object which in turn calls original dealloc without causing malloc error

iphone - UIImagePickerController 媒体类型 kUTTypeMovie 运行时异常