Ios 点击消失

Ios 点击消失,ios,objective-c,Ios,Objective C,我是Xcode的新手,当时我正在为iPhone制作一款小游戏,只是为了好玩,我遇到了一些麻烦,以至于当某个精灵诞生时,它就消失了 简介: 所以基本上我有一个精灵在屏幕上每0.70秒产卵一次,这个精灵被设置为一个全局变量,称为“圆圈”。这些圆圈以随机的宽度和高度出现在iPhone屏幕周围的随机位置 总体 所以基本上我是想让它,当你点击精确的圆时,它只会隐藏那个球 谢谢。另外,很抱歉mods的格式太草率 以下是文件中相关的代码: GameController.m - (void)viewDidLoa

我是Xcode的新手,当时我正在为iPhone制作一款小游戏,只是为了好玩,我遇到了一些麻烦,以至于当某个精灵诞生时,它就消失了

简介: 所以基本上我有一个精灵在屏幕上每0.70秒产卵一次,这个精灵被设置为一个全局变量,称为“圆圈”。这些圆圈以随机的宽度和高度出现在iPhone屏幕周围的随机位置

总体
所以基本上我是想让它,当你点击精确的圆时,它只会隐藏那个球

谢谢。另外,很抱歉mods的格式太草率

以下是文件中相关的代码:
GameController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.gameState = GameStatePaused;
    circleVerlocity = CGPointMake(CircleSpeedX, CircleSpeedY);
    [NSTimer scheduledTimerWithTimeInterval:SpawnSpeed target:self selector:@selector(addCircle:) userInfo:nil repeats:YES];
    [NSTimer scheduledTimerWithTimeInterval:BallSpeed target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)addCircle: (NSTimer *) aTimer {

    if(gameState == GameStateRunning)
    {
        UIImageView *Circle1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"sprite-small-1.png"]];
        self.Circle = Circle1;
        CGRect rect = CGRectMake(arc4random() % (274), arc4random() % (532), 50, 50);
        [Circle1 setFrame:rect];
        [self.view addSubview:Circle1];
    }
}

-(void)gameLoop
{
    if(gameState == GameStateRunning)
    {
        Circle.center = CGPointMake(Circle.center.x + circleVerlocity.x, Circle.center.y + circleVerlocity.y);

        if(Circle.center.x > self.view.bounds.size.width || Circle.center.x < 0)
        {
            circleVerlocity.x = -circleVerlocity.x;
        }

        if(Circle.center.y > self.view.bounds.size.height || Circle.center.y < 0)
        {
            circleVerlocity.y = -circleVerlocity.y;
        }

    }else{
        if(tapToBegin.hidden)
        {
            tapToBegin.hidden = NO;
        }
    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

    if(gameState == GameStatePaused)
    {
        tapToBegin.hidden = YES;
        gameState = GameStateRunning;
    }

    if([touch view] == Circle)
    {
        Circle.hidden = YES;
    }
}
-(void)viewDidLoad
{
[超级视图下载];
self.gameState=gamestateplaused;
Circlevelocity=CGPointMake(CircleSpeedX,CircleSpeedY);
[NSTimer scheduledTimerWithTimeInterval:SpawnSpeed目标:自选择器:@selector(addCircle:)userInfo:nil repeats:YES];
[NSTimer scheduledTimerWithTimeInterval:BallSpeed目标:自选择器:@selector(gameLoop)userInfo:nil repeats:YES];
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(void)addCircle:(NSTimer*)aTimer{
如果(gameState==GameStateRunning)
{
UIImageView*Circle1=[[UIImageView alloc]initWithImage:[UIImageName:@“sprite-small-1.png”];
自圆=圆圈1;
CGRect rect=CGRectMake(arc4random()%(274),arc4random()%(532),50,50);
[Circle1 setFrame:rect];
[self.view addSubview:Circle1];
}
}
-(无效)游戏循环
{
如果(gameState==GameStateRunning)
{
Circle.center=CGPointMake(Circle.center.x+circlevelocity.x,Circle.center.y+circlevelocity.y);
if(Circle.center.x>self.view.bounds.size.width | | Circle.center.x<0)
{
circleVerlocity.x=-circleVerlocity.x;
}
if(Circle.center.y>self.view.bounds.size.height | | Circle.center.y<0)
{
circleVerlocity.y=-circleVerlocity.y;
}
}否则{
如果(点击开始隐藏)
{
tapToBegin.hidden=否;
}
}
}
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件
{
UITouch*touch=[触摸任何对象];
如果(游戏状态==游戏状态暂停)
{
tapToBegin.hidden=是;
gameState=GameStateRunning;
}
如果([触摸视图]==圆圈)
{
Circle.hidden=是;
}
}

你可以在你的圈子里使用
手势识别器。使用class
uitappesturerecognizer
可以向元素添加识别器

  circle.userInteractionEnabled = YES;


  UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideThatBall)];
  [circle addGestureRecognizer:tapGesture];


-(void)hideThatBall{
   circle.hidden = YES;
}

我会用这样的东西

啊,好的,谢谢!!您建议我将UserInAction内容放在代码中的什么位置PI通常在我创建对象时添加它,在你的例子中是圆。我还在对象creationOh之后添加了tapperstation忽略它。啊哈。是的,我在屏幕上出现了一些问题,但有些问题仍然存在,当我点击它们时,它们不会消失?这很奇怪。也许你必须添加
self.view.userInteractionEnables=YES也是。真奇怪。。它适用于meMake一些调试任务,将
NSLog
添加到方法中,然后查看发生了什么。