当点击屏幕时,球在空中弹跳,如果高于某一高度,我滚动屏幕。在我的情况下,滚动总是不平滑,有一些滞后,任何想法做得更好?非常感谢你,如果你有更好的想法或其他想法!
以下是代码:
let world = SKNode()
var player : SKSpriteNode!
var firsttap = true
override func didMoveToView(view: SKView) {
physicsWorld.gravity = CGVectorMake(0.0, -9.9)
physicsWorld.contactDelegate = self
self.createPlayer()
addChild(world)
world.addChild(player)
let floor = SKSpriteNode(color: SKColor.blackColor(), size: 320, 20))
floor.physicsBody = SKPhysicsBody(rectangleOfSize: 320, 20))
floor.physicsBody?.dynamic = false
floor.position = CGPointMake(CGRectGetMidX(frame), 0)
addChild(floor)
}
func createPlayer() {
self.player = SKSpriteNode(imageNamed: "Ball")
let body = SKPhysicsBody(circleOfRadius:13.0)
body.dynamic = false
body.contactTestBitMask = PlayerCategory
body.allowsRotation = false
player.physicsBody = body
player.position = CGPointMake(CGRectGetMidX(frame), CGRectGetMidY(frame) * 0.75)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch: AnyObject in touches {
if firsttap {
player.physicsBody?.dynamic = true
firsttap = false
}
let touchLoc = touch.locationInNode(self)
var right = true
if touchLoc.x > CGRectGetMidX(frame) {
right = false
}
if right {
player.physicsBody?.velocity = CGVectorMake(0,0)
player.physicsBody?.applyImpulse(CGVectorMake(-2, 14))
}else{
player.physicsBody?.velocity = CGVectorMake(0,0)
player.physicsBody?.applyImpulse(CGVectorMake(2, 14))
}
}
}
override func update(currentTime: NSTimeInterval) {
let pos : CGFloat = -player.position.y + CGRectGetMidY(frame)
if pos < world.position.y {
world.position.y = pos
}
}
func didBeginContact(contact: SKPhysicsContact) {
let nextLevel = GameScene((size: frame.size))
let transition = SKTransition.fadeWithDuration(0.5)
view?.presentScene(nextLevel, transition:transition)
}
第一次碰巧是平滑的,但是在呈现相同的场景时不是以下的那些,我不明白为什么:(