Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何在自定义类中从viewcontroller访问变量?_Swift_Xcode_Class_Realitykit - Fatal编程技术网

Swift 如何在自定义类中从viewcontroller访问变量?

Swift 如何在自定义类中从viewcontroller访问变量?,swift,xcode,class,realitykit,Swift,Xcode,Class,Realitykit,我需要在我的锚上附加一些东西。 我试图在我的类的函数中实现这一点,但我在viewcontroller中声明了这个锚 这个锚在我的班上不被认出来。我如何到达这个锚点 viewController.swift: import UIKit import RealityKit import Combine class ViewController: UIViewController { @IBOutlet var arView: ARView! override func view

我需要在我的锚上附加一些东西。 我试图在我的类的函数中实现这一点,但我在viewcontroller中声明了这个锚

这个锚在我的班上不被认出来。我如何到达这个锚点

viewController.swift:

import UIKit
import RealityKit
import Combine

class ViewController: UIViewController {

    @IBOutlet var arView: ARView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let gameAnchor = AnchorEntity(plane: .horizontal)
        arView.scene.addAnchor(gameAnchor)

        //generate random width en distance
        let randomDistance = Double.random(in: 0.07...0.25)
        let randomWidth = Double.random(in: 0.01...0.05)

        //make a new platform
        let newPlatform = Platform(width: randomWidth, heigth: 0.1, depth: 0.05, distance: randomDistance)
        newPlatform.makePlatform()
    }
}
import Foundation
import RealityKit

class Platform {
    var width: Double = 0.05
    var heigth: Double = 0.1
    var depth: Double = 0.05
    var distance: Double = 0.1

    init(width: Double, heigth: Double, depth: Double, distance: Double) {
        self.width = width
        self.heigth = heigth
        self.depth = depth
        self.distance = distance
    }

    func makePlatform() {
        let platformMesh = MeshResource.generateBox(width: Float(width), height: 0.1, depth: 0.05)
        let platformMaterial = SimpleMaterial(color: .red, isMetallic: false)
        let newPlatform = ModelEntity(mesh: platformMesh, materials: [platformMaterial])
        newPlatform.position.x = Float(distance)
        newPlatform.position.y = 0.05

        //append it to my anchor
        ///gameAnchor.addChild(newPlatform)
    }
}
我的班级:

import UIKit
import RealityKit
import Combine

class ViewController: UIViewController {

    @IBOutlet var arView: ARView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let gameAnchor = AnchorEntity(plane: .horizontal)
        arView.scene.addAnchor(gameAnchor)

        //generate random width en distance
        let randomDistance = Double.random(in: 0.07...0.25)
        let randomWidth = Double.random(in: 0.01...0.05)

        //make a new platform
        let newPlatform = Platform(width: randomWidth, heigth: 0.1, depth: 0.05, distance: randomDistance)
        newPlatform.makePlatform()
    }
}
import Foundation
import RealityKit

class Platform {
    var width: Double = 0.05
    var heigth: Double = 0.1
    var depth: Double = 0.05
    var distance: Double = 0.1

    init(width: Double, heigth: Double, depth: Double, distance: Double) {
        self.width = width
        self.heigth = heigth
        self.depth = depth
        self.distance = distance
    }

    func makePlatform() {
        let platformMesh = MeshResource.generateBox(width: Float(width), height: 0.1, depth: 0.05)
        let platformMaterial = SimpleMaterial(color: .red, isMetallic: false)
        let newPlatform = ModelEntity(mesh: platformMesh, materials: [platformMaterial])
        newPlatform.position.x = Float(distance)
        newPlatform.position.y = 0.05

        //append it to my anchor
        ///gameAnchor.addChild(newPlatform)
    }
}

在newPlatform.makePlatform()之后添加如下内容

 gameAnchor.addChild(newPlatform.platform)

并使平台成为类变量,而不是makePlatform方法的局部变量。您还需要使newPlatform成为视图控制器中的类变量,而不是viewDidLoad的本地变量。

如果以前有人问过这一问题,我很抱歉,但我已经搜索了2个小时,无法找到适合我特定情况的解决方案。如果你们中有人能提供到解决我问题的另一个问题的链接,我将删除这个问题。你在哪里声明了你的AnchoreEntity?在我的viewDidLoad主viewControllerWait中,你希望平台有
AnchoreEntity
否,在我的平台类中,我有一个函数“makePlatform”。这就是我制作平台的地方,你可以在评论中看到一些东西:这就是我想将平台添加到我的锚的地方。但是锚点在那里是未知的,因为它是在主viewcontroller的viewdidload中声明的。我尝试了这个方法,但在我测试应用程序时,它实际上没有显示在我的AR视图中。对不起,您还需要将newPlatform设为类变量,而不是viewdidload的本地变量。(编辑回答)哦,不,对不起,这是我的错。我将platform设置为类变量,但我忘记删除在makePlatform方法中声明平台模型实体的“let”。所以我刚刚在那里宣布了一个新的。固定的