尝试使用变量更改SKSpriteNode图像

时间:2022-03-16 21:23:11

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:

所以我定义了一个名为'Ach1'的变量,如下所示:

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:

然后我使用下面的函数创建了一个SKSpriteNode:

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

然后我在另一个函数中定义SKSpritenode时使用Ach1变量的aImage属性作为tex字符串

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.

第一次运行游戏时纹理很好,但是当我在触摸方法中更改Ach1变量aImage属性时,我无法更改图像,它只是保持不变。

} 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?

我需要能够在游戏进行时更改SKSpriteNode的图像。有人可以告诉我我做错了什么吗?

EDIT

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

1 个解决方案

#1


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:

在Achieve结构中,添加一个名为node的属性:

var node: SKSpriteNode?

And change the aImage property to look something like this:

并将aImage属性更改为如下所示:

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.

现在,当您更改Ach1的aImage属性时,节点的纹理也应该更改。

#1


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:

在Achieve结构中,添加一个名为node的属性:

var node: SKSpriteNode?

And change the aImage property to look something like this:

并将aImage属性更改为如下所示:

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.

现在,当您更改Ach1的aImage属性时,节点的纹理也应该更改。