【问题标题】:TouchesBeganWithEvent not getting called in SKSceneTouchesBeganWithEvent 没有在 SKScene 中被调用
【发布时间】:2017-02-09 07:24:36
【问题描述】:

我在 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();
    } 
 }

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

【问题讨论】:

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


    【解决方案1】:

    你的语法看起来很奇怪,在 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)
    
                ...
            }
        }
    }
    

    【讨论】:

    • 感谢您的回复,我按照上面相同的代码,我正在使用 Xamarin 技术,技术无关紧要,但没有调用该方法。
    • 从您发布的代码来看,您的 touchesBegan 函数似乎与您的类定义不符,这就是 Chen 的意思,
    • @SteveIves 是的,但 touchesBegan 函数没有显示在我的 Xamarin 技术中:( 这就是我使用鼠标事件的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-12
    • 2016-08-23
    • 2016-10-12
    • 2019-08-14
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    相关资源
    最近更新 更多