所以,我有这个项目here这个菜单,但只是更新了swift的最新语法。我遇到的问题是,每次加载菜单所在的场景时,它都会从数组的中间开始,而不是像开头一样。
我想要实现的是当我用其中的菜单加载场景时我希望它从数组的开始而不是数组的中间开始放置和定位spritenodes的主要功能场景如下:
func createPlayers() {
for _ in 0..<9 {
let player = SKSpriteNode(color: UIColor.blue , size: CGSize(width: 100, height: 200))
players.append(player)
}
}
func placePlayersOnPositions() {
for i in 0..<players.count/2 {
players[i].position = CGPoint(x: leftGuide, y: size.height/2)
}
players[players.count/2].position = CGPoint(x: size.width/2, y: size.height/2)
for i in players.count/2 + 1..<players.count {
players[i].position = CGPoint(x: rightGuide, y: size.height/2)
}
for player in players {
player.setScale(calculateScaleForX(x: player.position.x))
self.addChild(player)
}
}
如何修改上面的代码,使其在数组的开头而不是中间启动菜单,如下所示:
以下是完整的菜单代码:
class GameScene: SKScene {
enum Zone {
case Left, Center, Right
}
var players = [SKSpriteNode]()
var leftPlayer: SKSpriteNode?
var centerPlayer: SKSpriteNode?
var rightPlayer: SKSpriteNode?
var leftGuide : CGFloat {
return round(view!.bounds.width / 6.0)
}
var rightGuide : CGFloat {
return view!.bounds.width - leftGuide
}
var gap : CGFloat {
return (size.width / 2 - leftGuide) / 2
}
// Initialization
override init(size: CGSize) {
super.init(size:size)
createPlayers()
centerPlayer = players[players.count/2]
setLeftAndRightPlayers()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func didMove(to view: SKView) {
placePlayersOnPositions()
calculateZIndexesForPlayers()
}
func createPlayers() {
for _ in 0..<9 {
let player = SKSpriteNode(color: UIColor.blue , size: CGSize(width: 100, height: 200))
players.append(player)
}
}
func placePlayersOnPositions() {
for i in 0..<players.count/2 {
players[i].position = CGPoint(x: leftGuide, y: size.height/2)
}
players[players.count/2].position = CGPoint(x: size.width/2, y: size.height/2)
for i in players.count/2 + 1..<players.count {
players[i].position = CGPoint(x: rightGuide, y: size.height/2)
}
for player in players {
player.setScale(calculateScaleForX(x: player.position.x))
self.addChild(player)
}
}
// Helper functions
func calculateScaleForX(x:CGFloat) -> CGFloat {
let minScale = CGFloat(0.5)
if x <= leftGuide || x >= rightGuide {
return minScale
}
if x < size.width/2 {
let a = 1.0 / (size.width - 2 * leftGuide)
let b = 0.5 - a * leftGuide
return (a * x + b)
}
let a = 1.0 / (frame.size.width - 2 * rightGuide)
let b = 0.5 - a * rightGuide
return (a * x + b)
}
func calculateZIndexesForPlayers() {
var playerCenterIndex : Int = players.count / 2
for i in 0..<players.count {
if centerPlayer == players[i] {
playerCenterIndex = i
}
}
for i in 0...playerCenterIndex {
players[i].zPosition = CGFloat(i)
}
for i in playerCenterIndex+1..<players.count {
players[i].zPosition = centerPlayer!.zPosition * 2 - CGFloat(i)
}
}
func movePlayerToX(player: SKSpriteNode, x: CGFloat, duration: TimeInterval) {
let moveAction = SKAction.moveTo(x: x, duration: duration)
let scaleAction = SKAction.scale(to: calculateScaleForX(x: x), duration: duration)
player.run(SKAction.group([moveAction, scaleAction]))
}
func movePlayerByX(player: SKSpriteNode, x: CGFloat) {
let duration = 0.01
if player.frame.midX <= rightGuide && player.frame.midX >= leftGuide {
player.run(SKAction.moveBy(x: x, y: 0, duration: duration), completion: {
player.setScale(self.calculateScaleForX(x: player.frame.midX))
})
if player.frame.midX < leftGuide {
player.position = CGPoint(x: leftGuide, y: player.position.y)
} else if player.frame.midX > rightGuide {
player.position = CGPoint(x: rightGuide, y: player.position.y)
}
}
}
func zoneOfCenterPlayer() -> Zone {
let gap = size.width / 2 - leftGuide
switch centerPlayer!.frame.midX {
case let x where x < leftGuide + gap/2:
return .Left
case let x where x > rightGuide - gap/2:
return .Right
default: return .Center
}
}
func setLeftAndRightPlayers() {
var playerCenterIndex : Int = players.count / 2
for i in 0..<players.count {
if centerPlayer == players[i] {
playerCenterIndex = i
}
}
if playerCenterIndex > 0 && playerCenterIndex < players.count {
leftPlayer = players[playerCenterIndex-1]
} else {
leftPlayer = nil
}
if playerCenterIndex > -1 && playerCenterIndex < players.count-1 {
rightPlayer = players[playerCenterIndex+1]
} else {
rightPlayer = nil
}
}
// Touch interactions
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch: UITouch = touches.first!
let node = self.atPoint(touch.location(in: self))
if node == centerPlayer {
let fadeOut = SKAction.fadeAlpha(to: 0.5, duration: 0.15)
let fadeIn = SKAction.fadeAlpha(to: 1, duration: 0.15)
centerPlayer!.run(fadeOut, completion: { self.centerPlayer!.run(fadeIn) })
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let duration = 0.01
let touch: UITouch = touches.first!
let newPosition = touch.location(in: self)
let oldPosition = touch.previousLocation(in: self)
let xTranslation = newPosition.x - oldPosition.x
if centerPlayer!.frame.midX > size.width/2 {
if (leftPlayer != nil) {
let actualTranslation = leftPlayer!.frame.midX + xTranslation > leftGuide ? xTranslation : leftGuide - leftPlayer!.frame.midX
movePlayerByX(player: leftPlayer!, x: actualTranslation)
}
} else {
if (rightPlayer != nil) {
let actualTranslation = rightPlayer!.frame.midX + xTranslation < rightGuide ? xTranslation : rightGuide - rightPlayer!.frame.midX
movePlayerByX(player: rightPlayer!, x: actualTranslation)
}
}
movePlayerByX(player: centerPlayer!, x: xTranslation)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
//func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
let touch: UITouch = touches.first!
let duration = 0.25
switch zoneOfCenterPlayer() {
case .Left:
if (rightPlayer != nil) {
movePlayerToX(player: centerPlayer!, x: leftGuide, duration: duration)
if (leftPlayer != nil) {
movePlayerToX(player: leftPlayer!, x: leftGuide, duration: duration)
}
if (rightPlayer != nil) {
movePlayerToX(player: rightPlayer!, x: size.width/2, duration: duration)
}
centerPlayer = rightPlayer
setLeftAndRightPlayers()
} else {
movePlayerToX(player: centerPlayer!, x: size.width/2, duration: duration)
}
case .Right:
if (leftPlayer != nil) {
movePlayerToX(player: centerPlayer!, x: rightGuide, duration: duration)
if (rightPlayer != nil) {
movePlayerToX(player: rightPlayer!, x: rightGuide, duration: duration)
}
if (leftPlayer != nil) {
movePlayerToX(player: leftPlayer!, x: size.width/2, duration: duration)
}
centerPlayer = leftPlayer
setLeftAndRightPlayers()
} else {
movePlayerToX(player: centerPlayer!, x: size.width/2, duration: duration)
}
case .Center:
movePlayerToX(player: centerPlayer!, x: size.width/2, duration: duration)
if (leftPlayer != nil) {
movePlayerToX(player: leftPlayer!, x: leftGuide, duration: duration)
}
if (rightPlayer != nil) {
movePlayerToX(player: rightPlayer!, x: rightGuide, duration: duration)
}
}
calculateZIndexesForPlayers()
}
}
答案 0 :(得分:1)
我只是快速查看并没有尝试过,但这可能有效
func placePlayersOnPositions() {
players.first?.position = CGPoint(x: size.width/2, y: size.height/2)
if players.count > 1 {
for i in 1..<players.count {
players[i].position = CGPoint(x: rightGuide, y: size.height/2)
}
}
for player in players {
player.setScale(calculateScaleForX(x: player.position.x))
self.addChild(player)
}
}
答案 1 :(得分:1)
对于初学者,你必须改变你的1,3,8,10..etc.
功能,不要将中心球员的硬编码除以2而不是玩家零。
这
init
到
centerPlayer = players[players.count/2]