UIScreen.mainScreen().bounds.size.height返回正确的数字,但SKSpriteNod



我正在学习SpriteKit,并尝试使用SKAction.moveToY将节点从屏幕上向上移动。我想先让它接近顶部(我想一旦我把它拿到节点的顶部,它就会仍然在屏幕上,我必须调整,但这应该不是问题)。

问题是,我已经尝试使用UIScreen.mainScreen().bounds.size.height和(在didMoveToView方法中)SKView的bounds.size.height。如果我同时使用println,它们会返回顶部的正确值(5s返回568.0)。当我将该值添加到SKAction.moveToY(screenHeightVariable, duration: 2)时,它可以正常工作并移动节点,但它会在大约75%的时间内停止到5s的顶部,在大约90%的时间内停到6+的顶部。有没有一种不同的方法可以让我得到屏幕顶部的Y值?以下是我的一些代码(尽量忽略一些更不合逻辑的东西,比如红色ufo,因为这更多的是学习和错误尝试):

override func didMoveToView(view: SKView) {
    physicsWorld.contactDelegate = self
    println(view.bounds.size.height)
    xVal = self.frame.width/2
    ufoNode = ufo(imageNamed: "ufo")
    ufoNode!.position = CGPoint(x: xVal!, y: 30)
    ufoNode!.physicsBody?.categoryBitMask = charCat

    ufoRedNode = obstacle(imageNamed: "ufo-red")
    ufoRedNode!.position = CGPoint(x: (self.frame.width/2 - 50), y: 30)
    ufoNode!.physicsBody?.categoryBitMask = sceneCat
    self.addChild(ufoRedNode!)
    self.addChild(ufoNode!)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    /* Called when a touch begins */
    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)

        if location.x < self.frame.width/2 {
            // Left
            direction = Selector("moveLeft:")
        } else {
            // Right
            direction = Selector("moveRight:")
        }
        movementTimer = NSTimer.scheduledTimerWithTimeInterval(0.005, target: self, selector: direction!, userInfo: nil, repeats: true)
        self.fireBullet(self)
        fireBulletTimer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "fireBullet:", userInfo: nil, repeats: true)
    }
}
func fireBullet(sender: AnyObject!) {

    var singleBullet: bullet?
    bulletY = ufoNode!.position.y + 10
    if bulletSide == "Left" {
        bulletX = (ufoNode!.position.x - ((ufoNode!.size.width/4) ))
        bulletSide = "Right"
       // println("left")
    } else {
        bulletX = (ufoNode!.position.x + ((ufoNode!.size.width/4) ))
        bulletSide = "Left"
       // println("right")
    }
    singleBullet = bullet(imageNamed: "bullet")
    singleBullet!.position = CGPoint(x: bulletX!, y: bulletY!)
    var bulletAction: SKAction = SKAction.moveToY(screenHeight, duration: 2)

    singleBullet!.runAction(bulletAction)
    self.addChild(singleBullet!)

}
默认情况下,SKSpriteNodeposition是精灵的中点。因此,将node移动到screenHeight,将子画面的中点移动到screenHeightnode的下半部分仍将在屏幕内

如果您想完全移除它,可以尝试将最终位置偏移singleBullet.size.height/2
var bulletAction: SKAction = SKAction.moveToY(screenHeight + singleBullet.size.height/2, duration: 2)

您还可以重新配置更改nodeanchorPoint属性,以更改spriteNode内部的哪个点移动到spriteNode 的position属性

anchorPoint:定义精灵中与节点的位置。您可以在单位中指定此属性的值坐标空间。默认值为(0.5,0.5),这意味着精灵以其位置为中心。

同时将您的NSTimer替换为SKActions。CCD_ 20与CCD_。例如

let action = SKAction.runBlock { () -> Void in
    self.fireBullet(self)
}
let timerAction = SKAction.sequence([action,SKAction.waitForDuration(0.1)])
let foreverTimer = SKAction.repeatActionForever(timerAction)
self.runAction(foreverTimer)

最新更新