2017-03-22 108 views
0

我目前有一個崩潰,我找不到解決方案。SKLabelNode相關EXC_BAD_ACCESS崩潰

在我正在開發的應用程序中,我有一個自定義類LabelNode: SKNode,它使用SKLabelNodeSKSpriteNode在黑色方形背景上創建白色文本。此標籤可以與帶有setText(newText: String)功能新值每幀以下

class LabelNode: SKNode { 

private let textNode: SKLabelNode 
private let backgroundNode: SKSpriteNode 
private let padding: CGFloat 

var width: CGFloat = 0 
var height: CGFloat = 0 
var horizontalAlignment = SKLabelHorizontalAlignmentMode.center 

init(fontSize: CGFloat, text: String? = "A") { 
    textNode = SKLabelNode(fontNamed: "Arial") 
    textNode.zPosition = 1 
    backgroundNode = SKSpriteNode(color: UIColor.black, size: CGSize(width: 0, height: 0)) 
    textNode.fontColor = UIColor.white 
    textNode.fontSize = fontSize 
    textNode.text = text 
    textNode.verticalAlignmentMode = SKLabelVerticalAlignmentMode.top 
    height = textNode.frame.size.height 
    textNode.position.y = (height/2) 
    padding = 0.165 * height 
    width = textNode.frame.size.width 
    backgroundNode.size = CGSize(width: width + padding, height: height + padding) 
    backgroundNode.position.x = textNode.frame.midX 
    super.init() 
    addChild(textNode) 
    addChild(backgroundNode) 
} 

required init?(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented for LabelNode") 
} 

func setText(newText: String) { 
    self.textNode.text = newText 
    width = self.textNode.frame.size.width 
    backgroundNode.size = CGSize(width: width + padding, height: height + padding) 
    backgroundNode.position.x = textNode.frame.midX 
} 

func setFontColor(newFontColor: UIColor) { 
    self.textNode.fontColor = newFontColor 
} 

func setAlignment(newAlignment: SKLabelHorizontalAlignmentMode) { 
    self.textNode.horizontalAlignmentMode = newAlignment 
} 

}更新

我的應用程序不斷拋出EXC_BAD_ACCESS隨機時間間隔,有時有不同的錯誤信息,並且在不同的線程。有時xcode指向該行

width = self.textNode.frame.size.width 

但有時在線程x上的隨機行,但上面的行在線程y中處於活動狀態。

如果殭屍被激活,但兩者All Exceptions斷點和malloc_error_break停用我得到崩潰以下端子輸出

-[SKTexture dealloc]: message sent to deallocated instance 0x10bbaf70 

其中po 0x10bbaf70只給出了輸出

280735600 

和以下堆棧跟蹤

* thread #41: tid = 0x73287, 0x1d9ac4d0 CoreFoundation`___forwarding___ + 532, queue = 'com.apple.scenekit.renderingQueue.applicationName.GameView0x1478d2b0', stop reason = EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT, subcode=0xdefe) 
frame #0: 0x1d9ac4d0 CoreFoundation`___forwarding___ + 532 
frame #1: 0x1d8d13d8 CoreFoundation`_CF_forwarding_prep_0 + 24 
frame #2: 0x1cc25194 libobjc.A.dylib`objc_object::sidetable_release(bool) + 238 
frame #3: 0x2b23ff0a SpriteKit`SKCSpriteNode::~SKCSpriteNode() + 38 
frame #4: 0x2b23ff28 SpriteKit`SKCSpriteNode::~SKCSpriteNode() + 8 
frame #5: 0x2b2204a2 SpriteKit`SKCLabelNode::rebuildText() + 134 
frame #6: 0x2b2201b2 SpriteKit`SKCLabelNode::getBoundingBox() + 34 
frame #7: 0x2b27d248 SpriteKit`-[SKNode frame] + 40 
frame #8: 0x2b297928 SpriteKit`-[SKLabelNode frame] + 40 
* frame #9: 0x000aea54 applicationName`LabelNode.setText(newText="9 555 m", self=0x15863970) ->() + 288 at LabelNode.swift:45 

如果我All Exceptions斷點和malloc_error_break激活我沒有得到任何端子輸出和以下堆棧跟蹤運行

* thread #21: tid = 0x7414f, 0x1cc24612 libobjc.A.dylib`objc_release + 2, queue = 'com.apple.scenekit.renderingQueue.applicationName.GameView0x177651b0', stop reason = EXC_BAD_ACCESS (code=1, address=0x2) 
frame #0: 0x1cc24612 libobjc.A.dylib`objc_release + 2 
frame #1: 0x2b23fefa SpriteKit`SKCSpriteNode::~SKCSpriteNode() + 22 
frame #2: 0x2b23ff28 SpriteKit`SKCSpriteNode::~SKCSpriteNode() + 8 
frame #3: 0x2b2204a2 SpriteKit`SKCLabelNode::rebuildText() + 134 
frame #4: 0x2b2201b2 SpriteKit`SKCLabelNode::getBoundingBox() + 34 
frame #5: 0x2b27d248 SpriteKit`-[SKNode frame] + 40 
frame #6: 0x2b297928 SpriteKit`-[SKLabelNode frame] + 40 
* frame #7: 0x00181a54 applicationName`LabelNode.setText(newText="4 033 m", self=0x17786cb0) ->() + 288 at LabelNode.swift:45 

有誰知道這是什麼崩潰背後的原因是或者任何進一步的步驟我可以採取的一些啓發這個?任何提示將是非常有用的:)

回答

0

你還沒有展示足夠的代碼來完全理解你的問題,從它看起來像你是什麼子類的labelNode,並試圖將spriteNode放在該標籤後面?在我看來,這將是一個更簡單的情況。

class LabelNode : SKSpriteNode { 

init(texture: SKTexture?, color: UIColor, size: CGSize, text: String) { 

    super.init(texture: texture, color: color, size: size) 


    let label = SKLabelNode(fontNamed: "Courier-Bold") 
    label.fontSize = 50 
    label.fontColor = SKColor.black 
    label.position = CGPoint.init(x: 0.0, y: 0.0) 
    label.zPosition = 1 
    label.verticalAlignmentMode = .center 
    label.text = text 
    self.addChild(label) 

} 

required init?(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
} 

     } 

創建一個自定義精靈節點類和直接添加標籤到它。

然後將其添加到視圖中。

 let labelNode = LabelNode(texture: nil, color: .white, size: CGSize.init(width: 400, height: 100) , text: "Enter text") 
    labelNode.position = CGPoint(x: self.frame.midX, y: self.frame.midY) 

    self.addChild(labelNode) 
相關問題