点击即可消失

时间:2014-03-18 00:29:39

标签: ios objective-c

我是Xcode的新相对论,我正在为iPhone制作一个小巧的有趣游戏,而且当它产生某个精灵消失时,我会遇到一些麻烦。

简介: 所以基本上我每隔0.70秒就会在屏幕上产生一个sprite,并且sprite被设置为一个名为" Circle"的全局IBOutlet变量。这些圆圈以随机宽度和高度在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;
    }
}

1 个答案:

答案 0 :(得分:0)

您可以在圈子中使用GestureRecognizer。使用类UITapGestureRecognizer,您可以为元素添加识别器,

  circle.userInteractionEnabled = YES;


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


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

我会用这样的东西