我正在开发一款针对iOS的游戏,我想知道为什么倒计时不起作用。我希望在这个游戏中每个级别有一定数量的移动,我希望每次用户点击其中一个拼图时减去1个移动。
这是我设置的一些代码:
我在标题文件中设置了两个整数:int movesRemaining;
和int moved;
显示移动量的SKLabelNode
:
moves = [SKLabelNode labelNodeWithFontNamed:@"DIN Condensed"];
moves.position = CGPointMake(110, -15);
moves.text = [NSString stringWithFormat:@"MOVES %d", movesRemaining];
moves.fontSize = 25;
moves.zPosition = 2.0;
moves.fontColor = [SKColor whiteColor];
[menuBar addChild:moves];
计算得分的方法:
-(void)moves {
movesRemaining = 4;
movesRemaining = (movesRemaining - moved);
}
每次触摸拼图时告诉moveRemaining减1的代码移动:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:@"sq1"]) {
[_BS1 removeFromParent];
moved = 1;
}
if ([node.name isEqualToString:@"sq2"]) {
[_BS2 removeFromParent];
moved = 1;
}
if ([node.name isEqualToString:@"sq3"]) {
[_BS3 removeFromParent];
moved = 1;
}
if ([node.name isEqualToString:@"sq4"]) {
[_BS4 removeFromParent];
moved = 1;
}
if ([node.name isEqualToString:@"sq5"]) {
[_BS5 removeFromParent];
moved = 1;
}
我的问题是我哪里出错,每次点击拼图时它都不减1?每次用户触摸拼图时,如何正确地进行moveRemaining减1?
如果需要更多信息,请告诉我。
感谢。
答案 0 :(得分:0)
将movesRemaining = 4;
移至init
方法。
然后将[self moves];
添加到-(void)touchesBegan
的末尾。
答案 1 :(得分:0)
在移动的方法中更改它以便声明move = moved - 1;然后,这将添加滚动效果
答案 2 :(得分:0)
moves = [SKLabelNode labelNodeWithFontNamed:@"DIN Condensed"];
moves.position = CGPointMake(110, -15);
moves.text = [NSString stringWithFormat:@"MOVES %d", movesRemaining];
moves.fontSize = 25;
moves.zPosition = 2.0;
moves.fontColor = [SKColor whiteColor];
movesRemaining = 4; //Move initial movesRemaining value here.
[menuBar addChild:moves];
-(void)moves { //Subtract number of moves everytime the user moves.
movesRemaining = (movesRemaining - moved);
}
答案 3 :(得分:0)
问题是它没有被更新。如何解决:
润色: - (void)touchesBegan:(NSSet *)触及withEvent:(UIEvent *)事件{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:@"sq1"]) {
movesRemaining = movesRemaining - 1;
[_BS1 removeFromParent];
}
取出initWithSize方法中的内容并将其添加到-(void)update:(CFTimeInterval)currentTime
方法中:
if (!moves) {
moves = [SKLabelNode labelNodeWithFontNamed:@"DIN Condensed"];
moves.position = CGPointMake(110, -15);
moves.fontSize = 25;
moves.zPosition = 2.0;
moves.fontColor = [SKColor whiteColor];
[menuBar addChild:moves];
}
//得分 moves.text = [NSString stringWithFormat:@" MOVES%d",moveRemaining];
这将修复您的代码。然后它会在每次点击时更新,就像你想要的那样。