tags:

views:

109

answers:

2

I use cocos2d for my game. In which I play a movie and have a separate overlay view for controls.The touches are detected in the overlay view. Now when the touches are detected the function in the game code has to be invoked. But the function is not detected and there is no error. I dont know what has gone wrong. Someone please help me. The code are as follows

The protocol part is

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

The function which is to be invoked in the game code is

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

The code in the overlay view in MovieOverlayViewController.h

#import "Protocol.h"

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

The code in the overlay view in MovieOverlayViewController.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  A: 

This sort of problems is best approached using the debugger: Set a breakpoint in touchesBegan:withEvent: and see if the method gets called. Then single step to see what happens. In the debugger you can also check if the value of transfer is nil, which might be the problem.

Oh, and since this is your twentyfirst question, please have a look at how code should be formatted in SO. Markup is not that difficult.

Nikolai Ruhe
Thanks Ruhe .I will format my next question onwards.
Muniraj
+1  A: 

You did not initialize transfer.

When an ivar is not initialized, it has a value of 0 (nil). Sending messages to nil is a no-op, so there's no error and nothing happens.

KennyTM
Can u please tell me how to intialize it.
Muniraj
@Muniraj: In which class is `-(void)transition1:(id)sender;` defined?
KennyTM
In the main game code. Which i named it Layer3
Muniraj
@Muniraj: Then call `transfer = [[Layer3 alloc] init];` in `-[MovieOverlayViewController init]`. (and `-release` it in `-dealloc`.)
KennyTM
@Kenny : it goes like a infinite loopMPMoviePlayerController instance <MPMoviePlayerController: 0x45b1b80> is already playing, ignoring.
Muniraj
@Muniraj: ?????
KennyTM