我有一个字符串数组,我试图在函数中使用
var PostionRotation:Array = ["100,45,0.4","305,45,-0.2","505,45,0.3","705,45,-0.1","905,45,0.2"]
func loadSprite(ArrayOfPositions:[String],NameOfSprite:String,SpriteTextureName:String)
{
// pass in a array for the positions and rotations of the sprites
// string for spritename
// string for spritetexturename
for Position in 0...ArrayOfPositions.count
{
var tempstring = ArrayOfPositions[Position ]as String//takes a string ie: 143,628,0.6
var tempStringarray : [String] = tempstring.componentsSeparatedByString(",") // adds each item seperated by , to an array
let XPos = CGFloat((tempStringarray[0] as NSString).floatValue)// exstracts the x cord
let YPos = CGFloat((tempStringarray[1] as NSString).floatValue)//exstracts the y cord
let Rot = CGFloat((tempStringarray[1] as NSString).floatValue)//exstracts the zrotation
let SpriteName = SKSpriteNode(imageNamed: SpriteTextureName) // add variable with texture
SpriteName.name = NameOfSprite + String(Position + 1)// gives it a name + number ted1 etc
SpriteName.position = CGPoint( x: XPos, y: YPos)// gets position from array
SpriteName.zPosition = 2
SpriteName.size = CGSize(width: 200, height: 100)
addChild(SpriteName)
}
}
如何调用该功能?
我试过了:
loadSprite(PostionRotation, NameOfSprite: "frog", SpriteTextureName: "frog001")
loadSprite(PostionRotation[String], NameOfSprite: "frog", SpriteTextureName: "frog001")
答案 0 :(得分:1)
你有没有尝试过:
loadSprite(PostionRotation, NameOfSprite: "frog", SpriteTextureName: "frog001")
这是一个Playground示例,它使用String数组作为参数调用全局函数:
import UIKit
func testFunc(posList:[String]) -> String
{
var s = ""
for pos in posList
{
s = s + " --- " + pos
}
return s
}
var PostionRotation:Array = ["100,45,0.4","305,45,-0.2","505,45,0.3","705,45,-0.1","905,45,0.2"]
var result = "result: " + testFunc(PostionRotation)
在Playground中复制它,然后玩它: - )
答案 1 :(得分:0)
我的错,应该是
var PostionRotation = ["100,45,0.4","305,45,-0.2","505,45,0.3","705,45,-0.1","905,45,0.2"]
然后这个工作
loadSprite(PostionRotation, NameOfSprite: "frog", SpriteTextureName: "frog001")