gpt4 book ai didi

ios - SpriteKit 和 Swift : Creating nodes via didBeginContact messes up the positioning

转载 作者:搜寻专家 更新时间:2023-10-31 19:31:38 25 4
gpt4 key购买 nike

我不知道这是 Xcode 中的一个奇怪错误还是 SpriteKit 的坐标系统有什么我不明白的地方。

前提是节点的位置总是相对于它的父节点。但是,每当我从 SKPhysicsContactDelegate 的“didBeginContact”调用创建和定位具有物理体的节点的 block 时,该节点始终相对于场景(而不是其父级)定位。请注意,在除“didBeginContact”以外的任何地方触发时,调用同一个 block 都会按预期工作。另一件事是,如果我移除所述节点的物理主体,即使从“didBeginContact”调用时,该 block 现在也会按预期工作。

我已经被这个问题困扰了两天,如果不提供有关我的实际代码的其他详细信息,那就太麻烦了。所以我做了一个非常简单的项目来演示这个异常。只需使用 Spritekit 模板在 Xcode 6 中创建一个新项目,并将 GameViewController.swift 和 GameSwift.swift 替换为下面发布的代码。只需在 iPad Air 中运行,其他一切都应该是不言自明的。

最后的笔记:

  1. 当您按下第一个按钮并与第二个按钮建立联系时按钮,查看屏幕的左下角。你会看见这些盒子被“错误地”放置在那里。
  2. 尝试删除“AddBox”中盒子的物理实体。它现在将按预期工作。
  3. 如果您认为这是一个错误,或者坐标系或物理世界中有我不理解的地方,请告诉我

GameViewController.swift:

import SpriteKit

class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let scene = GameScene()
scene.size = view.frame.size
let skView = self.view as SKView
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
}

GameScene.swift:

import SpriteKit

let kButtonSize = CGSize(width: 500, height: 100)
let kContainerSize = CGSize(width: 500, height: 300)
let kBoxSize = CGSize(width: 25, height: 25)

class GameScene: SKScene, SKPhysicsContactDelegate {

override func didMoveToView(view: SKView) {
physicsWorld.contactDelegate = self
addFirstButton()
addSecondButton()
addContainer()
}

func addFirstButton() {
let button = SKSpriteNode(color: SKColor.blueColor(), size: kButtonSize)
let label = SKLabelNode(text: "Call 'addBox' from didBeginContact")
button.name = "firstButton"
label.name = "firstLabel"
button.position = CGPointMake(CGRectGetMidX(frame), CGRectGetMidY(frame))
button.physicsBody = SKPhysicsBody(rectangleOfSize: button.size)
button.physicsBody.allowsRotation = false
button.physicsBody.affectedByGravity = false
button.physicsBody.categoryBitMask = 0x1
button.physicsBody.contactTestBitMask = 0x1
button.addChild(label)
addChild(button)
}

func addSecondButton() {
let button = SKSpriteNode(color: SKColor.blueColor(), size: kButtonSize)
let label = SKLabelNode(text: "Call 'addBox' from touchesBegan")
button.name = "secondButton"
label.name = "secondLabel"
button.position = CGPointMake(CGRectGetMidX(frame), CGRectGetMidY(frame)-200)
button.physicsBody = SKPhysicsBody(rectangleOfSize: button.size)
button.physicsBody.dynamic = false
button.physicsBody.categoryBitMask = 0x1
button.physicsBody.contactTestBitMask = 0x1
button.addChild(label)
addChild(button)
}

func addContainer() {
let container = SKSpriteNode(color: SKColor.greenColor(), size:kContainerSize)
let label = SKLabelNode(text: "Created node should fall here")
label.fontColor = SKColor.blackColor()
container.name = "container"
container.physicsBody = SKPhysicsBody(edgeLoopFromRect: container.frame)
container.position = CGPointMake(CGRectGetMidX(frame), CGRectGetMidY(frame)+300)
container.addChild(label)
addChild(container)
}

func addBox() {
let container = childNodeWithName("container")
let box = SKSpriteNode(color: SKColor.blueColor(), size: kBoxSize)
box.physicsBody = SKPhysicsBody(rectangleOfSize: box.size)
box.position = CGPointMake(0, 100)
container.addChild(box)
}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let touch = touches.anyObject() as UITouch
let point = touch.locationInNode(self)
let node = nodeAtPoint(point)
if node.name == nil {return}

switch node.name! {
case "firstButton", "firstLabel":
let button = childNodeWithName("firstButton") as SKSpriteNode
button.physicsBody.applyImpulse(CGVectorMake(0, -500))
case "secondButton", "secondLabel":
addBox()
default:
break
}
}

func didBeginContact(contact: SKPhysicsContact!) {
addBox()
}
}

最佳答案

我已经通过 Apple Bug Report 提交了关于此问题的票证。我希望这可以帮助遇到相同问题的任何人。这是他们的回应:

Apple Developer Relations24-Sep-2014 04:40 AM

Engineering has determined that this is an issue for you to resolve based on the following:

You can't modify a tree which it is being simulated, and this is clearly stated in Programming Guide.

关于ios - SpriteKit 和 Swift : Creating nodes via didBeginContact messes up the positioning,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25935571/

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