gpt4 book ai didi

ios - Swift 多节点访问

转载 作者:行者123 更新时间:2023-11-29 01:29:06 28 4
gpt4 key购买 nike

假设我们添加了大约 1000 次相同的节点,如下面的代码所示。有什么方法可以访问每个平台吗?

例如,如果 PlatformCategoryPlayerCategory 发生碰撞,发生碰撞的平台应将其颜色更改为绿色并运行 SKAction.

编辑:感谢 Whirlwind,我可以让与玩家碰撞的平台变为绿色,而其他所有平台保持红色。现在我需要正确设置碰撞,这意味着我只希望 PlayerPlatform 上方时与它发生碰撞。

例如,Player 从 1. 平台跳到 2. 平台。跳跃时,它不应该与第二个平台发生碰撞,只有在它通过它之后才可以。

class GameScene: SKScene, SKPhysicsContactDelegate {

let PlayerCategory : UInt32 = 0x1 << 1
let PlatformCategory : UInt32 = 0x1 << 2

var Platform: SKSpriteNode!
var Player: SKSpriteNode!

override func didMoveToView(view: SKView) {
/* Setup your scene here */
self.physicsWorld.contactDelegate = self
self.physicsBody?.velocity = CGVectorMake(0, 0)
.....
SpawnPlayer()
generatePlatforms()
}

func SpawnPlatforms(position: CGPoint)->SKSpriteNode{

Platform = SKSpriteNode(color: SKColor.redColor(), size: CGSize(width: self.frame.size.width, height: 25))
Platform.position = position
Platform.zPosition = 1

Platform.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: self.frame.size.width, height: 25))
Platform.physicsBody?.dynamic = false
Platform.physicsBody?.restitution = 0.0
Platform.physicsBody?.allowsRotation = false
Platform.physicsBody?.usesPreciseCollisionDetection = true

Platform.physicsBody?.categoryBitMask = PlatformCategory
Platform.physicsBody?.contactTestBitMask = PlayerCategory
Platform.physicsBody?.collisionBitMask = PlayerCategory

return Platform
}

func generatePlatforms(){

for i in 1...1000{

let position = CGPoint(x: self.frame.size.width / 2, y: 140 * CGFloat(i))

let platform = SpawnPlatforms(position)

self.addChild(platform)
}

}

func didBeginContact(contact: SKPhysicsContact) {

let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask

let platform = contact.bodyB.node as! SKSpriteNode

switch(contactMask) {

case PlayerCategory | PlatformCategory:
//either the contactMask was the bro type or the ground type
print("Contact Made0")

if(Player.position.y >= platform.position.y && Player.position.y <= platform.position.y + Player.size.height){
Player.physicsBody?.collisionBitMask = PlatformCategory
platform.color = SKColor.greenColor()
}

default:
return

}

}

func didEndContact(contact: SKPhysicsContact) {

let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask

switch(contactMask) {

case PlayerCategory | PlatformCategory:
//either the contactMask was the bro type or the ground type
print("Contact Made0")


default:
return

}
}

最佳答案

正如我所说,要检测接触以更改平台的颜色,您需要做几件事:

  • 遵守 SKPhysicsDelegate 协议(protocol)
  • 适本地设置 physicsWorld 的 contactDelegate 属性
  • 实现 didBeginContact 方法

符合SKPhysicsDelegate协议(protocol)

这个任务的语法很简单:

class GameScene: SKScene,SKPhysicsContactDelegate
{//your GameScene properties and methods go here...}

遵守协议(protocol)意味着该类必须实现该协议(protocol)所需的方法。 SKPhysicsDelegate 协议(protocol)特有的方法是 didBeginContact 和 didEndContact。如果查看头文件,您会看到:

protocol SKPhysicsContactDelegate : NSObjectProtocol {

optional func didBeginContact(contact: SKPhysicsContact)
optional func didEndContact(contact: SKPhysicsContact)
}

即使这些方法是可选的,我们也需要它们来获取描述联系人的联系人对象。

设置physicsWorld接触委托(delegate)属性

唯一需要的代码是:

self.physicsWorld.contactDelegate = self //self is a scene (eg. GameScene)

因此,当物理世界中发生物理接触时,我们应该以某种方式得到通知。根据文档,contactDelegate property 是一个委托(delegate),在该联系发生时调用。通过将场景设置为联系人委托(delegate),我们将联系人处理的责任委托(delegate)给它。这就是应该提到 didBeginContact 的地方。

以下是您应该如何实现它(可能还有其他几种方法):

func didBeginContact(contact: SKPhysicsContact) {

var firstBody, secondBody: SKPhysicsBody

if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}


if ((firstBody.categoryBitMask & CharacterCategory) != 0 &&
(secondBody.categoryBitMask & PlatformCategory != 0)) {

//secondBody would be a platform

}
}
}

在这里,您可以像这样轻松更改一个平台的颜色:

let platform = secondBody.node as SKSpriteNode
platform.color = UIColor.redColor()

或者您可以更改所有平台的颜色:

self.enumerateChildNodesWithName("platform", usingBlock: {
node, stop in
var platformNode = node as SKSpriteNode
platformNode.color = UIColor.redColor()
})

我还看到你正在使用这个问题的代码:

https://stackoverflow.com/a/31454044/3402095

所以我对其进行了编辑(更改了 didBeginContact),以便在该示例中向您展示如何将 sprite 的颜色更改为红色。

关于ios - Swift 多节点访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33658734/

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