gpt4 book ai didi

ios - 两个物体之间的碰撞

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

我创建了一个简单的项目,但在碰撞中遇到了问题。

很简单(球移动和垂直线),但没有弄清楚如果球触及线如何停止。

import SpriteKit

class GameScene: SKScene,SKPhysicsContactDelegate {

var rPipe = SKSpriteNode() // Left Pipe
var ball1 = SKSpriteNode() // Ball

enum ColliderType:UInt32 {

case Ball1 = 1
case Pipe = 2

}

override func didMoveToView(view: SKView) {

self.physicsWorld.contactDelegate = self

// Pipe
let rPipeTexture = SKTexture(imageNamed: "pipe_r.png")
rPipe = SKSpriteNode(texture: rPipeTexture)
rPipe.position = CGPoint(x: CGRectGetMaxX(self.frame)-50, y: CGRectGetMidY(self.frame)-30)
rPipe.physicsBody = SKPhysicsBody(rectangleOfSize: rPipeTexture.size())
rPipe.physicsBody?.dynamic = false
rPipe.physicsBody?.categoryBitMask = ColliderType.Pipe.rawValue
rPipe.physicsBody?.contactTestBitMask = ColliderType.Pipe.rawValue
rPipe.physicsBody?.collisionBitMask = ColliderType.Pipe.rawValue
self.addChild(rPipe)

// Ball
let ballTexture = SKTexture(imageNamed: "gBall.png")
ball1 = SKSpriteNode(texture: ballTexture)
ball1.position = CGPoint(x: CGRectGetMinX(self.frame)+675, y: CGRectGetMaxY(self.frame)-220)
ball1.physicsBody = SKPhysicsBody(circleOfRadius: ballTexture.size().height/2)
ball1.physicsBody?.dynamic = false
ball1.physicsBody?.categoryBitMask = ColliderType.Ball1.rawValue
ball1.physicsBody?.contactTestBitMask = ColliderType.Pipe.rawValue
ball1.physicsBody?.collisionBitMask = ColliderType.Pipe.rawValue
self.addChild(ball1)

}


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

for touch in (touches ) {

let location = touch.locationInNode(self)
if ball1.containsPoint(location) {
ball1.position.x = location.x
}
}
}

func didBeginContact(contact: SKPhysicsContact) {

print("Contact")

}

最佳答案

其中一个碰撞对象的 dynamic 属性应设置为 true。否则碰撞将被忽略。设置dynamic后,您还需要将affectedByGravity设置为false,因为球不应该受到重力的影响。

ball1.physicsBody?.dynamic = true
ball1.physicsBody?.affectedByGravity = false

关于ios - 两个物体之间的碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34282550/

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