2010-11-25 64 views
0

嗨我想在iphone上實現一個非常簡單的遊戲。我有一張圖片和一段文字。如果用戶在圖像和文本之間劃一條線,那麼它說「正確」(iphone)一個非常簡單的iPhone遊戲

現在我在想,只要用戶觸摸屏幕,我就會得到該觸摸點的位置(如X,Y) (哪種方法來獲取位置?),然後我會在那裏畫一個黑點。如果用戶持續平穩地觸摸屏幕,則會形成一條線。最後,我只需要判斷線條的起點位於圖像內,終點位於文本內。

我不知道我的想法是否是實現這種遊戲的正確方法。如果是如何獲取觸摸點的座標並在該特定位置繪製黑點?

感謝您的幫助!

+0

:s/Iphone/iPhone。而且,實際上沒有必要在「(iphone)」前加上問題的標題。這就是標籤的用途。真。 – 2010-11-25 03:49:15

回答

1

你可能會得到觸摸座標在您的視圖控制器的touchesMoved

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint touch_point = [touch locationInView:self.view]; 
    NSLog(@"x: %.0f y: %.0f", touch_point.x, touch_point.y); 
} 

劃清界線,你必須添加視圖並繪製線drawRect

- (void)drawRect:(CGRect)rect { 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetLineWidth(context, 1.0); 
    CGContextSetStrokeColorWithColor(context, [UIColor orangeColor].CGColor); 

    // ... 

    CGContextMoveToPoint(context, one_x, one_y); 
    CGContextAddLineToPoint(context, two_x, two_y); 
    CGContextStrokePath(context); 

    // ... 
} 

其中one_x one_y two_x two_x是兩點的座標..

0

這正是iPhone touch委託的工作原理。它調用touchesBegan:,touchesMoved:touchesEnded:。要繪製線條,您需要下列代碼:

CGContextRef context = UIGraphicsGetCurrentContext(); 
[current set]; 
CGContextSetLineWidth(context, 4.0f); 

for (int i = 0; i < (self.points.count - 1); i++) 
{ 
    CGPoint pt1 = POINT(i); 
    CGPoint pt2 = POINT(i+1); 
    CGContextMoveToPoint(context, pt1.x, pt1.y); 
    CGContextAddLineToPoint(context, pt2.x, pt2.y); 
    CGContextStrokePath(context); 
} 

並且在此代碼的幫助下,您應該做好準備。如果您需要任何幫助,只需對此發表評論或提出其他問題。

CGPoint pt = [[touches anyObject] locationInView:self]; 
[self.points addObject:[NSValue valueWithCGPoint:pt]]; 
[self setNeedsDisplay];