每当我的游戏中的文字发生变化(UILabel)时,它会将我的UIImageView(一个正在跑步的人)重置到起始位置(屏幕左侧)。
它在某种意义上几乎可以作为视图控制器重置......不确定原因。 使用计时器更新所有整数和变量,但是一旦我尝试更改实际文本,所有内容都会重置为起始位置。
以下是我更新标签的NSTimer:
-(void)updateLabels{
//update lightning strikes label
strikesLeftDisplay=@"Strikes Left: ";
strikesLeftDisplay=[strikesLeftDisplay stringByAppendingString:[NSString stringWithFormat: @"%d", strikesLeft]];
lblLeft.text=strikesLeftDisplay;
}
这是我的onDidLoad:
- (void)viewDidLoad {
currentScreenX=self.view.frame.size.width;
currentScreenY=self.view.frame.size.height;
currentLevel=1;
runningSpeed=7;
lightningSpeed=0.023;
runnerImageNumber=0;
strikesLeft=3;
cloudImage.hidden=YES;
boltImage.hidden=YES;
runningMovement=[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(startRunning) userInfo:nil repeats:YES];
labelScanner=[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(updateLabels) userInfo:nil repeats:YES];
[super viewDidLoad];
}
最后,这是我的startRunning方法:
- (void)startRunning{
if(livesLeft==0){[self endGame];}
manX=man.frame.origin.x;
manY=man.frame.origin.y;
if([direction isEqualToString:@"left"] && touchX < (manX+20)){
man.center=CGPointMake(man.center.x - runningSpeed,man.center.y);
}else if([direction isEqualToString:@"right"] && touchX > (manX+22)){
man.center=CGPointMake(man.center.x + runningSpeed,man.center.y);
}
else{
direction=@"0";
}
//animate runner
if ([direction isEqualToString:@"right"]){
if(runnerImageNumber==1){UIImage *image=[UIImage imageNamed:@"run1.png"];[man setImage:image];}
if(runnerImageNumber==2){UIImage *image=[UIImage imageNamed:@"run2.png"];[man setImage:image];}
if(runnerImageNumber==3){UIImage *image=[UIImage imageNamed:@"run3.png"];[man setImage:image];}
if(runnerImageNumber==4){UIImage *image=[UIImage imageNamed:@"run4.png"];[man setImage:image];}
if(runnerImageNumber==5){UIImage *image=[UIImage imageNamed:@"run5.png"];[man setImage:image];}
if(runnerImageNumber==6){UIImage *image=[UIImage imageNamed:@"run6.png"];[man setImage:image];}
if(runnerImageNumber==7){UIImage *image=[UIImage imageNamed:@"run7.png"];[man setImage:image];}
if(runnerImageNumber==8){UIImage *image=[UIImage imageNamed:@"run8.png"];[man setImage:image];}
if(runnerImageNumber==9){UIImage *image=[UIImage imageNamed:@"run9.png"];[man setImage:image];}
runnerImageNumber++;
if(runnerImageNumber==9){runnerImageNumber=1;}
}
if ([direction isEqualToString:@"left"]){
if(runnerImageNumber==1){UIImage *image=[UIImage imageNamed:@"run10.png"];[man setImage:image];}
if(runnerImageNumber==2){UIImage *image=[UIImage imageNamed:@"run11.png"];[man setImage:image];}
if(runnerImageNumber==3){UIImage *image=[UIImage imageNamed:@"run12.png"];[man setImage:image];}
if(runnerImageNumber==4){UIImage *image=[UIImage imageNamed:@"run13.png"];[man setImage:image];}
if(runnerImageNumber==5){UIImage *image=[UIImage imageNamed:@"run14.png"];[man setImage:image];}
if(runnerImageNumber==6){UIImage *image=[UIImage imageNamed:@"run15.png"];[man setImage:image];}
if(runnerImageNumber==7){UIImage *image=[UIImage imageNamed:@"run16.png"];[man setImage:image];}
if(runnerImageNumber==8){UIImage *image=[UIImage imageNamed:@"run17.png"];[man setImage:image];}
if(runnerImageNumber==9){UIImage *image=[UIImage imageNamed:@"run18.png"];[man setImage:image];}
runnerImageNumber++;
if(runnerImageNumber==9){runnerImageNumber=1;}
}
}
答案 0 :(得分:4)
我遇到了同样的问题。确保在Interface Builder Document中使用自动布局复选标记是UNCHECKED!这解决了我的问题。希望这会有所帮助。
答案 1 :(得分:2)
通常你会看到这种类型的东西,因为你直接设置了视图的框架(或中心或bounds.size),但你的应用程序使用自动布局。
当您更改标签的文本时,标签会将自身标记为需要布局。这使得自动布局运行并根据视图的约束设置视图的帧,撤消您对这些视图的帧所做的任何更改。
不需要直接设置man.center
,而是需要修改控制人员X位置的约束常量。如果您在xib或情节提要板中创建了约束,则可以将约束连接到插座(可能在视图控制器中)以访问它。
你可能最好为你的游戏切换到SpriteKit。