Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 触摸节点时applyImpulse不工作_Ios_Swift_Sprite Kit_Skphysicsbody_Skshapenode - Fatal编程技术网

Ios 触摸节点时applyImpulse不工作

Ios 触摸节点时applyImpulse不工作,ios,swift,sprite-kit,skphysicsbody,skshapenode,Ios,Swift,Sprite Kit,Skphysicsbody,Skshapenode,我在屏幕上有一个圆圈,它会慢慢变大(顺便说一句,Balloon类是SKShapeNode的一种类型)。在触摸开始时,我点击圆圈,圆圈显示我触摸过它,但没有脉冲。我怎样才能找到问题?我对SpriteKit相当陌生 import SpriteKit import GameplayKit class GameScene: SKScene { var radius = 40.0 var balloon : Balloon? = nil override func didMove(to view:

我在屏幕上有一个圆圈,它会慢慢变大(顺便说一句,Balloon类是SKShapeNode的一种类型)。在触摸开始时,我点击圆圈,圆圈显示我触摸过它,但没有脉冲。我怎样才能找到问题?我对SpriteKit相当陌生

import SpriteKit
import GameplayKit

class GameScene: SKScene {

var radius = 40.0

var balloon : Balloon? = nil

override func didMove(to view: SKView) {

    balloon = Balloon(radius: radius, position: CGPoint(x: frame.midX, y: frame.minY + 250))
    balloon?.name = "balloon"

    let physicsBody = SKPhysicsBody(circleOfRadius: CGFloat(radius))
    physicsBody.affectedByGravity = false
    balloon?.physicsBody = physicsBody
    balloon?.physicsBody?.isDynamic = true

    let borderBody = SKPhysicsBody(edgeLoopFrom: self.frame)
    borderBody.friction = 0
    self.physicsBody = borderBody
    physicsWorld.gravity = CGVector(dx: 0.0, dy: 0.0)

    self.addChild(balloon!)

}



override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self)
        let node : SKNode = self.atPoint(location)
        if node.name == "balloon" {
            print("touching balloon.")
            balloon?.physicsBody?.applyImpulse(CGVector(dx: 10.0, dy: 10.0), at: location)
        }
    }

}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

}

override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {

}


 override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered
    radius += 0.05
    self.balloon?.radius = radius

    let physicsBody = SKPhysicsBody(circleOfRadius: CGFloat(radius))
    physicsBody.affectedByGravity = false
    balloon?.physicsBody = physicsBody
    balloon?.physicsBody?.isDynamic = true

  }
}
导入SpriteKit
导入游戏工具包
类游戏场景:SKScene{
var半径=40.0
var气球:气球?=nil
覆盖func didMove(到视图:SKView){
引出序号=引出序号(半径:半径,位置:CGPoint(x:frame.midX,y:frame.minY+250))
气球?.name=“气球”
设physicsBody=SKPhysicsBody(圆半径:CGFloat(半径))
physicsBody.affectedByGravity=false
气球?.physicsBody=physicsBody
气球?.physicsBody?.isDynamic=真
让borderBody=SKPhysicsBody(edgeLoopFrom:self.frame)
borderBody.摩擦力=0
self.physicsBody=边界体
physicsWorld.gravity=CGVector(dx:0.0,dy:0.0)
self.addChild(气球!)
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
接触{
让位置=触摸。位置(in:self)
let节点:SKNode=self.atPoint(位置)
如果node.name==“引出序号”{
打印(“触摸气球”)
气球?.physicsBody?.applyImpulse(CGVector(dx:10.0,dy:10.0),at:location)
}
}
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
}
覆盖功能触摸已取消(touchs:Set,带有事件:UIEvent?){
}
覆盖函数更新(uCurrentTime:TimeInterval){
//在渲染每个帧之前调用
半径+=0.05
self.balloon?半径=半径
设physicsBody=SKPhysicsBody(圆半径:CGFloat(半径))
physicsBody.affectedByGravity=false
气球?.physicsBody=physicsBody
气球?.physicsBody?.isDynamic=真
}
}
以下行:

let location = touch.location(in: self)
在场景中查找触摸的位置。但是,在函数调用中,它是相对于节点的。而不是

balloon?.physicsBody?.applyImpulse(CGVector(dx: 10.0, dy: 10.0), at: location)
使用:


每次更新都会创建一个新主体。如果要给气球充气,请使用磅秤:

var scale = 1.0
override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered
    scale += 0.05
    self.balloon?.setScale(scale)
}

然后,你的物理体将能够对其施加冲动,因为你不会每次更新都创建一个新的物理体。

Yikes,你每天都在创建一个新的物理体update@Knight0fDragon我读到这是我了解你所说的问题的唯一途径。但是如果气球真的变大了,它就会变得模糊。更重要的是,如果我使用setScale,我还能检测到它何时击中其他节点吗?它看起来有多模糊取决于你,是的,如果它是一个气球,那么你希望它变得模糊。在现实生活中吹一个气球,看看图形lol会发生什么
var scale = 1.0
override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered
    scale += 0.05
    self.balloon?.setScale(scale)
}