我正在创建一个简单的游戏,但遇到了一些代码问题。游戏的工作方式是有两个球。一个是敌人,一个是玩家。每5秒钟游戏将难度提高到一个新的水平。我当前的代码是here,并在下面复制:
//
// ViewController.m
// BakerBattle
//
// Created by Cole Bergmann on 2/6/13.
// Copyright (c) 2013 Cole Bergmann. All rights reserved.
//
#import "ViewController.h"
#import "SecondView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//(X speed, Y speed) vvv
pos = CGPointMake(6,5);
[three setHidden:YES];
[two setHidden:YES];
[one setHidden:YES];
[go setHidden:YES];
[level1 setHidden:YES];
[level2 setHidden:YES];
[level3 setHidden:YES];
}
-(IBAction)start {
[startbutton setHidden:YES];
[logo setHidden:YES];
[instructions setHidden:YES];
[self performSelector:@selector(three) withObject:nil afterDelay:0.5];
}
-(void)three {
[three setHidden:NO];
[self performSelector:@selector(two) withObject:nil afterDelay:0.5];
}
-(void)two {
[two setHidden:NO];
[self performSelector:@selector(one) withObject:nil afterDelay:0.5];
}
-(void)one {
[one setHidden:NO];
[self performSelector:@selector(go) withObject:nil afterDelay:0.5];
}
-(void)go {
[go setHidden:NO];
[self performSelector:@selector(gamestart) withObject:nil afterDelay:0.5];
}
-(void)gamestart {
[level1 setHidden:NO];
[three setHidden:YES];
[two setHidden:YES];
[one setHidden:YES];
[go setHidden:YES];
[logo setHidden:YES];
pos = CGPointMake(6,5);
randomMain = [NSTimer scheduledTimerWithTimeInterval:(0.03) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
[self performSelector:@selector(difficulty2) withObject:nil afterDelay:5.0];
}
-(void)onTimer {
[self checkCollision];
enemy.center = CGPointMake(enemy.center.x+pos.x,enemy.center.y+pos.y);
if (enemy.center.x > 320 || enemy.center.x < 0)
pos.x = -pos.x;
if (enemy.center.y > 480 || enemy.center.y < 0)
pos.y = -pos.y;
}
-(void)checkCollision {
if( CGRectIntersectsRect(player.frame,enemy.frame))
{
[randomMain invalidate];
[startbutton setHidden:NO];
[logo setHidden:NO];
[three setHidden:YES];
[two setHidden:YES];
[one setHidden:YES];
[go setHidden:YES];
[level1 setHidden:YES];
[level2 setHidden:YES];
[level3 setHidden:YES];
[instructions setHidden:NO];
CGRect frame = [player frame];
frame.origin.x = 137.0f;
frame.origin.y = 326.0;
[player setFrame:frame];
CGRect frame2 = [enemy frame];
frame2.origin.x = 137.0f;
frame2.origin.y = 20.0;
[enemy setFrame:frame2];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"You Lost!" message:[NSString stringWithFormat:@"Baker Cat stole your dough!"] delegate:nil cancelButtonTitle:@"I want a rematch!" otherButtonTitles:nil];
[alert show];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *myTouch = [[event allTouches] anyObject];
player.center = [myTouch locationInView:self.view];
}
//##################################-[DIFFICULTY LEVELS]-##############################################\\
-(void)difficulty2 {
[level1 setHidden:YES];
pos = CGPointMake(10,8);
[self performSelector:@selector(difficulty3) withObject:nil afterDelay:5.0];
[level2 setHidden:NO];
}
-(void)difficulty3 {
[level2 setHidden:YES];
pos = CGPointMake(14,10);
[self performSelector:@selector(win) withObject:nil afterDelay:5.0];
[level3 setHidden:NO];
}
-(void)win {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"You Won!" message:[NSString stringWithFormat:@"You defeated Baker Cat!"] delegate:nil cancelButtonTitle:@"Play Again" otherButtonTitles:nil];
[alert show];
[randomMain invalidate];
[startbutton setHidden:NO];
[logo setHidden:NO];
[instructions setHidden:NO];
[three setHidden:YES];
[two setHidden:YES];
[one setHidden:YES];
[go setHidden:YES];
[level1 setHidden:YES];
[level2 setHidden:YES];
[level3 setHidden:YES];
CGRect frame = [player frame];
frame.origin.x = 137.0f;
frame.origin.y = 326.0;
[player setFrame:frame];
CGRect frame2 = [enemy frame];
frame2.origin.x = 137.0f;
frame2.origin.y = 20.0;
[enemy setFrame:frame2];
}
@end
我想知道是否有人知道另一种方法来定义关卡。基本上,不同级别必须做的唯一事情是pos = CGPointMake(10,8);
并将我的levelText
更改为游戏所在的级别。
我现在使用的问题是,如果玩家输了,等级不会被重置。例如,如果有人在2级死亡,他们将获得死亡消息并进入标题屏幕,但在屏幕顶部显示“3级”后不久,并发送消息说玩家赢了。任何帮助将不胜感激。
答案 0 :(得分:0)
延迟后执行选择器但从不取消选择器。你有很多不同的方法名称,因此选择器取消可能不可行,因此更快的解决方案(可能有更多的副作用)是将以下代码添加到任何终止游戏状态的方法(并且可能转换为新的水平):
[NSObject cancelPreviousPerformRequestsWithTarget:self];
如果您需要通过选择器取消,请查看+cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelector object:(id)anArgument]
。