What is the logic to move a paddle left and right automatically on the iPhone?
I am just developing a simple iPhone application where a paddle moves left and right automatically.
My logic:
// First to determine the CGPoint of paddle
CGPoint detectXY = CGPointMake(paddle.center.x ,paddle.center.y);
// 开发者_JAVA技巧Second to determine the velocity of paddle
CGPoint paddleVelocity = CGPointMake(1,1);
// Adding velocity with X,Y cordinate
paddle.center = CGPointMake(paddle.center.x + paddleVelocity.x,
paddle.center.y + paddleVelocity.y);
But the paddle is not moving...
Any ideas?
If you created the UIImageView in Interface Builder, make sure you connect it up to your IBOutlet or it won't respond to you setting the .center property. This has bitten me many times.
my updated code
- (void)viewDidLoad {
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:0.9 target:self selector:@selector(game) userInfo:nil repeats:YES];
}
-(void)game{
CGPoint imgVelocity = CGPointMake(20,1);
CGPoint detectXY = CGPointMake(imgViewPaddle.frame.origin.x , imgViewPaddle.frame.origin.y);
int x1= (int)detectXY.x;
CGRect rect=imgViewPaddle.frame;
if(x1>=0)
{
imgViewPaddle.frame=CGRectMake(imgViewPaddle.frame.origin.x-imgVelocity.x, imgViewPaddle.frame.origin.y, imgViewPaddle.frame.size.width, imgViewPaddle.frame.size.height);
}
else //<0
{
imgViewPaddle.frame=CGRectMake(imgViewPaddle.frame.origin.x+imgVelocity.x, imgViewPaddle.frame.origin.y, imgViewPaddle.frame.size.width, imgViewPaddle.frame.size.height);
}
rect=imgViewPaddle.frame;
}
精彩评论