2014-09-02 174 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"];