gpt4 book ai didi

ios - 如何在两个 SpriteKit 对象之间创建分隔

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

我有一个圆形和内部和外部的三角形。我正在检查我的角色是否不在接触三角形的圆外的圆内,反之亦然。

即使我的角色脱离了我的圈子,这种情况仍然会发生。事实上,如果我在我的圆圈内并且三角形在我的圆圈外,那么我的角色不应该接触它们。我该如何解决这个问题?

有人有想法吗?

我的代码:

 func AddCharacter() {

BooCharacter.size = CGSize(width: 30, height: 30)
BooCharacter.anchorPoint.y = 0
BooCharacter.zRotation = CGFloat(-M_PI_2)
//BooCharacter.position.y += circleRadius
BooCharacter.position = CGPoint(x:0.0, y:circleRadius)
BooCharacter.physicsBody = SKPhysicsBody(texture:BooCharacterSKT, size: CGSize(width: 30, height: 30))
BooCharacter.physicsBody = SKPhysicsBody(circleOfRadius: 15);
BooCharacter.physicsBody?.categoryBitMask = heroCategory
BooCharacter.physicsBody?.contactTestBitMask = triangleCategory
BooCharacter.physicsBody?.collisionBitMask = triangleCategory;
}


func AddCircle() {

Circle = SKShapeNode(circleOfRadius: circleRadius)
Circle.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
Circle.strokeColor = UIColor.whiteColor()
self.addChild(Circle)
Circle.addChild(BooCharacter)

self.AddTriangleToCircle(Circle, Location: CGFloat(random(1...90)), Inside: true)
self.AddTriangleToCircle(Circle, Location: CGFloat(random(90...280)), Inside: false)
self.AddTriangleToCircle(Circle, Location: CGFloat(random(280...360)), Inside: false)
//self.AddTriangleToCircle(Circle, Location: 90)
//self.AddTriangleToCircle(Circle, Location: 180)
//self.AddTriangleToCircle(Circle, Location: 240)
//self.AddTriangleToCircle(Circle, Location: 300)
}

func AddPointsLable() {

pointsLabel = PointsLabel(num: 0)
pointsLabel.position = CGPoint(x: self.size.width/2, y: self.size.height/2 - 35)
pointsLabel.name = "pointsLabel"
addChild(pointsLabel)
}

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

BooCharacter.zRotation += CGFloat(M_PI)



//for touch in touches {
//let location = touch.locationInNode(self)

let TapLable = childNodeWithName("tap")
TapLable?.removeFromParent()

//}



}



override func update(currentTime: NSTimeInterval) {
angleRelatedToCircle -= rotationSpeed
//BooCharacter.zRotation -= rotationSpeed

let newLocation = CGPointMake(circleRadius * cos(angleRelatedToCircle), circleRadius * sin(angleRelatedToCircle));
// BooCharacter.position.x = circleRadius * cos(angleRelatedToCircle)
// BooCharacter.position.y = circleRadius * sin(angleRelatedToCircle)

BooCharacter.runAction(SKAction.rotateByAngle(-rotationSpeed, duration: 0));
BooCharacter.runAction(SKAction.moveTo(newLocation, duration: 0));

//SKAction.moveTo(CGP, duration: <#T##NSTimeInterval#>)

//NSLog("x = %f, y = %f, r = %f",BooCharacter.position.x,BooCharacter.position.y,BooCharacter.zRotation);
}

func AddTriangleToCircle(Circle: SKShapeNode, Location: CGFloat, Inside: Bool) {

let Triangle: SKSpriteNode = SKSpriteNode(imageNamed: "Triangle")

Triangle.size = CGSize(width: 30, height: 30)
Triangle.anchorPoint.y = 0


if Inside == true {
// Inside Triangle
Triangle.zRotation = CGFloat(M_PI_2)
} else {
// Outside Triangle
Triangle.zRotation = CGFloat(-M_PI_2)
}

Triangle.position = CGPoint(x:0.0, y:circleRadius)

let rotationSpeed1 = rotationSpeed + Location;
var angleRelatedToCircle1 = angleRelatedToCircle;

angleRelatedToCircle1 -= rotationSpeed1
Triangle.zRotation -= rotationSpeed1

Triangle.position.x = circleRadius * cos(angleRelatedToCircle1)
Triangle.position.y = circleRadius * sin(angleRelatedToCircle1)

//Triangle.name = "Triangle";
Triangle.physicsBody = SKPhysicsBody(texture:TriangelSKT, size: CGSize(width: 30, height: 30))
//TODO: Make this a polygon body
Triangle.physicsBody?.categoryBitMask = triangleCategory
Triangle.physicsBody?.contactTestBitMask = heroCategory
Triangle.physicsBody?.collisionBitMask = heroCategory
Circle.addChild(Triangle);
}

func didBeginContact(contact: SKPhysicsContact) {
// ---------------------------------------------
// Hero Hit Triangle
// ---------------------------------------------
if (contact.bodyA.categoryBitMask == triangleCategory) {

// Play Sound Effect

// Remove Triangle
//contact.bodyA.node?.removeFromParent();

// Update Score

NSLog("Hero hit Triangle");
}
}

func random(range: Range<Int> ) -> Int
{
var offset = 0

if range.startIndex < 0 // allow negative ranges
{
offset = abs(range.startIndex)
}

let mini = UInt32(range.startIndex + offset)
let maxi = UInt32(range.endIndex + offset)

return Int(mini + arc4random_uniform(maxi - mini)) - offset
}

}

enter image description here

最佳答案

您可能想要像这样设置角色的物理体:

BooCharacter.physicsBody = SKPhysicsBody(circleOfRadius: 15);

因为目前,字符大小是一个30x30的矩形,它的主体是一个直径为60(d=2r)的圆。你需要一个直径为 30 的物体。

此外,您正在更改角色 anchor ,但请记住物理 body 不受该操作的影响。它始终以节点的位置为中心...阅读更多内容 example .

关于三角节点。目前它有一个矩形的物理体,即使它是三角形。不确定你是否想要那样,但这可能会导致你遇到的问题。您有几个选项可以解决这个问题:

  1. 手动创建物理体,就像您的 previous questions 中指出的那样,或者

  2. Create a physics body from a texture .请记住,这可能是性能密集型的,但它可能适合您的游戏,因为场景中没有很多物体。

关于ios - 如何在两个 SpriteKit 对象之间创建分隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34304214/

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