0

我有一个错误我是我的应用程序他们我的球会变得非常邋遢或非常快。它是一个if语句,可以让球一直保持相同的速度吗?

像这样。

if (ball.speed < 5) {
ball.speed = 5
}

if (ball.speed > 5) {
ball.speed = 5
}

我正在使用 NSTimer 来移动球

timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(BallMovement) userInfo:nil repeats:YES];

我如何定义.speed????

4

2 回答 2

2
int minSpeed = ...;
int maxSpeed = ...;

if (ball.speed < minSpeed) {
    ball.speed = minSpeed;
} 
else if (ball.speed > maxSpeed) {
    ball.speed = maxSpeed;
}

顺便说一句,您上面的伪代码仅相当于一行:

ball.speed = 5; 

;)

于 2013-11-09T22:25:49.407 回答
1

如果 ball 是 anUIImageView那么它没有名为 ' speed' 的属性,因此您不能ball.speed在代码中使用。你可以使用这样的东西来执行动画

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ball.png"]];
CATransition *transition = [CATransition animation];
transition.duration = 10.0f;
transition.type = kCATransitionMoveIn;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[imageView.layer addAnimation:transition forKey:nil];
于 2013-11-10T12:09:01.707 回答