无法使敌方精灵呈之字形并进行反击 Swift、Spritekit

发布于 2025-01-15 10:58:45 字数 3792 浏览 4 评论 0原文

我正在制作一个 2D 滚动射击游戏。我希望其中一个敌人能够随机移动,他们可以沿着屏幕向下移动,或者以之字形移动。敌人停顿了半秒,此时我希望他们还击。我无法让子弹在正确的位置产生。我的运动正常,敌人会向第一个位置开火,但不会向其他位置开火。

我的任务是在敌人位置生成子弹,然后使用 SKAction 来移动子弹。然而,我为敌人做运动的方式现在阻止了我这样做,但这是我能让运动按照我想要的方式工作的唯一方法。

我随机化了屏幕上敌人的生成位置,然后创建了新的位置供它们移动到,但是我认为当我使用 enemy3.position 稍后尝试生成子弹时,它无法找到敌人的位置因为我的做法。我不知道如何以其他方式做到这一点,因为我花了很长时间才弄清楚这一点。当我尝试了一些不同的事情时,我已经注释掉了我试图用产卵激光做的事情。我不擅长编程,所以我知道有比我更好的方法来做到这一点。任何帮助将不胜感激,谢谢。

这是我的敌人函数

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]))
    
    }

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]))
    
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文