精灵套件播放器和敌人的碰撞

时间:2017-04-25 16:57:47

标签: swift sprite-kit

我一直在四处寻找如何让我的玩家和敌人发生碰撞。我有一个攻击按钮设置,需要它,所以当按下按钮时,播放器前面的敌人将从屏幕上移除。我将如何设置碰撞,如何在按下按钮时仅进行碰撞?

我已尝试将此代码用于碰撞,但我认为它没有任何效果。

//physicsbody for player and enemies
func didBegin(_ contact: SKPhysicsContact) {
    var bodyA = SKPhysicsBody()
    var bodyB = SKPhysicsBody()

    if contact.bodyA.node?.name == "player" {
        bodyA = contact.bodyA
        bodyB = contact.bodyB
    } else  {
        bodyA = contact.bodyB
        bodyB = contact.bodyA
    }
    //Player hits the rocket and dies and also remove the rocket
    if bodyA.node?.name == "player" && bodyB.node?.name == "enemy" {
        bodyA.node?.removeFromParent()
        bodyB.node?.removeFromParent()


        //After player dies go to death scene
        let gameOver = GameOverScene(size:CGSize(width: 2048, height: 1536), won:  false)
        self.view?.presentScene(gameOver)

    }

}

那么这就是玩家:

 func createPlayer(_ position: CGPoint) { //This is the method that creates the player on screen. You must pass this method the position you want the sprite spawned. Or maybe you can do it somewhere else in Swift, I dunno.

    //graphic stuff
    //let playerTexture = SKTexture(imageNamed: "Ttam1") //create variable with graphic assigned to it
    let player = SKSpriteNode(texture: playerTexture) //give player the graphic
    player.size = CGSize(width: 175, height: 200) //player sprite size
    player.zPosition = 20
    player.name = "player"
    //physics stuff
    player.physicsBody = SKPhysicsBody(texture: playerTexture, size: player.size) //Set up pixel-perfect collision mesh for the player
    player.physicsBody?.isDynamic = true
    player.physicsBody!.affectedByGravity = true //Stop the player from being affected by gravity
    player.physicsBody?.allowsRotation = false
    player.physicsBody?.categoryBitMask = ColliderType.PLAYER


    //spawny stuff
    insertChild(player, at: 0) //spawn player
    player.position = position //move player to position passed into this method
    playerNode = player //Create a node where the player is, filled by the player itself... Like a D.Va ult! The call mech one, not the self-destruct one.
    /*
    let frame2 = SKTexture(imageNamed: "Ttam2")
    let frame3 = SKTexture(imageNamed: "Ttam3")
    let frame4 = SKTexture(imageNamed: "Ttam4")

    */
    let animation = SKAction.animate(with: [playerTexture, frame2, frame3, frame4], timePerFrame: 0.2)
    let runForever = SKAction.repeatForever(animation)

    player.run(runForever)

}

这是敌人:

//Create enemy - slime
func spawnEnemy() {

    //Call the enemy variable and sprite
    let enemyTexture = SKTexture(imageNamed: "Slime1")
    let enemy = SKSpriteNode(texture: enemyTexture)
    enemy.size = CGSize(width: 175, height: 175)
    enemy.zPosition = 10
    //Collision with the enemy
    enemy.name = "enemy"
    enemyNode = enemy

    //Randomly place the enemy on screen and move
    enemy.position = CGPoint(x: 2100, y: 325)
    enemy.physicsBody? = SKPhysicsBody(texture: enemyTexture, size: enemy.size)
    enemy.physicsBody?.affectedByGravity = true
    enemy.physicsBody?.allowsRotation = false
    enemy.physicsBody?.isDynamic = true




    //Add enemy node to screen.
    addChild(enemy)

    let frame2 = SKTexture(imageNamed: "Slime2")
    let frame3 = SKTexture(imageNamed: "Slime3")
    let animation = SKAction.animate(with: [enemyTexture, frame2, frame3, frame2], timePerFrame: 0.2)
    let runForever = SKAction.repeatForever(animation)

    enemy.run(runForever)



    //Set the enemy to move in a single direction:
    let actionMove = SKAction.moveTo(x: -enemy.size.width/2, duration: 10.0)

    //Remove enemy from screen when action is complete:
    let actionRemove = SKAction.removeFromParent()
    enemy.run(SKAction.sequence([actionMove, actionRemove]))

}

1 个答案:

答案 0 :(得分:0)

要让攻击按钮摧毁敌人,请执行touchesBegan,如果按下攻击按钮,请设置一个标记:

attacking = true

(首先定义attacking:您的属性定义中的var attacking = false

并在touchesEnded中,如果attck按钮上的触摸结束,请设置attacking = false

然后,在didBegin中,如果未设置攻击标记,则仅销毁播放器。如果它被设置(即当按下攻击按钮时玩家和粘液发生碰撞),只需破坏粘液,添加一些分数等。

//Player hits the rocket and dies and also remove the rocket
if bodyA.node?.name == "player" && bodyB.node?.name == "enemy" {    
    switch attacking {
    case false:
        bodyA.node?.removeFromParent()
        bodyB.node?.removeFromParent()

        //After player dies go to death scene
        let gameOver = GameOverScene(size:CGSize(width: 2048, height: 1536), won:  false)
        self.view?.presentScene(gameOver)
    case true:
        let enemy = contact.bodyA.categoryBitMask == ColliderType.ENEMY ? contact.bodyA.node : contact.bodyB.node
        enemy.removeFromParent()
        score += 1
    }
}

我假设评论中的'火箭'是敌人的节点,这是粘液? :-)我没有看到为敌人设置categoryBitMask - 是否有一个?我以为是ColliderType.ENEMY

didBegin:中的这段代码是多余的:

var bodyA = SKPhysicsBody()
var bodyB = SKPhysicsBody()
if contact.bodyA.node?.name == "player" {
    bodyA = contact.bodyA
    bodyB = contact.bodyB
} else  {
    bodyA = contact.bodyB
    bodyB = contact.bodyA
}

因为您似乎永远不会使用您设置的bodyAbodyB变量。如果你以后使用它们,那么这可能更有意义:

if contact.bodyA.node?.name == "player" {
    player = contact.bodyA
    enemy = contact.bodyB
} else  {
    enemy = contact.bodyB
    player = contact.bodyA
}

然后这行代码:

        let enemy = contact.bodyA.categoryBitMask == ColliderType.ENEMY ? contact.bodyA.node : contact.bodyB.node

可以从case true:块中删除。