当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - CCMenuItem that added as a child to another CCMenuItem doesn't respond to touch

问题描述:

I'm implementing a decorator pattern for adding the selected/unselected events to CCMenuItem inheritors:

 public class MenuItemDecorator : CCMenuItem

{

public event EventHandler OnSelected;

public event EventHandler OnUnselected;

private readonly CCMenuItem _menuItem;

public MenuItemDecorator(CCMenuItem menuItem) //set a decorated item

{

menuItem.ThrowIfNull("menuItem");

_menuItem = menuItem;

AddChild(_menuItem);

}

public override void Selected()

{

base.Selected();

_menuItem.Selected();

if(OnSelected != null)

OnSelected(this, null);

}

public override void Unselected()

{

base.Unselected();

_menuItem.Unselected();

if(OnUnselected != null)

OnUnselected(this, null);

}

}

Then I "decorate" my items:

...

var soundToggle = MenuItemToggleBuilder.New(soundOffBtn, soundOnBtn)

.SetPosition(positionX, positionY)

.SetTarget(ToggleSelector)

.Build();

var decorToggle = new MenuItemDecorator(soundToggle);

decorToggle.SetTarget(Selector); // for debugging

decorToggle.OnSelected += (s, e) => toggleWafer.Scale = 0.95f;

decorToggle.OnUnselected += (s, e) => toggleWafer.Scale = 1f;

LayerMenu.AddChild(soundToggle, int.MaxValue); //LayerMenu is CCMenu

...

private void Selector(object o)

{//breakpoint is here

}

...

But when I start the application and click on my decorToggle it doesn't respond.

Moreover it doesn't step into my Selector method (where I've set the breakpoint).

So what I'm doing wrong? Could it be made easier? Thanks in advance


Thank you, @LearnCocos2D. But why debugger doesn't stop on breakpoint in Selector method?

Also what with the second part of question? Could I add custom behavior to Selected/Unselected events on CCMenuItem inheritors without inheritance from a concrete CCMenuItem inheritor(sorry for the pun) and overriding the Selected/Unselected methods?

i.e. CCMenuItemImageWithExtendedSelect, CCMenuItemLabelWithExtendedSelect, CCMenuItemToggleWithExtendedSelect etc. and each of these classes just overriding Selected/Unselected methods (as in my MenuItemDecorator)

网友答案:

If CCMenu has the same behavior as in cocos2d-iphone, which is safe to assume, then CCMenuItem are not stackable. You can't have a CCMenuItem as child of another menu item and both reacting to touches properly.

分享给朋友:
您可能感兴趣的文章:
随机阅读: