如何让球在iPhone中随机落下
首先,请允许我向大家致意,我宣布我是iPhone编程新手。因此,我想就如何实现“a”球从iPhone屏幕顶部随机落下的动画的解决方案寻求帮助。球的属性是这样的,它有不同的大小,速度,也可以反弹,即有碰撞也涉及 我试图只使用球的一个图像,我通过Interface Builder(IB)添加了球图像,并将UIImageView连接到IB中的文件所有者,我一直试图通过将IBOutlet关键字添加到.h文件(请参见下面的.h和.m代码)以编程方式访问图像,但未成功。 当我运行代码时,我得到的只是图像的出现和瞬间消失。我如何让这个球在反弹或碰撞的情况下随机落下 确切地说,我希望有一种像iPhone游戏《落球》中那样的动画……球落下后仍会反弹 提前谢谢你的帮助 干杯如何让球在iPhone中随机落下,iphone,cocoa-touch,uiimageview,Iphone,Cocoa Touch,Uiimageview,首先,请允许我向大家致意,我宣布我是iPhone编程新手。因此,我想就如何实现“a”球从iPhone屏幕顶部随机落下的动画的解决方案寻求帮助。球的属性是这样的,它有不同的大小,速度,也可以反弹,即有碰撞也涉及 我试图只使用球的一个图像,我通过Interface Builder(IB)添加了球图像,并将UIImageView连接到IB中的文件所有者,我一直试图通过将IBOutlet关键字添加到.h文件(请参见下面的.h和.m代码)以编程方式访问图像,但未成功。 当我运行代码时,我得到的只是图像的出
***.h文件***
@接口TestBallsViewController:UIViewController{
IBUIImageView*球;
球点运动;
双倍大小;
双速;
}
@属性(非原子,保留)IBUIImageView*ball;
-(无效)初始化者;
-(void)animateBall:(NSTimer*)计时器;
**.m文件**
@TestBallsViewController的实现
@合成球;
-(无效)解除锁定{
[球释放];
[super dealoc];
}
-(无效)viewDidLoad{
[超级视图下载];
球的移动=CG的定点(2,2);
[自初始化程序];
}
-(无效)初始值设定项
{
浮动区间=0.3;
[N计时器计划时间间隔:时间间隔目标:self
选择器:@selector(animateBall:)userInfo:nil repeats:YES];
}
-(void)animateBall:(NSTimer*)计时器
{
ball.center=CGPointMake(ball.center.x+ballmotation.x,
球。中心。y+球移动。y);
int startX=arc4random()%100;
int endX=arc4random()%320;
ball.frame=CGRectMake(startX,-100,15.0*尺寸,15*尺寸);
ball.frame=CGRectMake(endX,480,15.0*尺寸,15.0*尺寸);
如果(ball.center.x>300 | | ball.center.x<20)ballMovement.x=
-ballmotation.x;
如果(ball.center.y>440 | | ball.center.y<40)ballMovement.y=
-ballmotation.y;
}
-(void)animateBall:(NSTimer*)计时器
{
[UIView beginAnimations:nil上下文:nil];
[UIView setAnimationDuration:1.0];
ball.center=CGPointMake(ball.center.x+ballmotation.x,
球。中心。y+球移动。y);
int startX=arc4random()%100;
int endX=arc4random()%320;
ball.frame=CGRectMake(startX,-100,15.0*尺寸,15*尺寸);
ball.frame=CGRectMake(endX,480,15.0*尺寸,15.0*尺寸);
如果(ball.center.x>300 | | ball.center.x<20)ballMovement.x=
-ballmotation.x;
如果(ball.center.y>440 | | ball.center.y<40)ballMovement.y=
-ballmotation.y;
[UIView委员会];
}
Crystal Skull非常感谢您编辑代码……我现在可以在屏幕上看到图像,但它仍然没有给我想要的。干杯中间的随机内容是什么意思?它将球移动到大小为15*size x 15*size的点(随机,-100),然后立即将球移动到大小相同的点(随机,480)。你想在这里做什么?中间的随机内容应该是屏幕上的开始点或图像开始的地方(-STARX)(-100),它应该在(480)结束(EDX),即Y轴上的屏幕底部。我想做的是让一个图像从屏幕顶部掉下来,然后从底部消失。这些图像是从iPhone屏幕x轴上任意任意位置的一张图像创建的。
***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;
}
- (void)animateBall:(NSTimer *)theTimer
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
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;
[UIView commitAnimations];
}