Why objects at the same SKNode level do not interact with each other?

I have less than 1 year using SpriteKit, so until recently, I did not use SKNodes as layers.

I have a SKNode level that contains all the fish and user position, for example:

var layerMainGame = SKNode()
layerMainGame.zPosition = 50
layerMainGame.addChild(userPosition)
layerMainGame.addChild(pipFish)
addChild(layerMainGame)

The interaction regarding the user, whether or not the fish is handled, is handled by this function, which basically checks to see if their frames overlap:

if CGRectIntersectsRect(CGRectInset(node.frame, delta.dx, delta.dy), self.userPosition.frame) {
    print("You got hit by \(name).")
    gameOver()
}

It works. The interaction between userPosition and pipFish works. The fish does not work, which is added as the game progresses. I have a function that propagates various types of fish at such intervals:

func spawnNew(fish: SKSpriteNode) {
    layerMainGame.addChild(fish)
}

, , . , . layerMainGame , , . SKNode .

, .

func createHitCollisionFor(name: String, GameOver gameOver: String!, delta: (dx: CGFloat, dy: CGFloat), index: Int = -1) {
    enumerateChildNodesWithName(name) { [unowned me = self] node, _ in
        if CGRectIntersectsRect(CGRectInset(node.frame, delta.dx, delta.dy), self.userPosition.frame) {
            me.gameOverImage.texture = SKTexture(imageNamed: gameOver)
            didGetHitActions()
            me.runAction(audio.playSound(hit)!)
            if index != -1 {
                me.trophySet.encounterTrophy.didEncounter[index] = true
            }
            print("You got hit by \(name).")
        }
    }
}

:

createHitCollisionFor("GoldPiranha", GameOver: model.gameOverImage["Gold"], delta: (dx: 50, dy: 50), index: 1)

, , , .

+4
1

node node, position , .

Sprite Kit , (0, 0), x y .

  • SKScene - anchorPoint (0, 0), . , (0.5, 0.5)

  • SKNode anchorPoint, (0.5, 0.5), node.

layerMainGame, , , , anchorPoint (0.5.0.5), , , , , :

func spawnNew(fish: SKSpriteNode) {
    layerMainGame.addChild(fish)
    fish.position = CGPointZero // position 0,0 = parent center 
}

, , .


: ( )

, , :

override func didMoveToView(view: SKView) {
        var layerMainGame = SKNode()
        addChild(layerMainGame)

        let pipFish = SKSpriteNode(color: UIColor.yellowColor(), size: CGSizeMake(50,50))
        pipFish.name = "son"
        self.addChild(pipFish)

        let layerPipFish = SKSpriteNode(color: UIColor.yellowColor(), size: CGSizeMake(50,50))
        layerPipFish.name = "son"
        layerMainGame.addChild(layerPipFish)

        enumerateChildNodesWithName("son") { [unowned me = self] node, _ in
            print(node)
        }
}

:

enter image description here

:

layerMainGame.addChild(layerPipFish)

:

self.addChild(layerPipFish)

enter image description here

?

enumerateChildNodesWithName, , , self ( enumerateChildNodesWithName, self.enumerateChildNodesWithName)

node ?

node "GoldPiranha", , // . , "//GoldPiranha":

enumerateChildNodesWithName("//GoldPiranha") { [unowned me = self] ...
+4

Source: https://habr.com/ru/post/1652265/


All Articles