碰撞检测中的动作 (CGRectIntersectsRect)
我有两个图像。您必须使用acceleremoter
来移动人并避免出现大尖峰。这是我的 .h 编码:
IBOutlet UIImageView *rect1;
IBOutlet UIImageView *rect2;
这是我的 .m:
bool CGRectIntersectsRect ( CGRect rect1, CGRect rect2 );
我已经连接了 nib 文件中的所有内容,我知道不会发生任何事情,因为没有错误。我需要这两件事发生碰撞,然后生成一个片尾画面。但是如何我要采取行动。谢谢!
.米
#import "GameScreen.h"
@implementation GameScreen
@synthesize ball, delta;
-(void)viewDidLoad {
UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = self;
accel.updateInterval = 1.0f / 60.0f;
[super viewDidLoad];
}
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
NSLog(@"x : %g", acceleration.x);
NSLog(@"y : %g", acceleration.y);
NSLog(@"z : %g", acceleration.z);
delta.y = acceleration.y * 70;
delta.x = acceleration.x * 70;
ball.center = CGPointMake(ball.center.x + delta.x, ball.center.y + delta.y);
// Right
if(ball.center.x < 0) {
ball.center = CGPointMake(320, ball.center.y);
}
// Left
if(ball.center.x > 320) {
ball.center = CGPointMake(0, ball.center.y);
}
// Top
if(ball.center.y < 0) {
ball.center = CGPointMake(ball.center.x, 460);
}
// Bottom
if(ball.center.y > 460){
ball.center = CGPointMake(ball.center.x, 0);
}
}
bool CGRectIntersectsRect ( CGRect rect1, CGRect rect2 );
-(void)dealloc {
[super dealloc];
}
@end
I have two images. You have to use a acceleremoter
to move a man and avoid a big spike. Here is my .h coding:
IBOutlet UIImageView *rect1;
IBOutlet UIImageView *rect2;
Here is my .m:
bool CGRectIntersectsRect ( CGRect rect1, CGRect rect2 );
I have connected everything in the nib file and I know nothing would happen because there was no error. I need these two things to collide and then generate an end screen. BUT HOW do I put an action in. THANKS!
.m
#import "GameScreen.h"
@implementation GameScreen
@synthesize ball, delta;
-(void)viewDidLoad {
UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = self;
accel.updateInterval = 1.0f / 60.0f;
[super viewDidLoad];
}
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
NSLog(@"x : %g", acceleration.x);
NSLog(@"y : %g", acceleration.y);
NSLog(@"z : %g", acceleration.z);
delta.y = acceleration.y * 70;
delta.x = acceleration.x * 70;
ball.center = CGPointMake(ball.center.x + delta.x, ball.center.y + delta.y);
// Right
if(ball.center.x < 0) {
ball.center = CGPointMake(320, ball.center.y);
}
// Left
if(ball.center.x > 320) {
ball.center = CGPointMake(0, ball.center.y);
}
// Top
if(ball.center.y < 0) {
ball.center = CGPointMake(ball.center.x, 460);
}
// Bottom
if(ball.center.y > 460){
ball.center = CGPointMake(ball.center.x, 0);
}
}
bool CGRectIntersectsRect ( CGRect rect1, CGRect rect2 );
-(void)dealloc {
[super dealloc];
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就在结束之前,
你必须把这段代码写下来。
但我也认为你必须购买一本 Objective-C 编程书籍并阅读它,而不是在 StackOverflow 上充斥大量类似的问题。我写这些是因为我认为它对你有用,而不是让你生气。
Just before the end of
you have to put this code
But I also think that you have to buy an Objective-C programming book and read it, instead of spamming StackOverflow with a lot of similar questions. I wrote that because I think it can be useful to you, not to make you angry.
一起工作就不要
你可以尝试这个,如果它能与 ImageView 的init
了
You can try this, just donne if its gonna work with ImageView's
init
Make a void