加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

无法在SpriteKit场景中使用自定义类(swift&xcode7 beta)

发布时间:2020-12-14 17:55:12 所属栏目:百科 来源:网络整理
导读:我不确定它是否是一个bug,但即使我在其中一个Sprite上设置了一个自定义类名,它似乎完全被忽略了. 我尝试使用拖动的资源,然后使用空节点,两者都完全忽略了Monkey类关联,只创建了一个原始的SKSpriteNode. Monkey Node代码如下. import Foundationimport Sprite
我不确定它是否是一个bug,但即使我在其中一个Sprite上设置了一个自定义类名,它似乎完全被忽略了.

我尝试使用拖动的资源,然后使用空节点,两者都完全忽略了Monkey类关联,只创建了一个原始的SKSpriteNode.

Monkey Node代码如下.

import Foundation
import SpriteKit

class Monkey: SKSpriteNode{

    let monkeyWalkAtlas = SKTextureAtlas(named: "MonkeyWalk")

    override init(texture: SKTexture?,color: NSColor,size: CGSize) {
        super.init(texture: texture,color: color,size: size)
        print("Monkey.init debug")
    }

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

注意:.sks节点上的自定义类是xcode7中的新增类.

2015年6月21日编辑

我简化了设置,但仍然是在iOS9模拟器中编译/运行的问题.我使用默认的SpriteKit模板创建了一个项目,将GameSence命名为以下(以及空的Monkey类)

import SpriteKit

class GameScene: SKScene {

    override func touchesBegan(touches: Set<UITouch>,withEvent event: UIEvent?) {
        print("touch down")
       /* Called when a touch begins */
        for node in self.children {
            print(node)
        }
    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }
}

public class Monkey: SKSpriteNode {}

在GameScene.sks上拖动一个Empty节点,将自定义类设置为Monkey,但是当我触摸下来打印子节点时,我再次得到:

touch down
<SKSpriteNode> name:'SKNode_0' texture:['nil'] position:{753.5,333.5} scale:{1.00,1.00} size:{0,0} anchor:{0,0} rotation:0.00

解决方法

在Swift中,您始终必须在类名之前添加模块名称.您可以在“产品模块名称”字段的构建设置中找到模块名称. 如果在项目名称中使用“ – ”,模块名称会将“ – ”转换为“_”.(在我的测试项目“Test-Game”中,模块名称为“Test_Game”,因此我必须使用“Test_Game.Monkey”作为自定义类,适用于当前Xcode Beta)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读