我只想在'didSimulatePhysics()'中编码,让鲨鱼在到达屏幕末尾时向相反的方向前进。但是当我尝试在'didSimulatePhysics()'中使用它时,我无法让Swift找到鲨鱼
GameElementsClass:
import SpriteKit
extension GameScene {
func createSharkAtPosition (position:CGPoint, ofType type:SharkType) -> SharkNode {
let node = SharkNode()
let position = CGPoint(x: position.x * scaleFactor, y: position.y)
node.position = position
node.name = "SHARKNODE"
node.sharkType = type
var sprite:SKSpriteNode
sprite = SKSpriteNode(imageNamed: "shark1")
node.addChild(sprite)
node.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size)
node.physicsBody?.dynamic = true
node.physicsBody?.affectedByGravity = false
node.physicsBody?.velocity.dx = CGFloat(-60)
node.physicsBody?.categoryBitMask = CollisionBitMask.Shark
node.physicsBody?.collisionBitMask = 0
return node
}
}
GameSceneClass:
func sharkMaker() {
let randomXPos = arc4random_uniform(170) + 10
let yPosition = CGRectGetMidY(self.frame) + 200 * CGFloat(counter)
counter += 1
let shark = createSharkAtPosition(CGPoint(x: CGFloat(randomXPos), y: yPosition - 180), ofType: SharkType.normalShark)
foreground.addChild(shark)
}
override func didSimulatePhysics() {
if node.position.x < -10 {
node.xScale = -1
node.physicsBody?.velocity.dx = CGFloat(50)
}else if (node.position.x > self.size.width + 10) {
node.xScale = 1
node.physicsBody?.velocity.dx = CGFloat(-50)
}
答案 0 :(得分:0)
shark
变量是sharkMaker
的本地变量,我想你的代码不能编译,或者?要么使用属性来存储鲨鱼,要么将它命名为shark.name="Sharky" and then use
让shark = childNodeByName(&#34; Sharky&#34;)`在层次结构的某处找到它。