在SKScene中没有调用TouchesBeganWithEvent

时间:2017-02-09 07:24:36

标签: cocoa sprite-kit xamarin.ios skscene xamarin.mac

我在SKScene中遇到触摸事件的问题,当我在场景中触摸精灵时,这些事件没有被调用:(

这是我的代码

public class FallingObjectsScene : SKScene
{
    public override void DidMoveToView(SKView view)
    {
        base.DidMoveToView(_view);

        UserInteractionEnabled = true;

        Image = new SKSpriteNode();
        Image.Position = new CGPoint(labelXpostion, skView.Bounds.GetMaxY());
        Image.Texture = imageTexture.Texture;
        Image.Size = new CGSize(60, 60);
        Image.Speed = 3.01f;
        Image.AnchorPoint = new CGPoint(x: 0.5, y: 0.5);
        AddChild(Image);
    }
 }

 public override void TouchesBeganWithEvent(NSEvent theEvent)
 {
    base.TouchesBeganWithEvent(theEvent);

    var point = theEvent.LocationInNode(this);
    var touchNode = GetNodeAtPoint(point);
    try
    {
        //... some code
    }
    catch (Exception exception)
    {
        LoggingManager.Error(exception);
        ClearScreenTouches();
    } 
 }

任何人都可以帮忙解决这个问题, 感谢。

1 个答案:

答案 0 :(得分:0)

你的语法看起来很奇怪,在Xcode 8.2.1& Swift 3,应该看起来像:

class MenuScene: SKScene {

    override init(){...}

    override func didMove(to view: SKView) {
       ...
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in (touches ){
            let location = touch.location(in: self)
            let touchNode = atPoint(location)

            ...
        }
    }
}