gpt4 book ai didi

swift - SKPhysics 触点无法正常工作

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

我正在尝试使用 SpriteKit 中的 SKPhysicsContactDelegate 函数,但它似乎不起作用。我希望一个 Sprite 在击中另一个 Sprite 时执行一个 Action 。我在 didBeginContact 函数中设置了断点,由于某种原因,我的应用程序从未调用此函数。感谢所有帮助。下面发布了代码。

struct PhysicsCatagory {
static let Enemy :UInt32 = 0x1 << 0
static let Slider :UInt32 = 0x1 << 1
static let Circle :UInt32 = 0x1 << 2
}

class GameScene: SKScene, SKPhysicsContactDelegate {

var EnemyTimer = NSTimer()

var Circle = SKSpriteNode()
var Slider = SKSpriteNode()
var FastButton = SKNode()

var Title = SKSpriteNode()
var Text = SKSpriteNode()

var Path = UIBezierPath()

var gameStarted = Bool()

override func didMoveToView(view: SKView) {

self.physicsWorld.contactDelegate = self

self.backgroundColor = UIColor.whiteColor()


Circle = SKSpriteNode(imageNamed:"blueCircle")
Circle.size = CGSize(width: 140, height: 140)
Circle.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
Circle.zPosition = 1.0
self.addChild(Circle)

Slider = SKSpriteNode(imageNamed: "blocker1")
Slider.size = CGSize(width: 15, height: 50)
Slider.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 + 80)
addChild(Slider)
Slider.zPosition = 1.0


moveClockWise()



}


func didBeginContact(contact: SKPhysicsContact) {
if contact.bodyA.node != nil && contact.bodyB.node != nil{
let firstBody = contact.bodyA.node as! SKSpriteNode
let secondBody = contact.bodyB.node as! SKSpriteNode

if ((firstBody.name == "Enemy") && (secondBody.name == "Slider")){

collisionBall(firstBody, Slider: secondBody)

}
else if ((firstBody.name == "Slider") && (secondBody.name == "Enemy")) {

collisionBall(secondBody, Slider: firstBody)

}


}

}

func collisionBall(Enemy : SKSpriteNode, Slider : SKSpriteNode){

Enemy.physicsBody?.dynamic = true
Enemy.physicsBody?.affectedByGravity = true
Enemy.physicsBody?.mass = 4.0
Slider.physicsBody?.mass = 4.0

Enemy.removeAllActions()

Enemy.physicsBody?.contactTestBitMask = 0
Enemy.physicsBody?.collisionBitMask = 0
Enemy.name = nil



}


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

//Slider.hidden = false
FastButton.hidden = false
Title.hidden = true
Text.hidden = true

EnemyTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(GameScene.Enemies), userInfo: nil, repeats: true)

//Physics
Slider.physicsBody?.categoryBitMask = PhysicsCatagory.Slider
Slider.physicsBody?.collisionBitMask = PhysicsCatagory.Enemy
Slider.physicsBody?.contactTestBitMask = PhysicsCatagory.Enemy
Slider.name = "Slider"
Slider.physicsBody?.dynamic = true
Slider.physicsBody?.affectedByGravity = true




if gameStarted == false{

gameStarted = true



}
else if gameStarted == true{


}


}

func moveClockWise(){

let dx = Slider.position.x / 2
let dy = Slider.position.y / 2

let rad = atan2(dy, dx)


let Path = UIBezierPath(arcCenter: CGPoint(x: self.frame.width / 2, y: self.frame.height / 2), radius: 90, startAngle: rad, endAngle: rad + CGFloat(M_PI * 4), clockwise: true)

let follow = SKAction.followPath(Path.CGPath, asOffset: false, orientToPath: true, speed: 150)
//let rotate = SKAction.rotateByAngle(75, duration: 100)

Slider.runAction(SKAction.repeatActionForever(follow).reversedAction())
//Slider.runAction(SKAction.repeatActionForever(rotate).reversedAction())


}


func Enemies(){

let Enemy = SKSpriteNode(imageNamed: "darkRedDot")
Enemy.size = CGSize(width: 20, height: 20)


//Physics
Enemy.physicsBody = SKPhysicsBody(circleOfRadius: Enemy.size.width / 2)
Enemy.physicsBody?.categoryBitMask = PhysicsCatagory.Enemy
Enemy.physicsBody?.contactTestBitMask = PhysicsCatagory.Slider //| PhysicsCatagory.Circle
Enemy.physicsBody?.collisionBitMask = PhysicsCatagory.Slider //| PhysicsCatagory.Circle
Enemy.physicsBody?.dynamic = true
Enemy.physicsBody?.affectedByGravity = false
Enemy.name = "Enemy"

最佳答案

接触尚未开始,因为您的 slider 没有物理体,因此无法识别接触。除非,这段代码中没有,你的 slider 没有物理体,但你的敌人有。尝试声明 Slider.phisicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "blocker1"), size: Slider.size)

此外,您应该阅读 Swift 中的标准命名约定。建议您的变量始终以小写字母开头(例如,enemyTimercirclesliderfastButton、等等...)。

关于swift - SKPhysics 触点无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37243989/

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