Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...