开发者

Moving a ball randomly

I am trying to move a ball across the screen with random coordinates . but my object moves just around x axis ! here is my code :

 [UIView beginAnimations:nil context:nil];
 [UIView setAnimationDuration:0.70];
 [UIView setAnimationRepeatCount:1000];
 [UIView setAnimationRepeatAutorever开发者_开发知识库ses:YES];

 CGFloat x = (CGFloat) random()/(CGFloat) RAND_MAX;
 CGFloat y = (CGFloat) random()/(CGFloat) RAND_MAX;

 CGPoint squarePostion = CGPointMake(y, x);
 blue3.center = squarePostion;

 [UIView commitAnimations];

EDITED :

I change my code to this but doesn't work ~:

CGFloat x = (CGFloat) random()/(CGFloat) RAND_MAX * self.view.bounds.size.width;
CGFloat y = (CGFloat) random()/(CGFloat) RAND_MAX * self.view.bounds.size.height;

CGPoint squarePostion = CGPointMake(y, x);

same happens !


You scale all your values to 0...1, so I'd expect your ball to actually move to (0,0) and stay there. Also, you might have your x any y coordinates swapped in CGPointMake. You need to scale your value to the region you want it to be in. For a random position all over your view (if blue3 is a subview of containerView - which might be self here):

 CGFloat x = (CGFloat) random()/(CGFloat) RAND_MAX * containerView.bounds.size.width;
 CGFloat y = (CGFloat) random()/(CGFloat) RAND_MAX * containerView.bounds.size.height;

 CGPoint squarePostion = CGPointMake(x, y);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜