Swift - SKAction moveByX,如何让我的节点从顶部/向下移动

时间:2016-01-23 00:05:36

标签: swift sprite-kit swift2

请原谅我,因为我有点像一个Swift noob并正在制作我的第一个游戏:D

好的,我有两个节点......一个是番石榴,另一个是辣椒。番石榴从屏幕底部移动到顶部。我希望Pepper从屏幕顶部移动到底部。这是我的代码,适用于番石榴。

// movement of guavas
let guavaToMove = CGFloat(self.frame.size.width + 2.0  * guavaNodeTexture.size().width)
let moveGuava = SKAction.moveByX(-guavaToMove, y: self.frame.size.width, duration: NSTimeInterval(0.01 * guavaToMove))
let removeGuava = SKAction.removeFromParent()

// movement of peppers
let pepperToMove = CGFloat(self.frame.size.width + 2.0  * pepperNodeTexture.size().width)
let movePepper = SKAction.moveByX(-pepperToMove, y: self.frame.size.width, duration: NSTimeInterval(0.01 * pepperToMove))
let removePepper = SKAction.removeFromParent()

    GuavaMoveAndRemove = SKAction.sequence([moveGuava,removeGuava])

    PepperMoveAndRemove = SKAction.sequence([movePepper,removePepper])

现在我将Guava节点固定在屏幕的底部,它完美地漂浮起来。我将Pepper节点固定在屏幕顶部,并试图让它以类似的方式浮动。

以下是两个函数的代码,spawnGuava和spawnPepper,其中两个节点/纹理都已锚定:

func spawnPepper(){

        //The way the Guava spawn and move

        let pepperNode = SKNode()

        let pepper = SKSpriteNode(texture: pepperNodeTexture)

        pepper.setScale(0.30)
        pepper.position = CGPointMake (self.size.width + 140, self.size.height * 0.75)
        pepper.alpha = 0.75
        pepper.physicsBody = SKPhysicsBody(rectangleOfSize: pepper.size)
        pepper.physicsBody?.affectedByGravity = false
        pepper.physicsBody?.dynamic = true
        pepper.physicsBody?.categoryBitMask = PhysicsCatagory.Guava
        pepper.physicsBody?.collisionBitMask = 1
        pepper.physicsBody?.contactTestBitMask = PhysicsCatagory.Boognish

        pepper.zPosition = 50

        pepperNode.addChild(pepper)


        pepper.runAction(PepperMoveAndRemove)

        self.addChild(pepperNode)


        if scoreIncreased == true {


        }



        //the way the pepper collide

    }


    func spawnGuava(){

        //The way the Guava spawn and move

        let guavaNode = SKNode()

        let guava = SKSpriteNode(texture: guavaNodeTexture)

        guava.setScale(0.75)
        guava.position = CGPointMake (self.size.width - 40, self.size.height * 0.05)
        guava.physicsBody = SKPhysicsBody(rectangleOfSize: guava.size)
        guava.physicsBody?.affectedByGravity = false
        guava.physicsBody?.dynamic = true
        guava.alpha = 0.75
        guava.physicsBody?.categoryBitMask = PhysicsCatagory.Guava
        guava.physicsBody?.collisionBitMask = 1
        guava.physicsBody?.contactTestBitMask = PhysicsCatagory.Boognish

        guava.zPosition = 0

        guavaNode.addChild(guava)


        guava.runAction(GuavaMoveAndRemove)

        self.addChild(guavaNode)


        if scoreIncreased == true {


        }



        //the way the guava collide

    }

请帮我配置相应的代码,以便辣椒从天而降:)

感谢提前。第一篇文章:)

仅供参考:我自己多次尝试修改代码无效,所以我已将其恢复到您现在看到的基线。如果我将辣椒固定在番石榴固定的地方,那么它们会同时漂浮起来。

2 个答案:

答案 0 :(得分:0)

你试图向相反的方向移动番石榴和辣椒,但似乎你正朝着同一方向移动它们。尝试在let movePepper = SKAction.moveByX(-pepperToMove, y: -self.frame.size.width, duration: NSTimeInterval(0.01 * pepperToMove)) 中删除或添加负号:

C:\Program Files (x86)\pgAgent\bin

答案 1 :(得分:0)

//辣椒的运动         让pepperToMove = CGFloat(self.frame.size.width + 2.0 * pepperNodeTexture.size()。width)         let movePepper = SKAction.moveByX(-pepperToMove,y:self.frame.size.height / -1,duration:NSTimeInterval(0.01 * pepperToMove))         让removePepper = SKAction.removeFromParent()

只需要将y值除以负数,derp ...:D