0

I have a layer:

public class MenuItemsLayer : CCLayer
{
    protected override void AddedToScene()
    {
        base.AddedToScene();

        var quitItem = new CCLabel("QUIT", "fonts/MarkerFelt", 22, CCLabelFormat.SpriteFont);
        (...)
        this.AddEventListener(this.addQuitItemTouchListener(), quitItem);
        this.AddChild(quitItem);
    }

    private CCEventListenerTouchOneByOne addQuitItemTouchListener()
    {
        var touchListener = new CCEventListenerTouchOneByOne();
        touchListener.OnTouchEnded = (touch, args) =>
        {
            System.Diagnostics.Debug.WriteLine("touched");
        };

        return touchListener;
    }
}

Please pay attention to "quitItem". I'm adding CCEventListenerTouchOneByOne to it with the hope, that it will do something I want. In this case it should write "touched" in the output if element is touched. Unfortunantely, nothing happens, also breakpoint is never hit.

Basic problem - I'd like to add touch event to the CCNode. How?

Andrzej
  • 848
  • 1
  • 11
  • 26

1 Answers1

1

// class variable CCLabel quitItem;

// In AddedToScene
var touchListener =  new CCEventListenerTouchAllAtOnce();
touchListener.OnTouchBegan = this.OnTouchesBegan;
AddEventListener(touchListener,this);


// Handler

void OnTouchesBegan(List<CCTouch> touches, CCEvent touchEvent)
{
    if ( quitItem.BoundingBoxTransformedToWorld.ContainsPoint(touches[0].Location ))
            {
                // print message here
            }

}
jaybers
  • 1,991
  • 13
  • 18