• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

如何在水平方向来回移动角色

IOS 来源:Butterfly 22次浏览

当前试图让怪物角色横向来回移动。我刚刚开始学习上周的时候对Swift非常陌生,而且还没有完全掌握OOP。那么如何在调用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方法,但不知道如何)。

===========解决方案如下:

要理解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位置从未改变应该是相同向左移动和移动right.to停止repeatforever行动使用本。

sprite.removeAllActions() 

版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)