我正在使用sprite套件(2D)
進行遊戲。當我在敵人身上射擊時,有時候子彈會穿過敵人,我該如何解決這個問題?
我有這樣的代碼:
meteor.physicsBody = SKPhysicsBody(rectangleOfSize: enemy.size)
和我有你需要摧毀,但有時當我拍我的設備上的流星子彈穿過流星 流星形象是這樣的一個bug或我做錯什麼了嗎? 和 我該如何解決這個問題?
感謝您閱讀我的問題,希望有人能幫助我! 如果你不明白我的問題PLZ評論你不明白。
func fireBullet() {
let bullet = SKSpriteNode(imageNamed: "bullet")
bullet.name = "Bullet"
bullet.setScale(2.9)
bullet.position = player.position
bullet.zPosition = 1
bullet.physicsBody = SKPhysicsBody(rectangleOfSize: bullet.size)
bullet.physicsBody!.affectedByGravity = false
bullet.physicsBody!.categoryBitMask = PhysicsCategories.Bullet
bullet.physicsBody!.collisionBitMask = PhysicsCategories.None
bullet.physicsBody!.contactTestBitMask = PhysicsCategories.Meteor
self.addChild(bullet)
let moveBullet = SKAction.moveToY(self.size.height + bullet.size.height, duration: 1)
let deleteBullet = SKAction.removeFromParent()
let bulletSequence = SKAction.sequence([bulletSound, moveBullet, deleteBullet])
bullet.runAction(bulletSequence)
}
func spawnMeteor(){
let randomXStart = random(min: CGRectGetMinX(gameArea), max: CGRectGetMaxX(gameArea))
let randomXEnd = random(min: CGRectGetMinX(gameArea), max: CGRectGetMaxX(gameArea))
let startPoint = CGPoint(x: randomXStart, y: self.size.height * 1.2)
let endPoint = CGPoint(x: randomXEnd, y: -self.size.height * 0.2)
let Meteor = SKSpriteNode(imageNamed: "Meteor\(arc4random_uniform(2))")
Meteor.name = "Meteor"
Meteor.setScale(0.2)
Meteor.position = startPoint
Meteor.zPosition = 2
Meteor.physicsBody = SKPhysicsBody(rectangleOfSize: meteor.size)
Meteor.physicsBody!.affectedByGravity = false
Meteor.physicsBody!.categoryBitMask = PhysicsCategories.Meteor
Meteor.physicsBody!.collisionBitMask = PhysicsCategories.None
Meteor.physicsBody!.contactTestBitMask = PhysicsCategories.Player | PhysicsCategories.Bullet
self.addChild(Meteor)
let moveMeteor = SKAction.moveTo(endPoint, duration: 2)
let deleteMeteor = SKAction.removeFromParent()
let loseALifeAction = SKAction.runBlock(loseALife)
let MeteorSequence = SKAction.sequence([moveMeteor, deleteMeteor, loseALifeAction])
if currentGameState == gameState.inGame{
Meteor.runAction(MeteorSequence)
}
let dx = endPoint.x - startPoint.x
let dy = endPoint.y - startPoint.y
let amountToRotate = atan2(dy, dx)
enemy.zRotation = amountToRotate
}
func didBeginContact(contact: SKPhysicsContact) {
var body1 = SKPhysicsBody()
var body2 = SKPhysicsBody()
if contact.bodyA.collisionBitMask < contact.bodyB.categoryBitMask{
body1 = contact.bodyA
body2 = contact.bodyB
}
else{
body1 = contact.bodyB
body2 = contact.bodyA
}
if body1.categoryBitMask == PhysicsCategories.Player && body2.categoryBitMask == PhysicsCategories.Meteor{
//if the player has hit the meteor
if body1.node != nil {
spawnExplosion(body1.node!.position)
}
if body2.node != nil {
spawnExplosion(body2.node!.position)
}
body1.node?.removeFromParent()
body2.node?.removeFromParent()
runGameOver()
}
if body1.categoryBitMask == PhysicsCategories.Bullet && body2.categoryBitMask == PhysicsCategories.Meteor && body2.node?.position.y < self.size.height {
//if the bullet has hit the meteor
addScore()
if body2.node != nil{
spawnExplosion(body2.node!.position)
spawnPlusScore(body2.node!.position)
}
body1.node?.removeFromParent()
body2.node?.removeFromParent()
}
我加入,但有時候我可以通過流星 – EPKgames
只是櫃面你有興趣還拍,我面臨着類似的問題,我有時可以通過我在遊戲中製作的繩索進行拍攝。我相信這是一個iOS 9.x錯誤,因爲這只是最近纔開始發生的。設置精確的碰撞對我來說也沒有幫助。 – crashoverride777
okey tnx比我知道的很多它不是代碼 – EPKgames