现在我的程序中球在Y轴上上下跳动。我想要的是球只能向下移动。当它到达屏幕的底部时,我希望它重新出现在顶部并再次开始。不断从上到下移动。
@implementation BounceViewController
@synthesize ball;
-(void) bounce{
ball.center = CGPointMake(ball.center.x+position.x, ball.center.y+position.y); {
if(ball.center.y>450 ||ball.center.y <0)
position.y = -position.y; }
}
- (void)viewDidLoad {
position = CGPointMake(0, 10);
[NSTimer scheduledTimerWithTimeInterval:.05 target:self selector:@selector(bounce) userInfo:nil repeats:YES];
[super viewDidLoad];
}
- (void)viewDidUnload
{
[self setBall:nil];
[super viewDidUnload];
}
@end
答案 0 :(得分:0)
替换了行
if(ball.center.y>450 ||ball.center.y <0)
position.y = -position.y;
与
if(ball.center.y>450 ||ball.center.y <0)
ball.center = CGPointMake(ball.center.x+position.x, position.y);