gpt4 book ai didi

swift - 请帮助我了解 UITouch (SpriteKit)

转载 作者:行者123 更新时间:2023-11-30 10:13:22 27 4
gpt4 key购买 nike

我想从左侧和右侧创建一个节点,当我触摸它们时,小鸟会飞向左侧或右侧。

  1. 首先我创建了一个鸟节点

  2. 然后,我创建了测试节点

    var groundleft = SKSpriteNode()
    groundleft.position = CGPoint(x: 0, y: 50)
    groundleft.size = CGSize(width: 500, height: 100)
    groundleft.color = UIColor(red: 7, green: 5, blue: 7, alpha: 20)


    groundleft.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.size.width, 100))
    groundleft.physicsBody?.dynamic = false
    groundleft.zPosition = 0.5

    groundleft.name = "toleft"
  3. 然后我实现了touchesBegan

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {


    let touch = touches.first as! UITouch
    let touchLocation = touch.locationInNode(self)
    let touchedNode = self.nodeAtPoint(touchLocation)

    if(touchedNode.name == "toleft"){

    bird.physicsBody?.velocity = CGVectorMake(0, 0)
    bird.physicsBody?.applyImpulse(CGVectorMake(-10, 100))

    }
    }

为什么不起作用??

最佳答案

您遇到问题的原因是物理主体的动态属性设置为 false。

您需要设置

groundleft.physicsBody?.dynamic = true //Or remove this line, as dynamic is true by default

您可以阅读有关动态属性的更多信息here 。如文档中所示:

The default value is YES. If the value is NO, the physics body ignores all forces and impulses applied to it. This property is ignored on edge-based bodies; they are automatically static.

您可能已将dynamic 设置为 false,因为节点可能会由于物理世界的重力而掉落。为了解决这个问题,您可以设置物理主体的 affectedByGravity 属性,或者完全更改场景的物理世界的 gravity 属性。

关于swift - 请帮助我了解 UITouch (SpriteKit),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31674432/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com