Sprite kit 精灵套件碰撞不工作
大家好,我正在尝试使用swift对xcode 6进行基本的线条绘制测试。但是我的碰撞系统根本不工作。这是我的碰撞系统的代码:Sprite kit 精灵套件碰撞不工作,sprite-kit,collision,game-physics,Sprite Kit,Collision,Game Physics,大家好,我正在尝试使用swift对xcode 6进行基本的线条绘制测试。但是我的碰撞系统根本不工作。这是我的碰撞系统的代码: func drawLines() { CGPathMoveToPoint(path, nil, location.x, location.y) CGPathAddLineToPoint(path, nil, self.frame.size.width/2, self.frame.size.height / 5) drawLine.appen
func drawLines() {
CGPathMoveToPoint(path, nil, location.x, location.y)
CGPathAddLineToPoint(path, nil, self.frame.size.width/2, self.frame.size.height / 5)
drawLine.append(SKShapeNode())
drawLine[index] = SKShapeNode()
line.append(drawLine[index])
line[index].path = path
line[index].strokeColor = UIColor.redColor()
line[index].lineWidth = 5.0
line[index].physicsBody = SKPhysicsBody(rectangleOfSize: line[index].frame.size)
line[index].physicsBody.dynamic = false
line[index].zPosition = 1
self.addChild(line[index])
index++
}
我无法找出问题所在,但我认为我在那段代码中犯了一个错误。
以下是我的代码的其余部分:
class GameScene: SKScene {
var line: [SKShapeNode] = []
var drawLine: [SKShapeNode] = []
var path = CGPathCreateMutable()
var touch: UITouch!
var location:CGPoint!
var index = 0
let player = SKSpriteNode(imageNamed: "player")
override func didMoveToView(view: SKView) {
player.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2)
player.physicsBody = SKPhysicsBody(rectangleOfSize: player.size)
player.physicsBody.dynamic = true
self.physicsWorld.gravity = CGVectorMake(0,-1)
player.zPosition = 1
self.addChild(player)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
touch = touches.anyObject() as UITouch!
location = touch.locationInNode(self)
drawLines()
}
func drawLines() {
CGPathMoveToPoint(path, nil, location.x, location.y)
CGPathAddLineToPoint(path, nil, self.frame.size.width/2, self.frame.size.height / 5)
drawLine.append(SKShapeNode())
drawLine[index] = SKShapeNode()
line.append(drawLine[index])
line[index].path = path
line[index].strokeColor = UIColor.redColor()
line[index].lineWidth = 5.0
line[index].physicsBody = SKPhysicsBody(rectangleOfSize: line[index].frame.size)
line[index].physicsBody.dynamic = false
line[index].zPosition = 1
self.addChild(line[index])
index++
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
----------编辑----------
我现在将这行代码用于我的行的物理:
line[index].physicsBody = SKPhysicsBody(rectangleOfSize:
CGSizeMake(line[index].frame.width*2 ,line[index].frame.height*2))
我认为这里的问题是,当我以一定角度绘制直线时,矩形不会随之旋转,因此我的矩形最终会变大。据我所知,touchesbeent返回一组触摸,一组触摸粗略地表示,因此您希望通过它们循环或通过其索引访问特定触摸。我建议像下面的代码一样通过触摸循环
override func touchesBegan(touches:NSSet) {
for touch: AnyObject in touches {
var location:CGPoint = touch.locationInNode(self.scene)
performSomethingCrazy(withLocationAt: location)
}
}
享受。根本不起作用不是问题描述。描述正在发生的事情和应该发生的事情。没有发生任何事情,玩家只是摔倒,根本没有碰撞