【问题标题】:How to enable cc touch events when i add CCLayer to UIViewController当我将 CCLayer 添加到 UIViewController 时如何启用 cc 触摸事件
【发布时间】:2011-03-07 12:12:02
【问题描述】:

这是我的代码:

#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];
}

当我初始化 CC_Director_INIT() 时,nextButton 没有接收到触摸事件;

还有- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event 不在 MainGame.h 的子视图中工作

换句话说...我无法接收任何触摸事件

【问题讨论】:

    标签: uiviewcontroller cocos2d-iphone touch


    【解决方案1】:

    尝试为您的图层提供这些方法

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

    【讨论】:

      【解决方案2】:

      sharedDispatcher 已弃用 - 您应该改用它:

      [[CCDirector sharedDirector] touchDispatcher]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多