2

So i'm using a spritenode with an image and I don't set the position of it. Funny enough I was having issues setting the position of it so I didn't and it by default was set to center of page (for obvious reason) which ended up being perfect. So now in my touchesbegan method i'm looking to change the said image IF the original image was pressed so i'm checking if the touched location is equal to the node (of the image)'s position. Then if that is true, I replace it with the new image which is called "nowaves".

    for touch in touches
    {
        let location = touch.location(in: self)
        if (location == audioPlaying.position)
        {
            audioPlaying = SKSpriteNode(imageNamed: "nowaves")
        }

        else
        {

Do you guys think I need to readd it? Well right as I asked that I tested it to no result. This is what I tried:

    audioPlaying.size = CGSize(width: frame.size.width / 13, height: frame.size.height / 20)
    addChild(audioPlaying)

If anyone has any idea what the problem is I would love some feedback, thank you :D

insta catering
  • 151
  • 2
  • 12

2 Answers2

0

By default, the position property returns the coordinates of the center of the node. In other words, unless you touched the exact center of the sprite, if (location == audioPlaying.position) will never be true. However, you should be aware that touching a node's exact center point is practically impossible.

So we use another method. We just need to check if the touched node is the node you want.

let touchedNode = self.nodes(at: touch.location(in: self)).first
if touchedNode == audioPlaying {
    // I think setting the texture is more suitable here, instead of creating a new node.
    audioPlaying.texture = SKTexture(imageNamed: "nowaves")
}
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Hey Sweeper! So that was my thought process before I even tested my initial idea. I knew it would be the exact center and theoretically I knew it would if the user were to LITERALLY touch x:0, y:0 So this is helpful for sure. I just tried it with the texture idea and it doesn't seem to work so I instead replaced the old node with a new node of said new image, which didn't work as well. Background info: declaring the new node as a var in the class then initializing the original image in the scenedidLoad override func that I added to my class! – insta catering Jan 29 '17 at 18:14
  • @instacatering What do you mean by it does not work? – Sweeper Jan 29 '17 at 18:15
  • Okay so when I use your idea of changing texture it remains. When instead of changing texture, I completely create a new node it adds the node (I can tell from node count at the bottom right corner) but obviously the old node is still there which I just have to learn how to remove which I think I can find via the developer information on spritekitnodes – insta catering Jan 29 '17 at 18:38
  • @instacatering you can call `removeFromParent` on the node before you create the new one. If you think my answer answers your question, please consider accepting it by clicking on the checkmark! – Sweeper Jan 29 '17 at 18:40
  • I for sure will. So far I have this: // I think setting the texture is more suitable here, instead of creating a new node. //audioPlaying.texture = SKTexture(imageNamed: "nowaves") audioPlaying.removeFromParent() audioPlaying = SKSpriteNode(imageNamed: "nowaves") audioPlaying.size = CGSize(width: frame.size.width / 13, height: frame.size.height / 20) self.addChild(audioPlaying) print("it got here") When I run that though, the initial image remains :( – insta catering Jan 29 '17 at 18:55
  • @instacatering did it print it got there? Did the node count go up? It should not go up because you removed the node from the parent this time. – Sweeper Jan 29 '17 at 18:59
  • The node count goes down, but for some odd reason it doesn't remove the image. I even tried doing audioPlaying.removeAllActions() before removing from parent since I created the variable at the beginning of the class outside of every method (so I can access it in this method along with scenedidload method). – insta catering Jan 29 '17 at 19:14
  • Accepting will be fine as well, so what fixes it? – Sweeper Jan 29 '17 at 19:55
0

To add on to @sweeper , I had all my initial image content in the scenedidLoad method, rather than that I created a new method that I was already using for another functionality to start the game and had the initial image there instead and now everything works well.

insta catering
  • 151
  • 2
  • 12