更改全局变量的图像名称 – Swift

huangapple go评论66阅读模式
英文:

Change image name of global variable - swift

问题

Here's the translated code without any additional content:

我正在使用Swift和SpriteKit库创建一个贪吃蛇游戏

在程序的顶部,我声明了一个变量
~~~
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {

    var player = SKSpriteNode(imageNamed: "head")


    [...]
}
~~~

这样我可以在所有的函数中访问这个变量。但是当蛇移动时,我想要改变蛇的图像。
例如,如果蛇向上移动,我想要显示蛇向上的图像。

所以我有以下代码来控制蛇的移动
~~~
if movesController[0] == true {
   playerY += 56.0
}
if movesController[1] == true {
   playerY -= 56.0
}
if movesController[2] == true {
   playerX += 56.0
}
if movesController[3] == true {
   playerX -= 56.0
}
~~~

为了改变蛇的图像,我认为我可以这样做

~~~
if movesController[0] == true {
   playerY += 56.0
   player = SKSpriteNode(imageNamed: "head_up")
}
if movesController[1] == true {
   playerY -= 56.0
   player = SKSpriteNode(imageNamed: "head_down")
}
if movesController[2] == true {
   playerX += 56.0
   player = SKSpriteNode(imageNamed: "head_right")
}
if movesController[3] == true {
   playerX -= 56.0
   player = SKSpriteNode(imageNamed: "head_left")
}
~~~

但是添加了这4行后,蛇无法移动...
所以我尝试更改`player`变量的声明

~~~
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
    var imageName = "head"
    var player = SKSpriteNode(imageNamed: "\(imageName)")

    [...]
}
~~~

然后用`imageName = "move_up"`(或down - left - right)来更改我之前添加的4

但是在程序开始时,它给我一个错误
~~~
无法在属性初始化器内使用实例成员'imageName'; 属性初始化器在'self'可用之前运行
~~~

我该怎么办?我的最后选择是创建另一个`playerVariable`并将其放在真正的蛇图像上面,但这意味着为了无用的东西添加更多的节点。
英文:

I'm creating a Snake Game using swift and the library SpriteKit.

At the top of my program I declare a variable

import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var player = SKSpriteNode(imageNamed: "head")
[...]
}

In this way I can access to this variable in all of my functions. But I'd like to change the snake's image when the it moves.
For example, if the snake moves Up, I'd like to display the image of the snake looking up.

So I have this code to control the movements of the snake

if movesController[0]==true{
playerY += 56.0000000
}
if movesController[1]==true{
playerY -= 56.0000000
}
if movesController[2]==true{
playerX += 56.0000000
}
if movesController[3]==true{
playerX -= 56.0000000
}

and to change the image of the snake I thought I could just do this

if movesController[0]==true{
playerY += 56.0000000
player = SKSpriteNode(imageNamed: "head_up")
}
if movesController[1]==true{
playerY -= 56.0000000
player = SKSpriteNode(imageNamed: "head_down")
}
if movesController[2]==true{
playerX += 56.0000000
player = SKSpriteNode(imageNamed: "head_right")
}
if movesController[3]==true{
playerX -= 56.0000000
player = SKSpriteNode(imageNamed: "head_left")
}

But adding this 4 lines, the Snake is unable to move...
So I tried changing the declaration of the player variable

import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var imageName = "head"
var player = SKSpriteNode(imageNamed: "\("imageName)")
[...]
}

and then change the 4 lines I added before with imageName = "move_up" (or down - left - right)

But at the start of the program it gave me an error

Cannot use instance member 'imageName' within property initializer; property initializers run before 'self' is available

What should I do? My last option is to create another playerVariable and put it above the real snake image, but it would mean adding more nodes for nothing.

答案1

得分: 1

你解决这个问题的方式是正确的。将self放在imageName变量之前。这样就可以工作。

import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
    var imageName = "head"
    var player = SKSpriteNode(imageNamed: "\(self.imageName)")
    
    [...]
}
英文:

Actually, your way to solve this problem is true.Put self before the imageName variable. It would be work.

  import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var imageName = "head"
var player = SKSpriteNode(imageNamed: "\("self.imageName)")
[...]
}

答案2

得分: 1

你必须更改玩家的纹理,但不要在每一步都创建新的玩家。尝试添加这个函数:

func changeHead(imageNamed name: String){
   player.texture = SKTexture(imageNamed: name)
}

然后程序文本将以以下方式更改:

if movesController[0] == true {
   playerY += 56.0000000
   changeHead(imageNamed: "head_up")
}
if movesController[1] == true {
   playerY -= 56.0000000
   changeHead(imageNamed: "head_down")
}
if movesController[2] == true {
   playerX += 56.0000000
   changeHead(imageNamed: "head_right")
}
if movesController[3] == true {
   playerX -= 56.0000000
   changeHead(imageNamed: "head_left")
}
英文:

You must change player's texture, but not create new player at each step. Try add this function:

func changeHead(imageNamed name: String){
player.texture = SKTexture(imageNamed: name)
}

And the program text will be changed in this way:

if movesController[0]==true{
playerY += 56.0000000
changeHead(imageNamed: "head_up")
}
if movesController[1]==true{
playerY -= 56.0000000
changeHead(imageNamed: "head_down")
}
if movesController[2]==true{
playerX += 56.0000000
changeHead(imageNamed: "head_right")
}
if movesController[3]==true{
playerX -= 56.0000000
changeHead(imageNamed: "head_left")
}

huangapple
  • 本文由 发表于 2020年1月3日 22:23:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580206.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定