如何让 iPhone 中的球随机下落
带着充分的赞美,我首先要声明我是 iPhone 编程的新手。因此,我想寻求有关如何实现“一个”球从iPhone屏幕顶部随机落下的动画的解决方案的帮助。球的属性是它有不同的大小、速度并且也可以弹跳,即还涉及碰撞。
我试图仅使用球的图像,并通过 Interface Builder(IB) 添加了球图像,并将 UIImageView 连接到 IB 中的文件所有者,我一直在尝试通过将 IBOutlet 关键字添加到以编程方式访问图像,但没有成功.h 文件(请参阅下面的 .h 和 .m 代码)。 当我运行代码时,我得到的只是图像出现并立即消失。我怎样才能让这个球随机下落并有一些弹跳或碰撞。
准确地说,我希望拥有像 iPhone 游戏《落球》中那样的动画……球落下后仍会反弹。
预先感谢您的帮助 干杯!!
***The .h file***
@interface TestBallsViewController : UIViewController {
IBOutlet UIImageView *ball;
CGPoint ballMovement;
double size;
double speed;
}
@property(nonatomic, retain)IBOutlet UIImageView *ball;
- (void)initializeTimer;
- (void)animateBall:(NSTimer *)theTimer;
**The .m file**
@implementation TestBallsViewController
@synthesize ball;
- (void)dealloc {
[ball release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
ballMovement = CGPointMake(2,2);
[self initializeTimer];
}
- (void)initializeTimer
{
float theInterval = 0.3;
[NSTimer scheduledTimerWithTimeInterval:theInterval target:self
selector:@selector(animateBall:) userInfo:nil repeats:YES];
}
- (void)animateBall:(NSTimer *)theTimer
{
ball.center = CGPointMake(ball.center.x+ballMovement.x,
ball.center.y+ballMovement.y);
int startX =arc4random()%100;
int endX = arc4random()%320;
ball.frame= CGRectMake(startX, -100, 15.0 * size, 15 * size);
ball.frame = CGRectMake(endX, 480, 15.0 * size, 15.0 * size);
if(ball.center.x > 300 || ball.center.x < 20) ballMovement.x =
- ballMovement.x;
if(ball.center.y > 440 || ball.center.y < 40) ballMovement.y =
- ballMovement.y;
}
With full compliments, may I start by declaring that I am new to iPhone programming. Thus I will like to ask for help on the solution on how to implement an animation where "a" ball falls randomly from the top of the iPhone screen. The ball's attribute is such that it has different sizes, speed and can bounce too i.e there is collision also involved.
I am trying to use just an image of the ball and I added the ball image through Interface Builder(IB) and connected the UIImageView to files owner in IB and I have been trying without success to access the image programatically by adding the IBOutlet keyword to the .h file(please see the .h and .m code below).
When I run the code all I get is the image appearing and instantly disappearing. how do I get this ball to fall randomly with some bounce or collision.
To be precise I wish to have the kind of animation such as that obtained in the iPhone game falling balls...where the balls fall and still bounce off.
Thanks in advance for your help
CHEERS!!
***The .h file***
@interface TestBallsViewController : UIViewController {
IBOutlet UIImageView *ball;
CGPoint ballMovement;
double size;
double speed;
}
@property(nonatomic, retain)IBOutlet UIImageView *ball;
- (void)initializeTimer;
- (void)animateBall:(NSTimer *)theTimer;
**The .m file**
@implementation TestBallsViewController
@synthesize ball;
- (void)dealloc {
[ball release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
ballMovement = CGPointMake(2,2);
[self initializeTimer];
}
- (void)initializeTimer
{
float theInterval = 0.3;
[NSTimer scheduledTimerWithTimeInterval:theInterval target:self
selector:@selector(animateBall:) userInfo:nil repeats:YES];
}
- (void)animateBall:(NSTimer *)theTimer
{
ball.center = CGPointMake(ball.center.x+ballMovement.x,
ball.center.y+ballMovement.y);
int startX =arc4random()%100;
int endX = arc4random()%320;
ball.frame= CGRectMake(startX, -100, 15.0 * size, 15 * size);
ball.frame = CGRectMake(endX, 480, 15.0 * size, 15.0 * size);
if(ball.center.x > 300 || ball.center.x < 20) ballMovement.x =
- ballMovement.x;
if(ball.center.y > 440 || ball.center.y < 40) ballMovement.y =
- ballMovement.y;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)