如何检查sprite节点是否改变位置

时间:2023-01-22 23:36:28

My sprite node change position on the screen. And I need to detect when sprite move to up, move to down, move to left, move to right.

我的精灵节点在屏幕上改变位置。我需要检测精灵何时向上移动,向下移动,向左移动,向右移动。

2 个解决方案

#1


0  

You’ve got two options here. As you’ve not posted any code I can’t really advise which is the best:

你有两个选择。由于您没有发布任何代码,我无法真正建议哪个代码最好:

  1. If you’ve sub-classed SKNode, then override the frame property to add a didSet observer clause:

    如果您已经对SKNode进行了子类,则覆盖frame属性以添加didSet observer子句:

    override var frame : CGRect {

    覆盖var frame:CGRect {

    didSet {
         // compute the change in the frame’s origin
         let delta : CGPoint = CGPoint(x: frame.origin.x - oldValue.origin.x,
                                                            y: frame.origin.y - oldValue.origin.y)
    
         // etc
     }
    

    }

See SKNode docs at https://developer.apple.com/documentation/spritekit/sknode

请参阅https://developer.apple.com/documentation/spritekit/sknode上的SKNode文档

  1. If you don’t want to subclass, you can use NSNotificationCenter to add an observer to your node via addObserver:selector:name:object: . See the documentation at:
  2. 如果您不想进行子类化,可以使用NSNotificationCenter通过addObserver:selector:name:object:将观察者添加到您的节点。请参阅以下文档:

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreAnimation_guide/Introduction/Introduction.html

I’d probably go for option 1.

我可能会选择1。

Hope that helps.

希望有所帮助。

#2


0  

Define a variable oldPosition and keep track of the sprite position in the update: method:

定义变量oldPosition并跟踪update:方法中的精灵位置:

var oldPosition: CGPoint?

override func update(_ currentTime: TimeInterval) {
        // Called before each frame is rendered

        if Int((oldPosition?.x)!) > Int((sprite?.position.x)!) {
            // sprite moves to the left

        } else if Int((oldPosition?.x)!) < Int((sprite?.position.x)!) {
            // sprite moves to the right

        }

        if Int((oldPosition?.y)!) > Int((sprite?.position.y)!) {
            // sprite moves down

        } else if Int((oldPosition?.y)!) < Int((sprite?.position.y)!) {
            // sprite moves up

        }

        // keep track
        oldPosition = sprite?.position
}

I hope it helps.

我希望它有所帮助。

#1


0  

You’ve got two options here. As you’ve not posted any code I can’t really advise which is the best:

你有两个选择。由于您没有发布任何代码,我无法真正建议哪个代码最好:

  1. If you’ve sub-classed SKNode, then override the frame property to add a didSet observer clause:

    如果您已经对SKNode进行了子类,则覆盖frame属性以添加didSet observer子句:

    override var frame : CGRect {

    覆盖var frame:CGRect {

    didSet {
         // compute the change in the frame’s origin
         let delta : CGPoint = CGPoint(x: frame.origin.x - oldValue.origin.x,
                                                            y: frame.origin.y - oldValue.origin.y)
    
         // etc
     }
    

    }

See SKNode docs at https://developer.apple.com/documentation/spritekit/sknode

请参阅https://developer.apple.com/documentation/spritekit/sknode上的SKNode文档

  1. If you don’t want to subclass, you can use NSNotificationCenter to add an observer to your node via addObserver:selector:name:object: . See the documentation at:
  2. 如果您不想进行子类化,可以使用NSNotificationCenter通过addObserver:selector:name:object:将观察者添加到您的节点。请参阅以下文档:

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreAnimation_guide/Introduction/Introduction.html

I’d probably go for option 1.

我可能会选择1。

Hope that helps.

希望有所帮助。

#2


0  

Define a variable oldPosition and keep track of the sprite position in the update: method:

定义变量oldPosition并跟踪update:方法中的精灵位置:

var oldPosition: CGPoint?

override func update(_ currentTime: TimeInterval) {
        // Called before each frame is rendered

        if Int((oldPosition?.x)!) > Int((sprite?.position.x)!) {
            // sprite moves to the left

        } else if Int((oldPosition?.x)!) < Int((sprite?.position.x)!) {
            // sprite moves to the right

        }

        if Int((oldPosition?.y)!) > Int((sprite?.position.y)!) {
            // sprite moves down

        } else if Int((oldPosition?.y)!) < Int((sprite?.position.y)!) {
            // sprite moves up

        }

        // keep track
        oldPosition = sprite?.position
}

I hope it helps.

我希望它有所帮助。