类从CCLayerColor继承
@interface LQModalLayer : CCLayerColor {
}
@interface LQModalLayer () {
// 模态对话框菜单
CCMenu *m_pMenu;
// 记录菜单点击
bool m_bTouchedMenu;
}
// 初始化数据
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super's" return value
if( (self=[super initWithColor:ccc4(0, 0, 0, 125)]) ) {
// ask director for the window size
//CGSize size = [[CCDirector sharedDirector] winSize];
CCMenuItemSprite *testItem1 =
[CCMenuItemSprite itemWithNormalSprite: [CCSprite spriteWithSpriteFrameName: @"btn3.png"]
selectedSprite: [CCSprite spriteWithSpriteFrameName: @"btn3sel.png"]
target: self
selector: @selector(itemPressed:)];
testItem1.position = ccp(320,200);
m_pMenu = [CCMenu menuWithItems: testItem1,nil];
m_pMenu.position = CGPointZero;
m_pMenu.tag = 10;
[self addChild:m_pMenu];
[self setIsTouchEnabled:YES];
}
return self;
}
-(void) onEnter
{
[[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:kCCMenuHandlerPriority-1 swallowsTouches:YES];
[super onEnter];
}
- (void)onExit
{
[[[CCDirector sharedDirector] touchDispatcher] removeDelegate:self];
[super onExit];
}
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
// CGPoint point = [touch locationInView: [touch view]];
// point = [[CCDirector sharedDirector] convertToGL: point];
m_bTouchedMenu = [m_pMenu ccTouchBegan:touch withEvent:event];
return YES;
}
-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
if (m_bTouchedMenu) {
[m_pMenu ccTouchMoved:touch withEvent:event];
}
return;
}
-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
if (m_bTouchedMenu) {
[m_pMenu ccTouchEnded:touch withEvent:event];
m_bTouchedMenu = false;
}
}
-(void) ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event
{
if (m_bTouchedMenu) {
[m_pMenu ccTouchCancelled:touch withEvent:event];
m_bTouchedMenu = false;
}
}
- (void) itemPressed: (CCNode *) item
{
NSLog(@"itemPressed...%@",item);
[self setVisible:false];
[self.parent removeChild:self cleanup:true];
}
// 释放对象资源
- (void) dealloc
{
// in case you have something to dealloc, do it in this method
// in this particular example nothing needs to be released.
// cocos2d will automatically release all the children (Label)
// don't forget to call "super dealloc"
[super dealloc];
}