如何在swift中水平来回移动角色

时间:2017-12-18 04:21:19

标签: ios swift xcode sprite-kit swift4

目前正试图让怪物角色在屏幕上水平来回移动。我刚刚开始学习Swift,并且还没有完全掌握OOP,我对Swift非常陌生。那么如何在调用addEnemy函数时正确调用enemyStaysWithinPlayableArea函数。这是我到目前为止所拥有的:

import SpriteKit

class GameScene: SKScene {
    var monster = SKSpriteNode()
    var monsterMove = SKAction()
    var background = SKSpriteNode()
    var gameArea: CGRect



// MARK: - Set area where user can play
    override init(size: CGSize) {
        // iphones screens have an aspect ratio of 16:9
        let maxAspectRatio: CGFloat = 16.0/9.0
        let playableWidth = size.height/maxAspectRatio
        let margin = (size.width - playableWidth)/2
        gameArea = CGRect(x: margin, y: 0, width: playableWidth, height: size.height)
        super.init(size: size)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}


override func didMove(to view: SKView) {
    // I want to run addEnemy() while it checks for enemyStayWithinPlayableArea()
    // run(SKAction.repeatForever(...)) ??? 
     enemyStayWithinPlayableArea()

    // MARK: - Setting the background
    background = SKSpriteNode(imageNamed: "background")
    background.size = self.size
    // Set background at center of screen
    background.position = CGPoint(x:self.size.width/2 ,y:self.size.height/2)
    // Layering so everything in the game will be in front of the background
    background.zPosition = 0
    self.addChild(background)


}


func addEnemy() {

    // MARK: - Set up enemy and its movements
    monster = SKSpriteNode(imageNamed: "monster")
    monster.zPosition = 5
    // this is where the enemy starts
    monster.position = CGPoint(x: 0, y: 300)
    self.addChild(monster)
    // I want it to move to the end of the screen within 1 sec
    monsterMove = SKAction.moveTo(x: gameArea.maxX, duration: 1.0)
    monster.run(monsterMove)


}

func enemyStayWithinPlayableArea(){
    addEnemy()
    // when the monster reaches the right most corner of the screen
    // turn it back around and make it go the other way
    if monster.position.x == gameArea.maxX{
        monsterMove = SKAction.moveTo(x: gameArea.minX, duration: 1.0)
        monster.run(monsterMove)

    }
    // when the monster reaches the left most corner of the screen
    // same idea as above
    if monster.position.x == gameArea.minX{
        monsterMove = SKAction.moveTo(x: gameArea.maxX, duration: 1.0)
        monster.run(monsterMove)
    }

}


}

到目前为止,敌人在屏幕上移动到屏幕的右边缘,因此该部分完美无缺。我只需要帮助确保它继续循环(可能使用SKAction.repeatForever方法但不确定如何)。

2 个答案:

答案 0 :(得分:0)

要了解spritekit中图像的移动,请查看此示例。

let moveLeft = SKAction.moveTo(CGPoint(x: xpos, y:  ypos), duration: duration)
let moveRight = SKAction.moveTo(CGPoint(x: xpos, y:  ypos), duration: duration)

sprite.runAction(SKAction.repeatActionForever(SKAction.sequence([moveLeft, moveRight])))

对于水平移动y位置从未被改变过它应该与向左移动和向右移动相同。停止重复操作使用它。

sprite.removeAllActions()

答案 1 :(得分:0)

使用相应的SKActions创建两个函数moveToMax和moveToMin。并使用完成处理程序运行操作。

func moveToMin(){ 
    monsterMove = SKAction.moveTo(x: gameArea.minX, duration: 1.0)
    monster.run(monsterMove, completion: {
        moveToMax()
    })
}