我是一个试图学习iOS开发的python / java程序员。我在开源游戏中工作(出于学习目的 - 代码可以在这里找到:https://github.com/nathanborror/FlapFlap)。我尝试以编程方式在重启页面上添加一个按钮,如果用户输了游戏,用户就会登陆该按钮。这是我的代码,它不会给我任何错误,但按钮只是没有出现。请注意,我已经注释了开源中提供的代码。
#import "NewGameScene.h"
#import "GameScene.h"
@implementation NewGameScene {
SKSpriteNode *_button;
UIButton *myButton;
}
- (id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size]) {
[self setBackgroundColor:[SKColor colorWithRed:.61 green:.74 blue:.86 alpha:1]];
myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton addTarget:self action:@selector(myMethod) forControlEvents:UIControlEventTouchDown];
[myButton setTitle:@"Start!" forState:UIControlStateNormal];
myButton.frame = CGRectMake(128, 32, self.size.width/2, self.size.height/10);
[self.view addSubview:myButton];
/*
_button = [SKSpriteNode spriteNodeWithColor:[SKColor colorWithWhite:1 alpha:1] size:CGSizeMake(128, 32)];
[_button setPosition:CGPointMake(self.size.width/2, self.size.height/10)];
[self addChild:_button];
*/
}
return self;
}
-(void) myMethod{
SKTransition *transition = [SKTransition doorsCloseHorizontalWithDuration:.4];
GameScene *game = [[GameScene alloc] initWithSize:self.size];
[self.scene.view presentScene:game transition:transition];
}
/*
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
SKTransition *transition = [SKTransition doorsCloseHorizontalWithDuration:.4];
GameScene *game = [[GameScene alloc] initWithSize:self.size];
[self.scene.view presentScene:game transition:transition];
}
*/
@end
答案 0 :(得分:0)
试试这个......
myButton = [UIButton alloc]initWithFrame:CGRectMake(128, 32, 100, 100);
[myButton addTarget:self action:@selector(myMethod) forControlEvents:UIControlEventTouchDown];
[myButton setTitle:@"Start!" forState:UIControlStateNormal];
[self.view addSubview:myButton];
查看它是否出现在您的视图中。