Reputation: 75
This is how I am currently doing it.
patch1.position = CGPointMake(CGRectGetMidX(self.frame) - 80, CGRectGetMidY(self.frame) + 205)
patch1.yScale = 0.75
patch1.xScale = 0.75
self.addChild(patch1)
percent1.position = CGPointMake(CGRectGetMidX(self.frame) - 80, CGRectGetMidY(self.frame) + 125)
percent1.xScale = 0.5
percent1.yScale = 0.5
self.addChild(percent1)
patch2.position = CGPointMake(CGRectGetMidX(self.frame) - 80, CGRectGetMidY(self.frame) + 55)
patch2.yScale = 0.75
patch2.xScale = 0.75
self.addChild(patch2)
percent2.position = CGPointMake(CGRectGetMidX(self.frame) - 80, CGRectGetMidY(self.frame) - 25)
percent2.xScale = 0.5
percent2.yScale = 0.5
self.addChild(percent2)
except I have 8 of each of them. I just figured that after all is said and done it will be a pain to update these all frequently. Is there a better way to have these set up so it will be easier/more efficient to update them? Eventually I am going to add a function that when tapped percent++
Edit: Here is the image
Upvotes: 0
Views: 46
Reputation: 974
You could use method.
func createSomething(yPosition: CGFloat,xScale: CGFloat, yScale: CGFloat) {
let percent = SKSpriteNode(imageNamed:"Something")
percent.position = CGPointMake(self.frame.size.width / 2 - 80, self.frame.size.height / 2 + yPosition )
percent.xScale = xScale
percent.yScale = yScale
self.addChild(percent)
}
And you add objects like this:
self.createSomething(205, xScale: 0.75, yScale: 0.75);
self.createSomething(125, xScale: 0.5, yScale: 0.5);
self.createSomething(55, xScale: 0.75, yScale: 0.75);
self.createSomething(-25, xScale: 0.5, yScale: 0.5);
Upvotes: 0
Reputation: 765
I'm not sure what these sprite nodes are doing in your app, but assuming they all are doing similar things, you can create a subclass of SKNode and declare these in that subclass.
class MySubclass: SKNode {
// Declare variables here.... initialize them in init().... ect.
let node1 = SKSpriteNode()
let node2 = SKSpriteNode()
override init(){
// DON'T FORGET TO ADD YOUR NODES AS CHILDREN!!
self.addChild(node1)
self.addChild(node2)
}
}
Then in your main view you can do something like this:
let myNodeSubClass = MySubclass() //Assuming your subclass init doesn't have any parameters
override init(size: CGSize){
super.init(size)
myNodesubClass.position = CGPointMake(200, 200) //This will set the position of all the children of your sub class
}
Upvotes: 1