2017-12-18 12 views
-1

현재 몬스터 캐릭터가 화면을 가로 질러 앞뒤로 가로 질러 움직 이도록 노력하고 있습니다. 나는 방금 지난 주에 배우기 시작했고 또한 OOP를 정확하게 마스터하지 않았기 때문에 Swift에 매우 새로운입니다. 그렇다면 addEnemy 함수를 호출하는 동안 enemyStaysWithinPlayableArea 함수를 제대로 호출하려면 어떻게해야합니까? Heres는 지금까지 무엇을 : 화면의 오른쪽 가장자리에 화면 전체신속하게 수평으로 문자를 앞뒤로 이동하는 방법

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 메서드를 사용하지만 확실하지 방법을 계속)에 계속 도움이 필요합니다.

답변

0

spritekit에서 이미지의 움직임을 이해하려면이 예제를 확인하십시오. 수평 이동 y 위치에 대한

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]))) 

결코이 왼쪽으로 이동에서와 동일하게 동작하고 repeatforever 사용이 중지 right.to 움직여야 변경되지 않았다.

sprite.removeAllActions() 
0

완성 처리기로 각각의 SKActions.And 실행 동작으로 moveToMax와 moveToMin의 두 함수를 만듭니다.

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