2014-03-31 41 views
-1

我是編程新手,現在確實需要幫助。我一直在尋找我認爲在過去兩個月的答案。我正在使用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與之碰撞但停在牆上的圖像。希望它足夠清晰(我是法國人,所以對於不好的寫作感到抱歉):謝謝。

+0

hoo確定那有點道理?沒有給我答案,你可以給我提示如何編碼..我嘗試了幾件事,但沒有幫助:s – user3481455

回答

1

鑑於您成功檢測到碰撞,答案似乎是,如果移動導致碰撞,則不要將對象更新到新位置。只是不要更新Dot.center。順序可能是:獲取移動的觸摸事件,預先計算對象所在的位置,如果沒有碰撞,則移動它;如果發生碰撞,請不要更新它的位置。

請注意,OpenGL可能更適合這種類型的事情,因爲你會做很多事情。