开发者

use drawn image for game on iphone

I want to use this user drawn image for the game that follows - i need it do be smaller and go to the top of the screen so that it can move left and right (to dodge objects). What do I do next?

@implementation DrawView


- (id)initWithFrame:(CGRect)frame {

self = [super initWithFrame:frame];
if (self) {
    // Initialization code.
}
return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code.
CGColorRef yellow = [[UIColor yellowColor] CGColor];
CGColorRef red = [[UIColor redColor] CGColor];
CGColorRef blue = [[UIColor blueColor] CGColor];

context = UIGraphicsGetCurrentContext();



// draw tryangle
CGContextBeginPath(context);

// give vertices
CGContextMoveToPoint(context, point.x, point.y);
CGContextAddLineToPoint(context, point.x+10, point.y);
CGContextAddLin开发者_开发问答eToPoint(context, point.x+10, point.y+10);
   CGContextAddLineToPoint(context, point.x, point.y+10);

CGContextClosePath(context);

// fill the path
CGContextSetFillColorWithColor(context, red);
CGContextFillPath(context);
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
touch=[[event allTouches]anyObject];
point= [touch locationInView:touch.view];
[self setNeedsDisplayInRect:CGRectMake(point.x, point.y, 10, 10)];


}

- (void)dealloc {
[super dealloc];
}


@end


As far as how to create a program that lets the user draw a picture, that's a bigger issue than stackoverflow questions are intended for. But let's assume you have got the user input and represented it as a set of points, colors, etc. You have the right idea to use drawRect to draw that variable content in your view.

However, rather than redraw the content every time they move, just draw the content in the same place in the view and move the whole view. For example instead of drawing a square with corners

(point.x, point.y), (point.x + 10, point.y), (point.x + 10, point.y + 10), (point.x, point.y + 10)

draw a square with corners

(0, 0), (10, 0), (10, 10), (0, 10)

Then in touchesMoved instead of the line with setNeedsDisplay, write

self.center = point;

Also to make it smaller you can set the transform property of the view. So assume the view naturally draws itself large, the way the user drew it, then to make it much smaller you can write

self.transform = CGAffineTransformMakeScale(0.1, 0.1);

That will make it one tenth the size.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜