【问题标题】:How do I add touch event to the CCNode in CocosSharp?如何在 CocosSharp 中为 CCNode 添加触摸事件?
【发布时间】:2015-07-12 17:05:46
【问题描述】:

我有一个图层:

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

请注意“quitItem”。我将 CCEventListenerTouchOneByOne 添加到它,希望它会做我想做的事情。在这种情况下,如果元素被触摸,它应该在输出中写入“touched”。不幸的是,什么也没发生,断点也永远不会命中。

基本问题 - 我想将触摸事件添加到 CCNode。怎么样?

【问题讨论】:

    标签: c# monogame cocossharp


    【解决方案1】:

    // 类变量 CCLabel 退出项目;

    // 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
                }
    
    }
    

    【讨论】:

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