首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在将CCLayer添加到UIViewController时启用cc触摸事件

如何在将CCLayer添加到UIViewController时启用cc触摸事件
EN

Stack Overflow用户
提问于 2011-03-07 12:12:02
回答 2查看 4.7K关注 0票数 2

这是我的代码:

代码语言:javascript
复制
#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 ()时,CC_Director_INIT没有接收到触摸事件;

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

换句话说..。我不能接受任何触摸事件

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-03-07 13:32:42

尝试给你的图层以下方法

代码语言:javascript
复制
 - (void)onEnter
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];
[super onEnter];
}

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

Stack Overflow用户

发布于 2013-04-17 14:57:03

不建议使用sharedDispatcher --您应该使用以下方法:

代码语言:javascript
复制
[[CCDirector sharedDirector] touchDispatcher]
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5219563

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档