2010-03-19 22 views
0

我將cocos2d用於我的遊戲。我在其中播放電影併爲控件設置單獨的疊加視圖。疊加視圖中檢測到觸摸。現在,當觸摸被檢測到時,遊戲代碼中的功能必須被調用。但是該功能未被檢測到並且沒有錯誤。我不知道出了什麼問題。有人請幫助我。 的代碼如下使用協議調用不同類的函數時出現問題 - iphone

協議部分是

@protocol Protocol 
@required 
- (void)transition1:(id)sender; 
@end 

這是在遊戲代碼被調用的功能是

- (void)transition1:(id)sender 
{ 
    [[Director sharedDirector] replaceScene: [ [Scene node] addChild: [Layer4 node] z:0] ]; 
} 

在MovieOverlayViewController.h疊加視圖的代碼

#import "Protocol.h" 

@interface MovieOverlayViewController : UIViewController 
{ 
    UIImageView *overlay; 
    NSObject <Protocol> *transfer; 
} 
@end 

MovieOve中疊加視圖中的代碼rlayViewController.m

@implementation MovieOverlayViewController 

- (id)init 
{ 
    if ((self = [super init])) 
     self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]; 
    return self; 
} 

-(void) viewWillAppear:(BOOL)animated 
{ 
    overlay = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"overlay.png"]] autorelease]; 

    [self.view addSubview:overlay]; 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{  
    UITouch *touch = [touches anyObject]; 
    CGPoint point = [touch locationInView:self.view]; 
    NSLog(@"pointx: %f pointy:%f", point.x, point.y); 

    if (CGRectContainsPoint(CGRectMake(1, 440, 106, 40), point)) 
    { 
     // the function is called here 
     [transfer transition1: nil]; 
    } 
    else if (CGRectContainsPoint(CGRectMake(107, 440, 106, 40), point)) 
     NSLog(@"tab 2 touched"); 
} 

- (void)dealloc 
{ 
    [overlay release]; 
    [super dealloc]; 
} 

@end 

回答

1

這類問題是使用調試器最好接近:在touchesBegan:withEvent:設置斷點,看看方法被調用。然後單步看看會發生什麼。在調試器中,您還可以檢查transfer的值是否爲nil,這可能是問題所在。

哦,既然這是你的第二個問題,請看看SO中的how code should be formatted。標記並不難。

+0

謝謝汝赫。 我將格式化我的下一個問題。 – Muniraj 2010-03-19 12:06:26

1

您沒有初始化transfer

當伊娃沒有初始化時,它的值爲0(無)。發送消息到零是一個沒有操作,所以沒有錯誤,沒有任何反應。

+0

你能告訴我如何初始化它嗎? – Muniraj 2010-03-19 12:07:32

+0

@Muniraj:在哪個類中是 - (void)transition1:(id)sender;'defined? – kennytm 2010-03-19 12:30:04

+0

在主要的遊戲代碼中。我把它命名爲Layer3 – Muniraj 2010-03-19 12:33:46

相關問題