Using swift, I'm trying to add a cup to a scene and then rotate, fade and shrink the cup about the screen's centre. I then want to add more cups to the scene which should then begin to shrink, fade and rotate. The way I am doing this at the moment is by creating a cup from 3 SKSpriteNodes, assigning physics bodies and appending each to another SKSpriteNode 'rotatingLayer'. rotatingLayer then rotates, shrinks and fades, but when I try to add another cup, the cup is placed upon the cups already in the scene, with the same size and rotation, so it doesn't look like anything has been added.
I'd like the next cup to be added at the original size and then immediately start to shrink and rotate, effectively chasing the already rotating cups.
My code:
override func didMoveToView(view: SKView) {
self.addChild(rotatingLayer)
rotatingLayer.position = CGPoint(x: screenWidth/2, y: screenHeight/2)
var rotate = SKAction.rotateByAngle(2 * 3.1415, duration: 5)
rotate = SKAction.repeatActionForever(rotate)
var fade = SKAction.repeatActionForever(SKAction.fadeAlphaTo(0, duration: 10))
var shrink = SKAction.repeatActionForever(SKAction.scaleTo(0, duration: 10))
var shrinkAndFade = SKAction.group([rotate, fade, shrink])
rotatingLayer.runAction(shrinkAndFade)
addCup()
}
func addCup()
{
let side1 = SKSpriteNode(imageNamed: "430x30.png")
side1.position = CGPoint(x: -side1.size.height/4 ,y: 0)
side1.zPosition = 1
side1.xScale = 0.5
side1.yScale = 0.5
side1.physicsBody = SKPhysicsBody(rectangleOfSize: side1.size)
side1.physicsBody?.dynamic = false
rotatingLayer.addChild(side1)
let side2 = SKSpriteNode(imageNamed: "430x30.png")
side2.position = CGPoint(x: side2.size.height/4 ,y: 0)
side2.zPosition = 1
side2.xScale = 0.5
side2.yScale = 0.5
side2.physicsBody = SKPhysicsBody(rectangleOfSize: side2.size)
side2.physicsBody?.dynamic = false
rotatingLayer.addChild(side2)
let base = SKSpriteNode(imageNamed: "30x430.png")
base.position = CGPoint(x: 0,y: -base.size.width/4)
base.zPosition = 1
base.xScale = 0.5
base.yScale = 0.5
base.physicsBody = SKPhysicsBody(rectangleOfSize: base.size)
base.physicsBody?.dynamic = false
rotatingLayer.addChild(base)
}
I'll be having some cups not rotating and some which will be, so I was hoping to use rotatingLayer as a constantly rotating, shrinking, fading layer which I can add cups to when they need to shrink, fade and rotate.
Can anyone help?
Thank you so much in advance.
Foot.
Aucun commentaire:
Enregistrer un commentaire