如何在SpriteKit中为PhysicalWorld设置contactDelegate? [英] How to set contactDelegate for physicsWorld in SpriteKit?

查看:111
本文介绍了如何在SpriteKit中为PhysicalWorld设置contactDelegate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将contactDelegate分配给self(GameScene)时遇到了一个问题.

I have just encountered a problem when assigning contactDelegate to self (GameScene).

override func didMoveToView(view: SKView)
{
    self.physicsWorld.contactDelegate = self
    /* ... */
}

不幸的是,我得到一个错误:

Unfortunately, I get an error:

不能将类型'GameScene'的值分配给类型'SKPhysicsContactDelegate吗?'

Cannot assign a value of type 'GameScene' to value of type 'SKPhysicsContactDelegate?'

在以前版本的Xcode(可能是Swift 2.0中的新功能)中不会发生这种情况,因此即使在Apple Developer上的SpriteKit文档中,它们也具有与我相同的代码.

This wouldn't happen in previous versions of Xcode (probably something new in Swift 2.0), so even in SpriteKit documentation on Apple Developer they have the same code I do.

我该如何解决?我已经尝试过强制投射

How do I get around this? I have already tried to force casting

self.physicsWorld.contactDelegate = (self as! SKPhysicsContactDelegate)

但是我遇到了运行时错误(无法执行广播).

but I got a runtime error (casting couldn't be performed).

有人知道如何将联系人委派给我的GameScene吗?

Anyone knows how to delegate contacts to my GameScene?

推荐答案

我知道在我的项目中(我假设是swift 1),您必须像这样将游戏场景定义为SKPhysicsContact Delegate.

I know in my project (swift 1 i assume) you have to define the game scene as a SKPhysicsContact Delegate like so.

class GameScene: SKScene, SKPhysicsContactDelegate {

}

然后将其设置为我刚刚做的代表.

And to set it as the delegate I just do.

self.physicsWorld.contactDelegate = self

不需要作为"运算符.

这篇关于如何在SpriteKit中为PhysicalWorld设置contactDelegate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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