我想知道是否可以同时在屏幕上显示两个相同的精灵。我的问题是云。我有两个云精灵在屏幕上移动。 Cloud1和Cloud2。当第一个Cloud1位于屏幕中间时,我想要另一个Cloud1生成。此外,我希望第一个Cloud1在到达屏幕末尾后被删除。这可能是使用精灵还是我需要使用纹理?
override func update(currentTime: CFTimeInterval) {
var moveCloud1 = SKAction.moveByX(-CGRectGetMidX(self.frame)*3, y:0, duration: 45)
var moveCloud2 = SKAction.moveByX(-CGRectGetMidX(self.frame)*3, y:0, duration: 60)
if(cloudSprite1.frame.origin.x < self.frame.size.width/2){
let cloudSprite1a = SKSpriteNode(texture: cloudTexture1)
self.addChild(cloudSprite1a)
cloudSprite1a.position = CGPoint(x:CGRectGetMidX(self.frame)*2.4, y:CGRectGetMidY(self.frame)*1.8);
cloudSprite1a.size = CGSize(width: 75, height: 60)
cloudSprite1a.runAction(moveCloud1)
}
}
self.addCloudSprite1(CGPointMake(100, 500))
self.addCloudSprite2(CGPointMake(100, 300))
}
func addCloudSprite1(position : CGPoint)
{
cloudSprite1a.position = CGPoint(x:CGRectGetMidX(self.frame)*2.4, y:CGRectGetMidY(self.frame)*1.8);
self.addChild(cloudSprite1a)
var moveCloud1 = SKAction.moveByX(-CGRectGetMidX(self.frame)*2.8, y:0, duration: 40)
cloudSprite1a.runAction(moveCloud1, completion: { () -> Void in
cloudSprite1a.removeFromParent()
self.addCloudSprite1(position)
})
}
func addCloudSprite2(position : CGPoint)
{
cloudSprite2a.position = CGPoint(x:CGRectGetMidX(self.frame)*2.4, y:CGRectGetMidY(self.frame)*1.5);
cloudSprite2a.size = CGSize(width: 75, height: 60)
self.addChild(cloudSprite2a)
var moveCloud2 = SKAction.moveByX(-CGRectGetMidX(self.frame)*2.6, y:0, duration: 60)
cloudSprite2a.runAction(moveCloud2, completion: { () -> Void in
cloudSprite2a.removeFromParent()
self.addCloudSprite2(position)
})
}
答案 0 :(得分:0)
您可以将cloud1
和cloud2加载为textures
并将其设置为SpriteNodes
并根据需要重复使用相同的纹理。
override func didMoveToView(view: SKView) {
let cloudTexture1 = SKTexture(imageNamed: "cloud1.png")
let cloudSprite1 = SKSpriteNode(texture: cloudTexture1)
cloudSprite1 // positioned in middle.
let cloudSprite2 = SKSpriteNode(texture: cloudTexture1)
cloudSprite1 // positioned in at starting.
}
您可以在更新功能中进行边界检查以添加新的云精灵。
override func update(currentTime: CFTimeInterval) {
if (self.cloudSprite1.position.x < - self.cloudSprite1.size.width)
{
cloudSprite1.position = //The other end.
}
if (self.cloudSprite2.position.x < -self.cloudSprite2.size.width)
{
cloudSprite2.position = /The other end.
}
}
或者,您也可以使用节点的SKAction完成处理程序。
func addCloudSprite(position : CGPoint, delay: NSTimeInterval)
{
let cloudSprite = SKSpriteNode(color: UIColor.greenColor(), size: CGSizeMake(100, 100))
cloudSprite.position = position
self.addChild(cloudSprite)
var action = SKAction.moveTo(CGPointMake(-cloudSprite.size.width, cloudSprite.position.y), duration: 6)
var sequence = SKAction.sequence([SKAction.waitForDuration(delay),action])
cloudSprite.runAction(sequence, completion: { () -> Void in
cloudSprite.removeFromParent()
self.addCloudSprite(position,delay:0)
})
}
override func didMoveToView(view: SKView) {
self.addCloudSprite(CGPointMake(self.frame.size.width+50, 200),delay: 0)
self.addCloudSprite(CGPointMake(self.frame.size.width+50 , 500),delay: 3)
}
答案 1 :(得分:0)
对于记录,要删除精灵,可以使用此方法执行此操作。
self.removeChildrenInArray([cloudSprite1a])
据我所知,它只能在数组中完成,因此你需要[]标记。