0

In my previous app (single view application) i used touch down and touch up inside IBActions for buttons in my game. However in a SpriteKit game you have to completely create the scene and i am having a hard time coding my touchesBegan method.

Here is my basic movement method/function:

func heroMovementLeft () {

    hero.position = CGPointMake(hero.position.x - 0.5,hero.position.y);

}

here is my Left arrow node, i would like the HeroMovementLeft method to be called upon touching the LeftArrow

func LeftArrow () {

    let LeftArrow = SKSpriteNode(imageNamed: "Left Arrow.png")
    LeftArrow.position = CGPointMake(30, 30)
    self.addChild(LeftArrow)
} 

Here's where I am at right now for coding the touchesBegan method

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
       /* Called when a touch begins */

        for touch in touches {
        }
}

Do I create an if statement? Do I make touch a variable? What do I need to write in my touches began method so that my hero will move when left arrow is touched. I have looked all over online and cannot find an answer to this question, please help.

jscs
  • 63,694
  • 13
  • 151
  • 195
Gage
  • 61
  • 2
  • 7
  • 1
    Possible duplicate of [Setting up buttons in SKScene](http://stackoverflow.com/questions/19082202/setting-up-buttons-in-skscene) – jscs Mar 22 '16 at 20:10
  • There are few ways to go. The simplest would be to give the name to the node you want to move, and in the touchesBegan method use `nodeAtPoint `to see "if you are touching the right node"... – Whirlwind Mar 22 '16 at 20:13

1 Answers1

1

You basically have 2 options to identify your sprites

1) make them global properties to your scene

   class YourScene: SKScene {

    var leftArrow: SKSpriteNode!

    override func didMoveToView(view: SKView) {

    }


    func leftArrow() {

        leftArrow = SKSpriteNode(imageNamed: "Left Arrow.png")
        leftArrow.position = CGPointMake(30, 30)
        addChild(leftArrow)
   }
 }

than in touches began you can get the location like so

  override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
   /* Called when a touch begins */

    for touch in touches {
         let location = touch.locationInNode(self)
         let node = nodeAtPoint(location)

         if node == leftArrow {
             heroMovementLeft()
         }
      }
   }

Option 2 is to give your sprites names

 func leftArrow() {
    let leftArrow = SKSpriteNode(...)
    leftArrow.name = "LeftArrow"
    ...
 }

and than check for those names in the touches began method

  ....
  if node.name == "LeftArrow" {
     heroMovementLeft()
  }

As a side note you should start your properties or func with small letters and only start classes, structs and protocols with capital letters.

crashoverride777
  • 10,581
  • 2
  • 32
  • 56
  • Thank you for the help @crachoveride777 I no longer get any error notifications but when i run the application in the simulator, the arrow does not display on the screen.I have tried multiple different CGpoints. Could this be because i did not execute the addChild(leftArrow) code in my override func didMoveToView(view: SKView) } ??? – Gage Mar 24 '16 at 15:08
  • You will always have to call addChild(YourSprite) to add it to the scene. – crashoverride777 Mar 24 '16 at 15:10
  • I dont man this has nothing to do with the question you asked. Are you having other images like background sprites that might cover it. Try give the arrow a zPosition higher than your background sprite if you have one. – crashoverride777 Mar 24 '16 at 19:30
  • func LeftArrow() { leftArrow = SKSpriteNode(imageNamed: "LeftArrow.png") leftArrow.position = CGPointMake(30, 30) addChild(leftArrow) leftArrow.xScale = 300 eftArrow.yScale = 300 }override func touchesBegan(touches: Set, withEvent event: UIEvent?) { for touch in touches { let location = touch.locationInNode(self) let node = nodeAtPoint(location) if node == leftArrow { heroMovementLeft() I even changed the CGpoints and increased the size of the image incase the sprite was loading off but it still is not loading on the screen – Gage Mar 24 '16 at 19:31
  • where are you calling func LeftArrow() ? – crashoverride777 Mar 24 '16 at 19:33
  • you're right! I'm not, how and where should i call that method? do i just put LeftArrow() under my override func didMoveToView(view: SKView) { /* Setup your scene here */? – Gage Mar 25 '16 at 04:40
  • Well you need to call left arrow either in view did load or when you want it to show on screen. – crashoverride777 Mar 25 '16 at 10:29