Mika
Mika

Reputation: 45

How to enable cc touch events when i add CCLayer to UIViewController

here is my code:

#import "VirusViewController.h"
#import "MainGame.h"
#import "cocos2d.h"

@implementation VirusViewController
@synthesize window;

- (void)loadView {
    // Initialization code
    CC_DIRECTOR_INIT();
    CCDirector *director = [CCDirector sharedDirector];
    //landscape
    [director setDeviceOrientation:kCCDeviceOrientationPortrait];
    [director setDisplayFPS:YES];

    //turn on multi-touch
    EAGLView *cocosView = [director openGLView];
    [cocosView setMultipleTouchEnabled:YES];

    self.view = cocosView;

    //default texture formats...
    [CCTexture2D  setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888];

    [[CCDirector sharedDirector] runWithScene:[MainGame scene]];
}

-(void)viewDidLoad
{
    UIButton *nextButton = [[UIButton alloc] initWithFrame:CGRectMake(40, 40, 64, 64)];
    [nextButton setImage:[UIImage imageNamed:@"homeButton.png"] forState:UIControlStateNormal];
    [nextButton addTarget:self action:@selector(goBack)  forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nextButton];
    [nextButton release];
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [[CCDirector sharedDirector] purgeCachedData];
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [[CCDirector sharedDirector] release];
    [window release];
    [super dealloc];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}

-(void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

nextButton is not receiving touch events when i init the CC_Director_INIT();

also - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event is not working inside a subview of MainGame.h

in other words... I can't receive any touch events

Upvotes: 2

Views: 4742

Answers (2)

gheese
gheese

Reputation: 1200

sharedDispatcher is deprecated - you should use this instead:

[[CCDirector sharedDirector] touchDispatcher]

Upvotes: 2

Bongeh
Bongeh

Reputation: 2300

Try giving your layer these methods

 - (void)onEnter
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];
[super onEnter];
}

- (void)onExit
{
[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
[super onExit];
}   

Upvotes: 3

Related Questions