2014-09-02 178 views
0

对不起,我不确定这个问题的术语,但我希望你能理解并帮助我。 (另外如果术语是错误的,请将其引用以供将来参考..谢谢!)如何在iPhone上设置参数

我想为我的iOS游戏设置参数,以便当您单击屏幕的任一侧时,玩家将转向该侧。

我的作品让玩家的举动,虽然它被设置到整个屏幕,因此只能去一个方向的代码...

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 


    X = -7; 
    Player.image = [UIImage imageNamed:@"Player Left.png"]; 
} 

有什么需要补充,以便只有当用户触摸屏幕左侧执行此代码。

预先感谢,我希望你能理解。 我很抱歉有任何错误的术语。

回答

3

检查触摸点在屏幕的左侧.. e.g -

if ([touches count] == 1) { 
    // one finger 
    CGPoint touchPoint = [[touches anyObject] locationInView:self.view]; 
    if (touchPoint.x <= [UIScreen mainScreen].bounds.size.width*0.50) { 
      Player.image = [UIImage imageNamed:@"Player Left.png"]; 
    }else{ 
     Player.image = [UIImage imageNamed:@"Player Right.png"]; 
    } 
} 
1

通过定义CGRect只要定义你的“反应性”区域。然后尝试使用CGRectContainsPoint()来定义触摸是否在里面。类似的东西(未测试):

CGRect reactable = CGRectMake(0, 0, 10, 10); // A square of 10 px 
UITouch *touch = [[event allTouches] anyObject]; 
CGPoint touchLocation = [touch locationInView:self]; 

if (CGRectContainsPoint(reactable, touchLocation) 
    Player.image = [UIImage imageNamed:@"Player Left.png"];