2010-03-19 23 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

相关问题