how to make bouncing ball application
I have been following http://www.bit-101.com/blog/?p=1798 tutorial to make bouncing ball app. But what i want to do is make 20-30 balls which can be dragged anywhere in the iphone screen on user touch and also can be dropped anywhere in the screen.So please can anyone suggest me on this -- how to do this in i phone not using cocos2d please help me out or give me some links or useful information any help will be appreciated.开发者_Python百科....
you can use timer {
pos = CGPointMake(7.0, 14.0); timer=[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(onTimer) userInfo:nil repeats:YES]; }
and call the method using selector
-(void)onTimer{ image.center = CGPointMake(image.center.x+pos.x ,image.center.y+pos.y ); if (image.center.x>320 || image.center.x<0) pos.x=-pos.x;
if (image.center.y>460 || image.center <0 )
pos.y=-pos.y;
}
by this u can move image in iphone screen and use touch move to hold that image
As you are working in mentioned code, you can write touchesMoved
method where you will handle the position of ballView
.
- (void)touchesMoved: (NSSet *)touches withEvent:(UIEvent *)event {
UITouch *anyTouch = [touches anyObject];
[self setCenter:[anyTouch locationInView:[self superView]];
}
try this.
or this updated one
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([touches count] == 1) {
UITouch *newTouch = [[touches anyObject] locationInView:[self superview]];
UITouch *lastTouch = [[touches anyObject] previousLocationInView: [self superview]];
NSLog(@"xxxxxggdfgdfgfdgdfg%f",newTouch.x);
NSLog(@"yyyyyyggdfgdfgfdgdfg%f",newTouch.y);
xDif = newTouch.x - lastTouch.x;
yDif = newTouch.y - lastTouch.y;
}
CGAffineTransform translate = CGAffineTransformMakeTranslation(xDif, yDif);
[self setTransform: CGAffineTransformConcat([self transform], translate)];
}
精彩评论