1

So I recently started using a struct to hold variables that define my achievement so that it makes it easier to access. as seen below:

struct Achieve {

var aName:String = ""
var aDes:String = ""
var aImage:String = ""
var aAmount:Int = 0
var aRequired:Int = 0

}

So I defined a variable called 'Ach1' like so:

var Ach1 = Achieve(aName: "No. Games", aDes: "Games Played", aImage: "locked", aAmount: 0, aRequired: 10)

and then I created an SKSpriteNode using the function below:

func generateShopItems (location: CGPoint, page:SKSpriteNode, tex: String) -> SKSpriteNode {

        let node = SKSpriteNode(imageNamed: tex)
        node.position = location
        page.addChild(node)
        return node
}

Then I used the Ach1 variable's aImage property as the tex string when defining the SKSpritenode in another function like below

Achievements.append(generateShopItems(CGPointMake(-120, 200), page:(page1ScrollView), tex: Ach1.aImage))

The texture comes out fine when the game is run for the first time but i can't get the image to change when I change the Ach1 variables aImage property in my touches method it just stays the same.

} else if (node == Achievements[7]) {

        Ach1.aImage = "diamondicon"
         print("working")

}

I need to be able to change the image of the SKSpriteNode while the game is being played. Can someone please tell me what I am doing wrong?

EDIT

var Ach1 = Achieve(node: SKSpriteNode?, aName: "No. Games", aDes: "Games Played", aImage: "locked", aAmount: 0, aRequired: 10)
Astrum
  • 375
  • 6
  • 21

1 Answers1

1

This is the solution that comes to my mind first. It may not be the best, but it's not bad either.

In your Achieve struct, add a property called node:

var node: SKSpriteNode?

And change the aImage property to look something like this:

var aImage: String = "" {
    didSet {
        node?.texture = SKTexture(imageNamed: aImage)
    }
}

Now when you generate the nodes, replace this:

Achievements.append(generateShopItems(CGPointMake(-120, 200), 
    page:(page1ScrollView), tex: Ach1.aImage))

with this

var node = generateShopItems(CGPointMake(-120, 200), 
    page:(page1ScrollView), tex: Ach1.aImage)
Achievements.append(node)
Ach1.node = node

Now when you change the aImage property of Ach1, the node's texture should change as well.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • I get an error when defining Ach1 variable because It asks me to set something for node which i don't know what to put except for SKSpriteNode?, I have edited my answer with it. – Astrum Aug 19 '16 at 08:08
  • @Astrum Oh I forgot about that. Just put `nil`. – Sweeper Aug 19 '16 at 08:10
  • Thanks that seem to have done the job. would have there been another way to do it without creating a node variable? like maybe using the update function to change the image? – Astrum Aug 19 '16 at 09:08
  • Well yes you can. But that just seems a little excessive to me. The `update` function is called several times per second, but the texture only needs changing when `aImage` is changed. @Astrum – Sweeper Aug 19 '16 at 09:10
  • I only ask because i'm going to have more than one achievement SKspritenode in the future and would like an easier way of updating the image on all of them. – Astrum Aug 19 '16 at 09:17
  • could you also take a look at this other question i have about this here: http://stackoverflow.com/questions/39176379/updating-an-sklabel-to-show-the-right-integer – Astrum Aug 27 '16 at 00:41
  • @Astrum Your new question is almost the same as this one, you know. Just try to apply the solution! Tip: it would be faster if you do it yourself. – Sweeper Aug 27 '16 at 07:30