在iPhone上自动左右移动球拍的逻辑是什么?

时间:2009-10-20 12:35:01

标签: iphone

我正在开发一个简单的iPhone应用程序,其中一个球拍自动左右移动。

我的逻辑:

// First to determine the CGPoint of paddle
CGPoint detectXY = CGPointMake(paddle.center.x ,paddle.center.y);

// 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);

但是桨没有移动......

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

如果您在Interface Builder中创建了 UIImageView ,请确保将其连接到您的IBOutlet,否则它将不会响应您设置 .center 属性。这已经困扰了我很多次。

答案 1 :(得分:0)

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;
}