本文介绍了sknode看不到添加的子代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我创建了一个包含一些项目的菜单类.我想在主类中显示这些精灵.我通过在touches begin方法中创建一个与sknode类相关联的对象来对此进行了试验,但是当我使用addChild方法将菜单对象添加到主类中时,什么都没显示出来.

Here i created a menu class which contains a few items. I want to display these sprites in the main class. I experimented with this by creating an object associating with the sknode class in the touches began method, but when i added the menu object in the main class using the addChild thing, nothing showed up.

class menu:SKNode {
    let background = SKSpriteNode(imageNamed:"background")
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(){
        super.init()
         var fixedSize = self.frame.width/11
    background.size = CGSizeMake(self.frame.width-fixedSize, self.frame.size.height-fixedSize)
    background.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2)
    self.addChild(background)
    }
}


 //In the main method i said let settings = menu()  self.addChild(settings)   nothing shows up

推荐答案

SKNodeframe属性等于CGRectZero,因此,当您尝试设置背景节点的大小时,它也会结束设为CGRectZero.

The frame property of an SKNode is equal to CGRectZero, so when you try to set the size of your background node it will also end up as CGRectZero.

解决您的问题的一个简单方法是添加自定义初始化程序,并根据场景的大小进行调用.

An easy fix to your problem would be to add custom initializer and call that with the size of the scene.

class menu:SKNode {
    let background = SKSpriteNode(imageNamed:"background")
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    init(size: CGSize) {
        super.init()
        var fixedSize = size.width/11
        background.size = CGSizeMake(size.width-fixedSize, size.height-fixedSize)
        background.position = CGPointMake(size.width/2, size.height/2)
        self.addChild(background)
    }
}

这篇关于sknode看不到添加的子代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 15:05