Xcode Spritekit可以消除触摸时的精灵并创建其他精灵?

时间:2018-11-08 15:28:11

标签: ios sprite-kit

我正在创建一个游戏,玩家必须触摸敌人的精灵以消灭它们。播放器始终位于屏幕中间。我要测试多种不同类型的敌人,首先要测试2种类型的敌人。

这是创建敌人的代码。

[globalVar = GP:L = 1]
   #whatever
[end]

和触摸功能

func createEnemy(type:Enemies, forSpawner spawner: Int) -> SKSpriteNode?
    {

        switch type {
        case .diamond:
            enemySprite = SKSpriteNode(imageNamed: "Diamond")
            enemySprite.setScale(0.5)
            enemySprite.color = UIColor.green
            enemySprite.colorBlendFactor = 1
            enemySprite.name = "enemy"
            let location = targetLocation
            let action = SKAction.move(to: CGPoint(x: location.x, y: location.y), duration: 2.0)
            enemySprite.run(action)
        case .diamondSolid:
            enemySprite = SKSpriteNode(imageNamed: "DiamondSolid")
            enemySprite.setScale(0.5)
            enemySprite.color = UIColor.purple
            enemySprite.colorBlendFactor = 1
            enemySprite.name = "enemySolid"
            let location = targetLocation
            let action = SKAction.move(to: CGPoint(x: location.x, y: location.y), duration: 1.0)
            enemySprite.run(action)
        }

        guard let enemyPosition = spawnArray?[spawner].position else {return nil}
        let dx = targetLocation.x - enemyPosition.x
        let dy = targetLocation.y - enemyPosition.y
        let angle = atan2(dy,dx)

        enemySprite.position.x = enemyPosition.x
        enemySprite.position.y = enemyPosition.y
        enemySprite.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: enemySprite.size.width, height: enemySprite.size.height))
        enemySprite.physicsBody?.categoryBitMask = enemyCategory
        enemySprite.zRotation = angle - 90 * degreesToRadians



        return enemySprite

    }

我认为我需要做的是从触摸事件中发送一个位置,并将其发送到一个新功能,该功能可以以某种发射器粒子优势消灭敌人,让玩家知道形状已经死了。根据类型的不同,它会消灭并增加得分,也可能消灭,增加得分并产生许多其他敌人,并将它们移回一定数量,然后再将其移向目标位置。我对此有何帮助?

1 个答案:

答案 0 :(得分:0)

好吧,我想我通过为敌人本身创建一个类来解决这个问题,但是现在我有一个问题,当每个敌人被杀死时,我都无法生成一个爆炸效果的发射器。我删除了一些代码来解决问题,但这是代码

import SpriteKit

enum EnemyType:Int {
    case diamond
    case triangle
    case chevron
    case elipse
    case pentagon
    case diamondSolid
    case traingleSolid
    case elipseSolid
    case pentagonSolid
}

class Enemy: SKSpriteNode {

    let type: EnemyType
    var enemyTexture:SKTexture!
    var touched = false
    init (type:EnemyType){
        self.type = type

        switch type {
        case .diamond:
            enemyTexture = SKTexture(imageNamed: "diamond")
        case .triangle:
            enemyTexture = SKTexture(imageNamed: "triangle")
        case .chevron:
            enemyTexture = SKTexture(imageNamed: "chevron")
        case .elipse:
            enemyTexture = SKTexture(imageNamed: "elipse")
        case .pentagon:
            enemyTexture = SKTexture(imageNamed: "pentagon")
        case .diamondSolid:
            enemyTexture = SKTexture(imageNamed: "diamondSolid")
        case .traingleSolid:
            enemyTexture = SKTexture(imageNamed: "triangleSolid")
        case .elipseSolid:
            enemyTexture = SKTexture(imageNamed: "elipseSolid")
        case .pentagonSolid:
            enemyTexture = SKTexture(imageNamed: "pentagonSolid")

        }

        super.init(texture: enemyTexture,color:UIColor.clear, size: CGSize.zero)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func createPhysicsBody()
    {
        physicsBody = SKPhysicsBody(rectangleOf: size)
        physicsBody?.isDynamic = true
        physicsBody?.categoryBitMask = PhysicsCategory.enemyCategory
        physicsBody?.contactTestBitMask = PhysicsCategory.playerCategory
        physicsBody?.collisionBitMask = PhysicsCategory.playerCategory
    }


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

            if let explosion = SKEmitterNode(fileNamed: "explosion.sks"){
            explosion.position = self.position
            self.addChild(explosion)
            self.run(SKAction.wait(forDuration: 1)){
            explosion.removeFromParent()
            }
        }
        removeFromParent()
    }

}

当前所有这些代码要做的是清除被触摸的敌人并播放粒子发射器。难道我做错了什么?