我试图从屏幕的顶部做一个方形的下降。但是当它下降一半时,它会一直在屏幕上反弹。我没有在屏幕中间创建一个对象。我不明白为什么它在屏幕中间弹跳。SKSpriteNode弹跳的东西,我没有在Swift中创建
这里的运行代码的链接,我的视频:http://sendvid.com/enm9l1np
下面的代码:
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var animator : UIDynamicAnimator?
let BlueSquare = SKSpriteNode()
let RedSquare = SKSpriteNode()
var scorenumber = 0
var Ground = SKSpriteNode()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
physicsWorld.gravity = CGVector(dx: 1.0, dy: -9.0)
let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
// 2. Set the friction of that physicsBody to 0
borderBody.friction = 0
// 3. Set physicsBody of scene to borderBody
self.physicsBody = borderBody
let width = UInt32(self.frame.size.width)
let X = arc4random() % width
Ground = SKSpriteNode(imageNamed: "Ground")
Ground.setScale(1.0)
Ground.position = CGPoint(x: self.frame.width/2, y: 5)
Ground.physicsBody = SKPhysicsBody(rectangleOfSize: Ground.size)
Ground.physicsBody!.affectedByGravity = false
Ground.physicsBody!.dynamic = false
Ground.zPosition = 3
self.addChild(Ground)
//INizialize
animator = UIDynamicAnimator(referenceView: self.view!)
//Add Gravity
//animator?.addBehavior(gravity, sprite)
BlueSquare.size = CGSize(width: 100, height: 100)
BlueSquare.position = CGPoint(x: CGFloat(X), y: 1000)
BlueSquare.physicsBody?.affectedByGravity = true
BlueSquare.physicsBody?.dynamic = false
BlueSquare.name = "bluecube"
BlueSquare.physicsBody = SKPhysicsBody(circleOfRadius: 20)
BlueSquare.color = SKColor.blueColor()
// BlueSquare.physicsBody?.velocity = CGVectorMake(0, 0)
//BlueSquare.physicsBody?.applyImpulse(CGVectorMake(0, -90))
self.addChild(BlueSquare)
let X2 = arc4random() % width
RedSquare.size = CGSize(width: 100, height: 100)
RedSquare.position = CGPoint(x: CGFloat(X2), y: 1000)
RedSquare.physicsBody?.affectedByGravity = true
RedSquare.physicsBody?.dynamic = true
RedSquare.name = "redcube"
RedSquare.physicsBody = SKPhysicsBody(circleOfRadius: 20)
RedSquare.hidden = false
RedSquare.color = SKColor.redColor()
self.addChild(RedSquare)
var gameTimer = NSTimer!()
gameTimer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "spawning1", userInfo: nil, repeats: true)
}
func spawning1() {
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for touch in touches {
// let location = touch.locationInNode(self)
let currentPoint = touch.locationInNode(self)
let currentNode = nodeAtPoint(currentPoint)
let nodeName: String? = currentNode.name
if nodeName == "bluecube" {
BlueSquare.hidden = true
score()
}
if nodeName == "redcube" {
RedSquare.hidden = true
score()
}
}
}
func score() {
scorenumber++
print(scorenumber)
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
if CGRectIntersectsRect(RedSquare.frame, Ground.frame) {
RedSquare.position = CGPoint(x: RedSquare.position.x, y: RedSquare.position.y + 10)
}
}
}
请不要用大写字母开头变量名。变量名称(和方法名称)是'camelCase',类名称是'CamelCase'。您可以看到格式化程序将您的变量名写入蓝色,因为它认为它们是类名。 – Hamish