使用Swift使SKSpriteNode子类 [英] Make SKSpriteNode subclass using Swift

查看:116
本文介绍了使用Swift使SKSpriteNode子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建类,它是SKSpriteNode的子类,我想向其添加其他属性和功能.但是在第一步中,我遇到了一个错误. 这是我的代码:

I'm trying to create class which is a subclass of SKSpriteNode and I want to add other properties and functions to it. But in the first step I faced an error. Here's my code:

import SpriteKit

class Ball: SKSpriteNode {
    init() {
        super.init(imageNamed: "ball")
    }
}

这不是编译错误,它是运行时错误.它说:Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)fatal error: use of unimplemented initializer 'init(texture:)' for class Project.Ball.

It's not a compile error, It's run-time error. It says: Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) and fatal error: use of unimplemented initializer 'init(texture:)' for class Project.Ball.

我该如何解决?

推荐答案

您必须为SKSpriteNode调用指定的初始化程序.我真的很惊讶,您没有收到关于未完全实现SKSpriteNode的另一个错误,您是否正在使用较旧的Xcode6 beta?

You have to call the designated initializer for SKSpriteNode. I'm actually surprised you didn't get another error about not full implementing SKSpriteNode, are you using an older Xcode6 beta maybe?

由于必须使用指定的初始化程序,因此需要调用super.init(texture: '', color: '', size: '').

Since you have to use the designated initializer, you need to call super.init(texture: '', color: '', size: '').

会是这样的:

class Ball: SKSpriteNode {
    init() {
        let texture = SKTexture(imageNamed: "ball")
        super.init(texture: texture, color: nil, size: texture.size())
    }

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

注意: 我还为Xcode所需的NSCoder添加了init.

Note: I also added the init for NSCoder which Xcode will require.

这篇关于使用Swift使SKSpriteNode子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆