1

I am currently creating a 2D game in which I have a cannon staying at the bottom of the screen that fires projectiles to some targets in the upper part of the screen. The user can rotate the cannon in order to aim for the targets. I want the projectile to be fired to the location that the canon faces. I have no idea how to do that and I need your help... Heres my code ->

import SpriteKit

class GameScene: SKScene {

    var canon = SKSpriteNode()
    var projectile = SKSpriteNode()


    var touching = false
    var locked = false
    var location = CGPoint()


    override func didMoveToView(view: SKView) {

        /* Setup your scene here */

        projectile.position = canon.position
        projectile.color = UIColor.whiteColor()
        projectile.size = CGSize(width: 50, height: 50)
        projectile.physicsBody?.affectedByGravity = false
        projectile.physicsBody?.linearDamping = 1
        projectile.physicsBody?.dynamic = true
        addChild(projectile)


        canon.zPosition = 2
        canon = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: 50, height: 200))
        canon.position = CGPoint(x: self.size.width/2.0, y: 10)

        addChild(canon)

            }

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

       /* Called when a touch begins */
        touching = true

        let touch = touches.first as UITouch!
        location = touch.locationInView(self.view)

    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
        rotateCanon(location.x)
        projectile.zRotation = canon.zRotation


    }

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {

        touching = false

    }

    func rotateCanon(locationx:CGFloat) {
        if locationx <= frame.width/5 && touching {
            print(1)

            canon.runAction(SKAction.rotateByAngle(0.01, duration: 0.1))

        }
        if locationx > frame.width/5 && touching {
            print(2)

            canon.runAction(SKAction.rotateByAngle(-0.01, duration: 0.1))

        }
    }


}`

Thanks a lot if you can help that would be awesome :D

Whirlwind
  • 14,286
  • 11
  • 68
  • 157
robo-monk
  • 134
  • 3
  • 9
  • use trig to convert Polar Coordinates to Rectangular Coordinates, then applyImpulse based on this value. http://www.teacherschoice.com.au/maths_library/coordinates/polar_-_rectangular_conversion.htm – Knight0fDragon Sep 13 '16 at 17:35
  • @Knight0fDragon I am really begginer with this, can you provide me some code? Thanks a lot anyways. I looked it up, but I still have no idea how to accomplish this through code. – robo-monk Sep 13 '16 at 18:01
  • 1
    If you are a beginner and can't apply basic math functions, then you need to stop what you are doing, and pick an easier project to learn the skills you need – Knight0fDragon Sep 13 '16 at 18:04
  • 2
    @FilipposT Take a look at this http://stackoverflow.com/a/26366034 I think it is what you are looking for. You may take a peek into this as well: http://stackoverflow.com/q/36230619/3402095 if you decide to make seeking projectiles :) – Whirlwind Sep 13 '16 at 18:07

0 Answers0