开发者

(iphone) a very simple Iphone game

Hi I want to implement a very simple game on iphone. I have a image and a text. If a user draw a line between the image and the text then it says "correct"

Now what I am thinking is that whenever a user touch the screen I will get the location (like X,Y) of that touching spot ( which method to call to get the location? ) and then I will draw a black开发者_JAVA百科 dot there. If the user continuously and smoothly touch the screen then a line will form. Finally I will only need to judge whether the starting point of the line locates within the image and the ending point locates within the text.

I dont know whether my idea is a correct way to implement this kinda game. If it is how to get coordinate of touching spot and draw a black dot in that specific location?

Thanks for helping!


You may get the touch coordinates in touchesMoved of your view controller:

- (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);
}

To draw the line, you have add a view and draw line in 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);

    // ...
}

where one_x one_y two_x two_x are coordinates for two points ..


This is exactly how the iPhone touch delegation works. It calls touchesBegan:, touchesMoved: and touchesEnded:. To draw the lines, you need this code:

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);
}

And with the help of this code, you should be ready to make it. If you need any help, just comment on this or make another question.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜