gpt4 book ai didi

swift - 如何在没有物理的情况下检测碰撞

转载 作者:行者123 更新时间:2023-11-30 10:13:27 24 4
gpt4 key购买 nike

我想在不使用 didBeginContact() 函数的情况下检测两个物体何时接触

我尝试使用 CGRectIntersectsRect() 但没有任何效果。

class GameScene: SKScene, SKPhysicsContactDelegate {

// Player
var _player = SKNode()
var platform = SKNode()

let heroCategory: UInt32 = 1 << 0
let platformCategory: UInt32 = 1 << 1

override func didMoveToView(view: SKView) {

/* Setup your scene here */

// Setup
self.anchorPoint = CGPointMake(0.5, 0.5)
self.physicsWorld.gravity = CGVectorMake(0.0, -2.0);
self.physicsWorld.contactDelegate = self

// Start populating
_player = creatPlayer()
self.addChild(_player)

platform = creatPlatform(1, atPosition: CGPoint(x: 0, y: -200))
self.addChild(platform)


let platformTwo = creatPlatform(2, atPosition: CGPoint(x: 0, y: 200))
self.addChild(platformTwo)

}

func creatPlatform(type: Int, atPosition: CGPoint) -> PlatformNode {

let node = PlatformNode()
node.platformType = type
node.position = atPosition
let sprite = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: 200, height: 50))
node.addChild(sprite)

node.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size)
node.physicsBody?.dynamic = false;
node.physicsBody?.categoryBitMask = platformCategory
node.physicsBody?.contactTestBitMask = heroCategory
node.physicsBody?.collisionBitMask = 0;

return node

}

func creatPlayer() -> SKNode {

let playerNode = SKNode()
let sprite = SKSpriteNode(color: UIColor.whiteColor(), size: CGSize(width: 50, height: 50))
playerNode.addChild(sprite)

playerNode.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size)
playerNode.physicsBody?.dynamic = false
playerNode.physicsBody?.allowsRotation = false
playerNode.physicsBody?.restitution = 1.0
playerNode.physicsBody?.friction = 0.0
playerNode.physicsBody?.angularDamping = 0.0
playerNode.physicsBody?.linearDamping = 0.0
playerNode.physicsBody?.categoryBitMask = heroCategory
playerNode.physicsBody?.contactTestBitMask = platformCategory
playerNode.physicsBody?.collisionBitMask = 0;

return playerNode

}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

/* Called when a touch begins */
_player.physicsBody?.dynamic = true

}

override func update(currentTime: CFTimeInterval) {

/* Called before each frame is rendered */
if(CGRectIntersectsRect(platform.frame, _player.frame)){
println("Collision")
}
}

}

也许使用 CGRectIntersectsRect 是一种错误的方法,但我可以改用?

最佳答案

框架属性

The frame property is a rectangle in the parent’s coordinate system that contains the node’s content, ignoring the node’s children.

同样来自文档:

The frame is non-empty if the node’s class draws content.

因为 SKNode类不执行任何自己的绘制,并且您的平台可能是 SKNode 的子类,玩家是 SKNode,frame 属性始终为 (0,0,0,0)。

您可以通过几种方式访问​​框架的实际大小:

  • 您可以将播放器和平台作为 SKSpriteNode 的子类.
  • 您可以将它们保留原样并访问其中的子 SKSpriteNode。
  • 您可以保持原样并使用 calcualteAccumulatedFrame()方法。

关于文档中的 calcualteAccumulatedFrame():

A node’s accumulated frame, retrieved by calling the calculateAccumulatedFrame method, is the largest rectangle that includes the frame of the node and the frames of all its descendants.

关于swift - 如何在没有物理的情况下检测碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31604541/

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