我是編程新手,現在確實需要幫助。我一直在尋找我認爲在過去兩個月的答案。我正在使用Xcode和objective-c。我的問題是關於碰撞檢測。當使用CGRECT進行兩個矩形碰撞時,例如警報或翻轉屏幕或播放聲音分機,有數千個示例,但沒有任何關於做任何事的大聲笑!我想要的是我的對象不會通過其他對象!這就是我想要繼續拖拽它在屏幕上。我只是不希望這兩個對象在彼此之上,而且似乎我是世界上唯一想要這樣做的在線用戶,因爲我找不到任何東西。所以請幫助,因爲我是新的..儘可能簡單,請所以在這裏:使用代碼碰撞檢測
#import "YellowDot.h"
@interface YellowDot()
@end
@implementation YellowDot
@synthesize Dot;
@synthesize CollisionImage;
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *Drag = [ [ event allTouches ] anyObject ];
Dot.center = [ Drag locationInView: self.view ];
[self checkCollison];
}
-(void) checkCollison
{
if (CGRectIntersectsRect(Dot.frame, CollisionImage.frame))
{
AudioServicesPlaySystemSound(playSoundId);
}
}
- (void)viewDidLoad
{
NSURL *SoundURL = [ NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"beep"ofType:@"wav"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)SoundURL, & playSoundId);
[super viewDidLoad];
// Do any additional setup after loading the view.
}
and here is the .h file :
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
@interface YellowDot : UIViewController
{
IBOutlet UIImageView *Dot;
IBOutlet UIImageView *CollisionImage;
SystemSoundID playSoundId;
}
@property (nonatomic, retain) UIImageView *Dot;
@property (nonatomic, retain) UIImageView *CollisionImage;
@end
那麼可以在那裏呢?正如你所看到的,它已經在發生碰撞時發出聲音,但就是這樣。點是我在屏幕上拖動的圖像,碰撞圖像是我希望Dot與之碰撞但停在牆上的圖像。希望它足夠清晰(我是法國人,所以對於不好的寫作感到抱歉):謝謝。
hoo確定那有點道理?沒有給我答案,你可以給我提示如何編碼..我嘗試了幾件事,但沒有幫助:s – user3481455