2017-12-29 51 views
0

SKYSapeNode의 하위 클래스에서 초기화 프로그램을 사용할 수 없습니다. 이 문제를 해결하기 위해 여기에 게시 된 예제를 시도했습니다. Adding Convenience Initializers in Swift Subclass. 내가 사용하고있는 코드는 다음과 같습니다. 편의 초기화를 초기화Swift SKShapeNode가 초기화 프로그램을 허용하지 않습니다.

class Ground1 : SKShapeNode { 

override init() { 
    super.init() 
    print("Hello1") 
} 

convenience init(width: CGFloat, point: CGPoint) { 
    var points = [CGPoint(x: -900, y: -300), 
        CGPoint(x: -600, y: -100), 
        CGPoint(x: -100, y: -300), 
        CGPoint(x: 2, y: 150), 
        CGPoint(x: 100, y: -300), 
        CGPoint(x: 600, y: -100), 
        CGPoint(x: 900, y: -300)] 
    self.init(splinePoints: &points, count: points.count) 
    lineWidth = 5 
    strokeColor = UIColor.orange 
    physicsBody = SKPhysicsBody(edgeChainFrom: path!) 
    physicsBody?.restitution = 0.75 
    physicsBody?.isDynamic = false 
    print("Hello2") 
} 

required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
} 

}

()를 무시하고 사용되지 않습니다. 장면에 쉐이프가 추가되지 않습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까 ?

+1

지상 추가를 위해 사용한 코드는 무엇입니까? – Sweeper

+0

나는 gameScene에 인스턴스를 만들고, ground = ground1()이라고하고, 장면에 인스턴스를 추가한다. Addchild (gound). 나는 콘솔에서 print hello1을 얻지 만 프린트는 안된다. hello2 – hoboBob

+0

이'let ground = Ground1 (width : 10, point : CGPoint.zero)'와'addChild (ground)'를 시도해 보라. 'override init() {...}'및'필요한 init {...}'을 삭제할 수 있습니다. – 0x141E

답변

0

0x141E 덕분에 누구의 왕래가 올바른 방향으로 나를 가리켰습니까? 이 코드는 작동하지만 코드가 더러워 보입니다. 이러한 방식으로 초기화에 대한 이유는 여기 Adding Convenience Initializers in Swift Subclass에서 자세하게 설명됩니다. splinePoints를 사용하고 경로에 지정하기 때문에 솔루션을 찾지 못했습니다.

class Ground1 : SKShapeNode { 

    override init() { 
     super.init() 
    } 

    convenience init(name: String) { 
     var points = [CGPoint(x: -900, y: -300), 
         CGPoint(x: -600, y: -100), 
         CGPoint(x: -100, y: -300), 
         CGPoint(x: 2, y: 150), 
         CGPoint(x: 100, y: -300), 
         CGPoint(x: 600, y: -100), 
         CGPoint(x: 900, y: -300)] 
     self.init(splinePoints: &points, count: points.count) 
     lineWidth = 5 
     strokeColor = UIColor.orange 
     physicsBody = SKPhysicsBody(edgeChainFrom: self.path!) 
     physicsBody?.restitution = 0.75 
     physicsBody?.isDynamic = false 
    } 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
    } 
}