我正在使用Xcode 8.1 Swift
3制作小型弹跳游戏。
玩家应该围绕一个弹跳球创建围墙,这个球应该在每个墙上反弹。
在触摸下,我移动到指向并触摸结束,我在两个触摸开始和结束之间创建一个线共享节点。
我添加了节点所需的物理,然后我将child添加到此节点(请参阅下面的节点)。
接下来发生的事情是,每次接触都会开始,接触结束,“斯威夫特'绘制线节点并将其附加到自身,但只有第一个节点反弹球。 第一个之后的所有线(墙)都不会影响球。
这是我的代码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))
}
}
答案 0 :(得分:0)
感谢所有我已修复它
实际上可变路径myPath定义触摸开始和触摸结束线之间的线以仅绘制形状节点...同时为节点之间的每个触摸系列创建物理边缘...
分步骤 1- i添加了起点和终点变量 2-触摸开始节点触摸开始节点和触摸端节点结束 3- i为点到点起始节点和结束节点设置物理体
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中将show physics设置为true
skview.showsPhysics = true
感谢