1 year ago

#313309

test-img

user1857403

Unable to make enemy sprites zig zag and fire back Swift, Spritekit

I'm making a 2D scrolling shooter. I want one of the enemies to move randomly has they move down the the screen or in a zig zag kinda movement. The enemy pause's for half a second and at this point I want them to fire back. I'm having trouble with getting the bullets to spawn at the correct position. I have the movement working and the enemy will fire at the first position but not at any of the other positions.

My task was to spawn the bullet at the enemy position and then use SKAction for the movement of the bullet. However the way I've done the movement for the enemy has now stopped me doing this, but this was the only way I could get the movement to work the way I wanted.

I randomised the enemies spawn position of screen and then created new positions for them to move to, however I think that when I use enemy3.position to try spawn the bullet later it can't find the enemies position because of the way I've done it. I don't know how to do it any other way as It took me a long time to figure this out. I've commented out what I was trying to do with the spawn laser, as I tried a couple of different things. I'm not great at programming so I know there's a much better way to do this then the way I am. Any help would be much appreciated, thank you.

Here's my enemy function

func addEnemy3() {
        
        //Enemy3 Image
        let enemy3 = SKSpriteNode(imageNamed: "Enemy3.png")

        //GameplayKit randomization to spawn enemy across screen size
        let randomEnemy3Position = GKRandomDistribution(lowestValue: -350, highestValue: 350)
        //Randomly spawn enemy3 in different positions
        let position = CGFloat(randomEnemy3Position.nextInt())
        enemy3.position = CGPoint(x: position, y: 700/*self.frame.size.height + enemy3.size.height*/)
        addChild(enemy3)
        
        //To randomly spawn next positions on the x axis
        let position1 = CGFloat(randomEnemy3Position.nextInt())
        let position2 = CGFloat(randomEnemy3Position.nextInt())
        let position3 = CGFloat(randomEnemy3Position.nextInt())
        let position4 = CGFloat(randomEnemy3Position.nextInt())
        
        //Delay enemies
        let delay = SKAction.wait(forDuration: 5)
        
        //Move enemies to next positions randomly on the x axis while moving down the y axis
        let actionMoveOne = SKAction.move(to: CGPoint(x: position, y: 500), duration: 3.0)
        
        let actionMoveTwo = SKAction.move(to: CGPoint(x: position1, y: 200), duration: 3.0)
        
        let actionMoveThree = SKAction.move(to: CGPoint(x: position2, y: -100), duration: 3.0)
        
        let actionMoveFour = SKAction.move(to: CGPoint(x: position3, y: -400), duration: 3.0)
        
        let actionMoveFive = SKAction.move(to: CGPoint(x: position4, y: -600), duration: 3.0)
        
        let wait = SKAction.wait(forDuration: 0.5)
        
        // Remove enemies when finished
        let actionRemove = SKAction.removeFromParent()
        
        enemy3.run(SKAction.sequence([delay, actionMoveOne, wait, actionMoveTwo, wait, actionMoveThree, wait, actionMoveFour, wait, actionMoveFive, actionRemove]), withKey:"enemy3")
        
        
        //Create and position laser
       // let enemyLaser = SKSpriteNode(imageNamed: "EnemyLaser")
        
        //enemyLaser.position = CGPoint(x: position1, y: position1)
//        enemyLaser1.position = CGPoint(x: position1, y: position1)
//        enemyLaser2.position = CGPoint(x: position2, y: position2)
//        enemyLaser3.position = CGPoint(x: position3, y: position3)
//        enemyLaser4.position = CGPoint(x: position4, y: position4)
        
//        enemyLaser.position.y -= 5
//        addChild(enemyLaser)

        //let laserEndPoint = CGPoint(x: enemyLaser.position.x, y: -600)
//        let enemyFire = SKAction.move(to: laserEndPoint, duration: 1.0)
//        let laserDelay = SKAction.wait(forDuration: 10)
//        let enemyFire = SKAction.move(to: CGPoint(x: enemyLaser.position.x, y: -600), duration: 3.0)
//        let enemyFire1 = SKAction.move(to: CGPoint(x: position1, y: -600), duration: 3.0)
//        let enemyFire2 = SKAction.move(to: CGPoint(x: position2, y: -600), duration: 3.0)
//        let enemyFire3 = SKAction.move(to: CGPoint(x: position3, y: -600), duration: 3.0)
//        let enemyFire4 = SKAction.move(to: CGPoint(x: position4, y: -600), duration: 3.0)
//        let enemyFireRemove = SKAction.removeFromParent()
//        enemyLaser.run(SKAction.sequence([laserDelay, enemyFire, /*enemyFire1, enemyFire2, enemyFire3, enemyFire4, */enemyFireRemove]))
    
    }

swift

sprite-kit

skaction

0 Answers

Your Answer

Accepted video resources