これは初めてのiOSでのゲームです。自然に私はいくつかの問題を抱えていました。私はそれらを解決するために管理してきましたが、今、それはマリオとパイプ間の衝突部分に来ることを私はいくつかの問題を抱えている:スプライトキットの衝突検出Swift
私がやりたいこと:
マリオがパイプに衝突した場合には、彼は彼らの上を飛び越えて反対側に行く必要があります。彼がしなければ、パイプはゲームが終了する画面の端に彼を押し続けるでしょう。
実際に何が起こるか:
何も衝突は検出されません。ここで
は、以下のコードは次のとおりです。
import SpriteKit
import GameplayKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var mario = SKSpriteNode()
enum ColliderType: UInt32 {
case Mario = 1
case Object = 2
}
func addPipes() {
let pipetexture = SKTexture(imageNamed: "pipes")
let pipes = SKSpriteNode(texture: pipetexture)
let setScale = CGFloat(arc4random_uniform(19) + 15)
pipes.setScale(setScale) //min 15, max 19
pipes.position = CGPoint(x: self.frame.maxX, y: self.frame.minY)
let movePipes = SKAction.move(by: CGVector(dx: -5 * self.frame.width, dy: 0), duration: TimeInterval(self.frame.width/100))
let removePipes = SKAction.removeFromParent()
let moveAndRemovePipes = SKAction.sequence([movePipes, removePipes])
pipes.zPosition = +1
pipes.physicsBody = SKPhysicsBody(rectangleOf: pipes.size)
pipes.physicsBody!.isDynamic = false
pipes.physicsBody!.contactTestBitMask = ColliderType.Object.rawValue
pipes.physicsBody!.categoryBitMask = ColliderType.Object.rawValue
pipes.physicsBody!.collisionBitMask = ColliderType.Mario.rawValue
self.addChild(pipes)
pipes.run(moveAndRemovePipes)
}
func didBegin(_ contact: SKPhysicsContact) {
print("contact")
}
override func didMove(to view: SKView) {
self.physicsWorld.contactDelegate = self
_ = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(self.addPipes), userInfo: nil, repeats: true)
let marioTexture = SKTexture(imageNamed: "mariorun0.png")
let marioTexture2 = SKTexture(imageNamed: "mariorun1.png")
let marioTexture3 = SKTexture(imageNamed: "mariorun2.png")
let animation = SKAction.animate(with: [marioTexture, marioTexture2, marioTexture3], timePerFrame: 0.1)
let makeMarioRun = SKAction.repeatForever(animation)
mario = SKSpriteNode(texture: marioTexture)
mario.setScale(10)
mario.position = CGPoint(x: -(self.frame.size.width/3.5), y: -(self.frame.size.height/2.8))
mario.run(makeMarioRun)
mario.physicsBody = SKPhysicsBody(rectangleOf: mario.size)
mario.physicsBody!.isDynamic = false
mario.physicsBody!.contactTestBitMask = ColliderType.Object.rawValue
mario.physicsBody!.categoryBitMask = ColliderType.Mario.rawValue
mario.physicsBody!.collisionBitMask = ColliderType.Object.rawValue
self.addChild(mario)
addPipes()
mario.zPosition = +2
}
}
私も完璧に動作ジャンプ機能を実装しました。私の問題は、パイプとマリオの間で衝突の検出ができないことです。
両方のスプライトのz位置を同じ値に設定してみました。
私の最初の心配は、画像が小さくて衝突が検出できなくなることでした。しかし、私は両方のノードをスケールアップしているので、スケールアップされた画像の物理的ボディを使用しているので問題はありませんが、どちらも役に立ちません。事前にあなたの助けのための
感謝。
この行は不要です: 'pipes.physicsBody!.contactTestBitMask = ColliderType .Object.rawValue'です。まず、これはパイプが自分自身と接触したときに通知することを意味しますが、Marioを正しくパイプに接続するように設定したので、このラインを削除するだけで済みます。パイプをMarioに連絡させる必要はありません。衝突を正しく設定しました。 –