gpt4 book ai didi

swift - 从另一个类创建对象时,对象不会生成

转载 作者:行者123 更新时间:2023-11-30 12:11:58 26 4
gpt4 key购买 nike

所以,它非常像这样:我有一个可以工作的点击手势识别器,这是 GameViewController 类中的代码:

@IBAction func handleTap(_ sender: UITapGestureRecognizer) 
{
GameScene().makeCirc();
}

我调用的函数位于 GameScene 类中,它看起来像这样:

public func makeCirc() {

circle = SKShapeNode (circleOfRadius: 15)
circle.name = "white circ"
circle.position = CGPoint (x: 0, y: 10)
circle.fillColor = .white
circle.physicsBody = SKPhysicsBody(circleOfRadius: 15)

circle.physicsBody?.isDynamic = true
circle.physicsBody?.affectedByGravity = false
circle.physicsBody?.linearDamping = 0
circle.physicsBody?.restitution = 1.0
circle.physicsBody?.allowsRotation = true
circle.physicsBody?.mass = 300
circle.physicsBody?.density = 2*3.14*15/(circle.physicsBody?.mass)!
circle.physicsBody?.angularDamping = 0.0

self.addChild(circle)
circle.physicsBody?.applyImpulse(CGVector(dx: 0.0, dy:1.0))
print(circCount)
circCount += 1
}

我在更新功能上还有一个计时器,这样每隔一小会儿就会产生一个球。当我点击屏幕时,还必须生成一个球。这是代码:

override func update(_ currentTime: TimeInterval)
{
timer += 1
if timer > spawntime {
makeCirc()
timer = 0
}
if circCount > 0 {
if (self.childNode(withName: "white circ")?.position.y)! > self.frame.maxY - 100 {
self.childNode(withName: "white circ")?.removeFromParent()
circCount -= 1
}
}
}

然而,输出有点奇怪。不敲击时,输出是 circCount,通常是常数 12,这才是它应该的样子。然而,当我点击时,显示的 circCount 为 0,而不是生成球并使 circCount 为 13+,但是,所有对象仍然在屏幕上。

是否有办法在 circCount 不为 0 的情况下从另一个类生成球?谢谢!

<小时/>

编辑:这是 viewDidMove 函数,如果有帮助的话:

override func viewDidLoad() {
super.viewDidLoad()
// Load 'GameScene.sks' as a GKScene. This provides gameplay related content
// including entities and graphs.
self.view.isUserInteractionEnabled = true
let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(handleTap(_:)))
self.view.addGestureRecognizer(tapGesture)
if let scene = GKScene(fileNamed: "GameScene") {

// Get the SKScene from the loaded GKScene
if let sceneNode = scene.rootNode as! GameScene? {
// Set the scale mode to scale to fit the window
sceneNode.scaleMode = .aspectFill

// Present the scene
if let view = self.view as! SKView? {
view.presentScene(sceneNode)

view.ignoresSiblingOrder = true

view.showsFPS = true
view.showsNodeCount = true
}
}

}
}

最佳答案

好吧,在完成下面的操作后,一切都正常了。感谢您的帮助!

let scene = GKScene (fileNamed: "GameScene")
var instance = GameScene()
override func viewDidLoad() {
super.viewDidLoad()
let sceneNode = scene?.rootNode as! GameScene?
// Load 'GameScene.sks' as a GKScene. This provides gameplay related content
// including entities and graphs.

let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(handleTap(_:)))
self.view.addGestureRecognizer(tapGesture)
self.view.isUserInteractionEnabled = true
// Get the SKScene from the loaded GKScene
// Set the scale mode to scale to fit the window
sceneNode?.scaleMode = .aspectFill
instance = (sceneNode)!
// Present the scene
if let view = self.view as! SKView? {
view.presentScene(sceneNode)
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true


}
}

关于swift - 从另一个类创建对象时,对象不会生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45943477/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com