swift - 触摸时的 Sprite 节点第一个仅采用物理主体

标签 swift xcode sprite-kit xcode8

我正在使用 Xcode 8.1 Swift 3 制作小型弹跳游戏。

玩家应该在弹跳球周围创建墙壁,并且这个球应该在每面墙上弹跳。

触摸按下时,我移动到点,触摸结束时,我在两个触摸开始和结束之间创建一个线共享节点。

我添加了节点所需的物理特性,然后向该节点添加了子节点(请参阅下面的节点)。

发生的情况是,对于每次触摸开始和触摸结束,“Swift”绘制线节点并将其附加到自身,但只有第一个节点反弹球。 第一条线之后的所有线(墙)都不影响球。

这是我的 GameScene Swift 文件代码:

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {
// vars lets and nodes
let startingBall = SKShapeNode(circleOfRadius: 10)
let myPath = CGMutablePath()
let ballCategory : UInt32 = 1
let wallCategory : UInt32 = 2

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for t in touches {
        myPath.move(to: t.location(in: self))
    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    for t in touches {
        myPath.addLine(to: t.location(in: self))
        let wallNode = SKShapeNode(path: myPath)
        wallNode.lineWidth = 5.0
        wallNode.fillColor = SKColor.green
        wallNode.strokeColor = SKColor.green
        wallNode.physicsBody = SKPhysicsBody(edgeLoopFrom: myPath)
        wallNode.physicsBody?.categoryBitMask = wallCategory
        self.addChild(wallNode)
    }
}

override func didMove(to view: SKView) {

    self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame)
    self.physicsBody?.categoryBitMask = wallCategory
    startingBall.fillColor = SKColor.red
    startingBall.position = CGPoint(x: self.frame.width/2, y: self.frame.height/2)
    startingBall.physicsBody = SKPhysicsBody(circleOfRadius: 10)
    startingBall.physicsBody?.affectedByGravity = false
    startingBall.physicsBody?.isDynamic = true
    startingBall.physicsBody?.restitution = 1.0
    startingBall.physicsBody?.friction = 0.0
    startingBall.physicsBody?.linearDamping = 0.0
    startingBall.physicsBody?.angularDamping = 0.0
    self.addChild(startingBall)

    startingBall.physicsBody?.applyImpulse(CGVector(dx: 3.0, dy: 3.0))
}
}

最佳答案

谢谢大家,我已修复它

实际上可变路径 myPath 定义触摸开始线和触摸结束线之间的线,仅绘制形状节点...而为从节点到节点的每个触摸系列创建物理边缘...

逐步 1-我添加了起点和终点变量 2-拾取触摸开始中的起始节点和触摸结束中的结束节点 3-我为点到点的起始节点和结束节点设置物理体

1-

var startPoint : CGPoint = CGPoint.zero
var endPoint : CGPoint = CGPoint.zero

2-..

override func touchesBegan(_ touches: Set<UITouch>, with event:   UIEvent?) {
    for t in touches {
        startPoint = t.location(in: self)
        myPath.move(to: t.location(in: self))
    }
}

        endPoint = t.location(in: self)

3-..

        wallNode.physicsBody = SKPhysicsBody(edgeFrom: startPoint, to:  endPoint)

如需帮助,您可以在skiver中将显示物理设置为true

    skview.showsPhysics = true

谢谢

关于swift - 触摸时的 Sprite 节点第一个仅采用物理主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41091041/

相关文章:

ios - 升级到 xcode 4.6 和 iOS 6.1 后出错 "used as the name of the previous parameter rather than as part of the selector"

ios - Xcode:仅当 PKI key 插入时,才能在钥匙串(keychain)中找到指定的项目

swift - 如何检测您的手指是否与蒙面 Sprite 接触

ios - 使用 Overlayskscene Xcode Swift 触摸屏幕 Scenekit 时应用程序崩溃

ios - Swift - 如何从相册加载视频并在收藏 View 中显示它们

ios - UICollectionView 粘性标题在集合过度滚动时插入部分后消失一段时间(弹跳效果)

ios - 如果 Macro 设置为 1,则排除目标中的类

swift - Sprite 定位在 y 轴上的错误点

swift - Firebase:登录时找不到用户

ios - 在视频帧下添加ScrollView