SKSpriteNode does not have member named ''- how to access a variable declared in my spawn method?

喜夏-厌秋 提交于 2019-12-13 04:52:06

问题


I asked another question before about how to access a global variable in Swift, but realized the global variable did not work for my purposes. Some background:

The game is a math/match game. Player's will have an equation, and they have to get the sheep with the matching answer.

I have sheep that spawn on screen. Each sheep holds a unique int value (1-12) that is generated within my spawnSheep method:

var sheepValue: Int = Int(arc4random_uniform(UInt32(12))+1)

Multiple sheep will spawn on screen- so each will need their own value. Before, the global variable of sheepValue would update all sheep's value to the most recent sheep that spawned, thereby negating any form of 'unique' numbers for all sheep. I have the values converted into strings which will be visible on each sheep.

Here's a snippet of where I'm trying to access the sheepValue outside of the function it was declared in:

 func checkCollisions() {
    var hitSheep: [SKSpriteNode] = []
    enumerateChildNodesWithName("sheep") { node, _ in
    let sheep = node as SKSpriteNode

    if CGRectIntersectsRect(sheep.frame, self.sandman.frame) {
    if sheep.sheepValue == self.equation {

        hitSheep.append(sheep)

        }

sheep.sheepValue gets an error: 'SKSpriteNode does not have member named "sheepValue"

But as I understand it, I can set properties to my SKSpriteNode (the sheep) and access it in that syntax. But not variables/ints? I'm not sure what I'm doing wrong.

Some other thoughts- could I just use the string values to check sheepValues?? If a sheep spawns with a sheepValue of 5, then '5' would be printed out on the sheep and in the console. This string value is saved, and acts how I wish the int var would act. I'm thinking about just using string values to check if a sheepValue is equal to the player's equation. But that would involved converting more ints into more strings, which is a bit more work.


回答1:


I understand this wrong with your explanation

import UIKit

class SKSpriteNode {
   var spriteTest:String = "SpriteKitTeste";
}

class Sheep: SKSpriteNode
{
   var sheepTest:String = "SheepTest";
}

let sheep = Sheep();
let someSprite:SKSpriteNode = sheep;

println(someSprite.sheepTest); //SKSpriteNode does not have a member named 'sheepTest';

the super class SKSpriteNode can't access properties or methods from Sheep class.

to resolve this problem is very simple, you need cast to class first or if you use swift the language can understand what is the correct type.

let someSheepSprite = sheep as Sheep;
println(someSheepSprite.sheepTest);

In your code i believe the problem will be fix change the cast line

func checkCollisions() {
var hitSheep: [SKSpriteNode] = []
enumerateChildNodesWithName("sheep") { node, _ in
let sheep = node as Sheep // <<<<<<< this line

if CGRectIntersectsRect(sheep.frame, self.sandman.frame) {
if sheep.sheepValue == self.equation {

    hitSheep.append(sheep)

    }


来源:https://stackoverflow.com/questions/28863229/skspritenode-does-not-have-member-named-how-to-access-a-variable-declared-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!