Ios 两个物体之间的碰撞
我创建了一个简单的项目,但在冲突中我遇到了一个问题 这很简单(球移动和垂直线),但没有弄清楚如果球碰到线怎么停Ios 两个物体之间的碰撞,ios,swift,sprite-kit,skphysicsbody,skscene,Ios,Swift,Sprite Kit,Skphysicsbody,Skscene,我创建了一个简单的项目,但在冲突中我遇到了一个问题 这很简单(球移动和垂直线),但没有弄清楚如果球碰到线怎么停 import SpriteKit class GameScene: SKScene,SKPhysicsContactDelegate { var rPipe = SKSpriteNode() // Left Pipe var ball1 = SKSpriteNode() // Ball enum ColliderType:UInt32 { case Ball1 = 1 case
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")
}
导入SpriteKit
类游戏场景:SKScene,SKPhysicContactDelegate{
var rPipe=SKSpriteNode()//左管道
var ball1=SKSpriteNode()//Ball
枚举冲突类型:UInt32{
案例1=1
箱管=2
}
覆盖func didMoveToView(视图:SKView){
self.physicsWorld.contactDelegate=self
//烟斗
让rPipeTexture=SKTexture(图像名为:“pipe_r.png”)
rPipe=SKSpriteNode(纹理:rPipeTexture)
rPipe.position=CGPoint(x:CGRectGetMaxX(self.frame)-50,y:CGRectGetMidY(self.frame)-30)
rPipe.physicsBody=SKPhysicsBody(矩形大小: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)
//球
让ballTexture=SKTexture(图像名为:“gBall.png”)
ball1=SKSpriteNode(纹理:ballTexture)
ball1.position=CGPoint(x:CGRectGetMinX(self.frame)+675,y:CGRectGetMaxY(self.frame)-220)
ball1.physicsBody=SKPhysicsBody(圆环半径:ballTexture.size().height/2)
ball1.physicsBody?.dynamic=错误
ball1.physicsBody?.categoryBitMask=ColliderType.ball1.rawValue
ball1.physicsBody?.contactTestBitMask=ColliderType.Pipe.rawValue
ball1.physicsBody?.collisionBitMask=ColliderType.Pipe.rawValue
self.addChild(ball1)
}
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
用于触摸输入(触摸){
let location=touch.locationInNode(自)
如果球1.包含点(位置){
ball1.position.x=location.x
}
}
}
func didBeginContact(联系人:SKPhysicsContact){
打印(“联系人”)
}
一个碰撞对象的动态
属性应设置为真
。否则碰撞将被忽略。设置动态
后,还需要将受重力影响
设置为假
,因为球不应受到重力的影响
ball1.physicsBody?.dynamic = true
ball1.physicsBody?.affectedByGravity = false
附加链接中的项目供您参考:如果您将
physicsBody?
替换为physicsBody!