为什么SpriteKit的Showfields属性无法正常工作?

发布于 2025-01-24 18:22:09 字数 2165 浏览 3 评论 0 原文

在SpriteKit构建游戏时,我使用代码> 具有负面强度以模拟正电荷的电场。为了可视化字段及其互动,我使用代码> 属性,它运行正常。

现在,尝试实施负电荷,我尝试将所有字段切换为 。字段和粒子(吸引力和排斥)的行为继续完美地工作,但是 Showfields 创建的视觉表示停止工作。

调查,视觉表示仅适用于 radialgravityfield ,并且对所有其他 skfieldnode 选项。我试图改变田野的力量,但什么都没有。除了田野物理效果很好,我还使用 skphysicsworld.sample fields(at:) ,它们是正确的。

示例

基于Xcode的iOS游戏默认项目的 ,以下此 GAMESCENE 可以重现该行为。从 skfieldnode.radialgravityfield()更改为 skfieldnode.electricfield()使字段未显示。

import SpriteKit
import GameplayKit

class GameScene: SKScene {

    private var fieldNode : SKFieldNode?
    
    override func didMove(to view: SKView) {
        view.showsFields = true
        
        self.fieldNode = SKFieldNode.radialGravityField() // Change field type here
        
        if let fieldNode = self.fieldNode {
            fieldNode.strength = 2
        }
    }

    func touchDown(atPoint pos : CGPoint) {
        if let n = self.fieldNode?.copy() as! SKFieldNode? {
            n.position = pos
            self.addChild(n)
        }
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for t in touches { self.touchDown(atPoint: t.location(in: self)) }
    }
}

Building a game in SpriteKit, I used SKFieldNode.radialGravityField() with negative strength to simulate a positive charge's electric field. To visualize the fields and their interaction, I used SKView.showFields property and it was working fine.

Now, trying to implement a negative charge, I tried switching all fields to SKFieldNode.electricField(). The behavior of the fields and particles (attraction and repulsion) continued to work perfectly, but the visual representation created by showFields stopped working.

Investigating, the visual representation worked only for radialGravityField and did nothing for all other SKFieldNode options. I tried changing the fields' strength but couldn't reach anything. In addition to the fields physics working fine, I looked for the fields magnitude using SKPhysicsWorld.sampleFields(at:) and they were correct.

Example

Based on XCode's default project for iOS game, this GameScene below can reproduce the behavior. Changing from SKFieldNode.radialGravityField() to SKFieldNode.electricField() makes the fields not show up.

import SpriteKit
import GameplayKit

class GameScene: SKScene {

    private var fieldNode : SKFieldNode?
    
    override func didMove(to view: SKView) {
        view.showsFields = true
        
        self.fieldNode = SKFieldNode.radialGravityField() // Change field type here
        
        if let fieldNode = self.fieldNode {
            fieldNode.strength = 2
        }
    }

    func touchDown(atPoint pos : CGPoint) {
        if let n = self.fieldNode?.copy() as! SKFieldNode? {
            n.position = pos
            self.addChild(n)
        }
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for t in touches { self.touchDown(atPoint: t.location(in: self)) }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文