Передача объекта между сценами

swift

#1

Добрый день, вчера задавала вопрос про разделение файлов Разделить код на разные файлы
не получается объекту текстуру передать

GameScene.swift
import Foundation
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
  var number = 0
  var ballReference: SKShapeNode?
  override func didMoveToView(view: SKView) {
    self.scene?.size = CGSize(width: 640, height: 1136)
    scene?.backgroundColor = UIColor.whiteColor()
  }
  func ballStop(){
    let position = CGPoint(x: scene!.size.width - 50, y: scene!.size.height - 50)
    let ball = Ball(circleOfRadius: 25, position: position)
    self.addChild(ball)
    self.ballReference = ball
  }
  
  CustomScrollView.swift
  import Foundation
  import SpriteKit
  class Ball: SKShapeNode {
    init(circleOfRadius: CGFloat, position: CGPoint){
      super.init()
      let diameter = circleOfRadius * 2
      self.path = CGPathCreateWithEllipseInRect(CGRect(origin: CGPointZero, size: CGSize(width: diameter, height: diameter)), nil)
      self.strokeColor = SKColor.brownColor()
      self.fillColor = SKColor.grayColor()
      self.position = position
      self.physicsBody = SKPhysicsBody(circleOfRadius: 25)
      self.physicsBody?.affectedByGravity = false
    }
    required init?(coder aDecoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
    }}
  // а свойства такие нужны:
  let ball = SKShapeNode(circleOfRadius: 25)
  let tex = self.view?.textureFromNode(ball)
  var ballk = SKSpriteNode(texture: tex)
  textureAtlasBall = SKTextureAtlas(named: "myball")
  for i in 1...textureAtlasBall.textureNames.count{
  let nameBall = "ball_\(i).png"
  textureArrayBall.append(SKTexture(imageNamed: nameBall))
  }
  ballk = SKSpriteNode(imageNamed: textureAtlasBall.textureNames[0])
  ballk.size = CGSize(width: 120, height: 120)
  let animationWithTextureBall = SKAction.animateWithTextures(textureArrayBall, timePerFrame: 0.2)
  let repeatActionForeverBall = SKAction.repeatActionForever(animationWithTextureBall)
  ballk.runAction(repeatActionForeverBall)
  ballk.position = CGPoint(x: scene!.size.width - 320, y: scene!.size.height - 255)
  ballk.physicsBody = SKPhysicsBody(circleOfRadius: 12)
  ballk.physicsBody?.categoryBitMask = PhysicsCategory.Ball1
  ballk.physicsBody?.collisionBitMask = PhysicsCategory.Border | PhysicsCategory.Ball1 | PhysicsCategory.Kvadr | PhysicsCategory.Ball2  | PhysicsCategory.Ball3  | PhysicsCategory.Ball4
  ballk.physicsBody?.contactTestBitMask = PhysicsCategory.Square
  self.addChild(ballk)
  self.ballReference = ballk
  ballk.physicsBody?.affectedByGravity = false

#2

А как вы её передаете?

Должно быть как то так:

class GameScene: SKScene, SKPhysicsContactDelegate {
    var ball: SKShapeNode!
    
    override func didMoveToView(view: SKView) {
        scene?.size = CGSize(width: 640, height: 1136)
        scene?.backgroundColor = UIColor.whiteColor()
        
        let radius: CGFloat = 25
        let position = CGPoint(x: scene!.size.width / 2 - radius, y: scene!.size.height / 2 - radius)
        ball = Ball(circleOfRadius: 25, position: position)
        addChild(ball)
    }
}


class Ball: SKShapeNode {
    convenience init(circleOfRadius: CGFloat, position: CGPoint) {
        self.init()
        self.position = position
        
        let diameter = circleOfRadius * 2
        path = CGPathCreateWithEllipseInRect(CGRect(origin: CGPointZero, size: CGSize(width: diameter, height: diameter)), nil)
        
        // Писать нужные свойства для конкретного Ball
        
        strokeColor = SKColor.brownColor()
        fillColor = SKColor.grayColor()
        physicsBody = SKPhysicsBody(circleOfRadius: circleOfRadius)
        physicsBody!.affectedByGravity = true
    }
}

Вы делаете сабкласс со своей инициализацией, с целью задать нужные свойства при инициализации.


#3

Ну это да, так у меня получилось, не получается именно анимацию передать, вот эту часть

  let tex = SKTexture()
  var ballk = SKSpriteNode(texture: tex)
  textureAtlasBall = SKTextureAtlas(named: "myball")
  for i in 1...textureAtlasBall.textureNames.count{
  let nameBall = "ball_\(i).png"
  textureArrayBall.append(SKTexture(imageNamed: nameBall))
  }
  ballk = SKSpriteNode(imageNamed: textureAtlasBall.textureNames[0])
  ballk.size = CGSize(width: 120, height: 120)
  let animationWithTextureBall = SKAction.animateWithTextures(textureArrayBall, timePerFrame: 0.2)
  let repeatActionForeverBall = SKAction.repeatActionForever(animationWithTextureBall)
  ballk.runAction(repeatActionForeverBall)

#4

Скиньте ваш myball.atlas


#5

#6

Пробуйте так:

class GameScene: SKScene, SKPhysicsContactDelegate {
    var ball: Ball!
    
    override func didMoveToView(view: SKView) {
        scene?.size = CGSize(width: 640, height: 1136)
        scene?.backgroundColor = UIColor.whiteColor()
        
        let size = CGSize(width: 120, height: 120)
        let position = CGPoint(x: scene!.size.width / 2 - size.width / 2, y: scene!.size.height / 2 - size.height / 2)
        ball = Ball(size: size, position: position, textureNamed: "myball")
        ball.runAnimation()
        addChild(ball)
    }
}


class Ball: SKSpriteNode {
    
    var textures = [SKTexture]()
    
    convenience init(size: CGSize, position: CGPoint, textureNamed: String) {
        self.init()
        self.position = position
        self.size = size
        
        for (index, name) in SKTextureAtlas(named: textureNamed).textureNames.enumerate() {
            let texture = SKTexture(imageNamed: name)
            if index == 0 { self.texture = texture }
            textures.append(texture)
        }
    }
    
    func runAnimation() {
        let animationWithTextureBall = SKAction.animateWithTextures(textures, timePerFrame: 0.2)
        let repeatActionForeverBall = SKAction.repeatActionForever(animationWithTextureBall)
        runAction(repeatActionForeverBall)
    }
}

#7

Помогли решить вопрос, спасибо)