- 在.h文件中设置一个int,可能是
int balloonMovement
- 将所有气球图像链接到大括号之间的 .h 作为 IBOutlets
即IBOutlet UIImageView *balloon1;
IBOutlet UIImageView *balloon2;
- 设置另一个
int randomPosition 并使用随机数生成器设置气球在屏幕上的位置,可能是当用户单击开始时,或者发生某些事情时。
你可以在 viewDidLoad 中有这个:
RandomPosition = arc4random()%248; //the number of possibilities
RandomPosition = RandomPosition + 36; //the new position
balloon1.center = CGPointMake(RandomPosition, RandomPosition); //make position
这会使它们随机出现在屏幕上。
然后,为了让它们向上浮动,设置int upMovement; 和NSTimer *movement;,并每隔一段时间设置一次,它们就会向上移动。
使用以下代码:
movement = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(moving) userInfo:nil repeats:YES];
这表示,每 0.05 秒,我们要激活一次运动。
然后在.h中创建一个气球运动的方法——-(void)balloonMovement;
并在.m中填写其信息 -->
-(void)balloonMovement {
balloon1.center = CGPointMake(balloon1.center.x, balloon1.center.y + upMovement);
}
另一个用于实际移动气球,并设置您希望它们向上浮动的条件,即用户剪断绳子?
-(void)balloonMoving {
upMovement = upMovement + 0.1 //this will make it gradually faster
if (//user cuts string code) {
[self balloonMovement];
}
}
这应该可行。显然它非常简单。您将不得不对其进行自定义。但这应该为您提供基础知识。希望它有所帮助。
还有一件事**
要让它们在屏幕上移动时改变位置,您必须设置一个 int sideMovement,并以与 upMovement 相同的方式添加它。但我会把它留给你去弄清楚。