简体   繁体   English

屏幕上同时有两个相同的节点 Swift SpriteKit

[英]Have two of the same nodes on the screen at once Swift SpriteKit

I am creating a space shooter game.我正在创建一个太空射击游戏。 I have created an enemy node.我创建了一个敌人节点。 What I want to do is have 4 of those nodes in each corner of the screen.我想要做的是在屏幕的每个角落有 4 个节点。 In other words, I want to spawn multiple copies of the same node at the same time.换句话说,我想同时生成同一个节点的多个副本。 When the game loads, I want there to be four nodes, that are all the same.当游戏加载时,我希望有四个节点,它们都是一样的。 How can I do this?我怎样才能做到这一点?

Thanks谢谢

Here's a section of code I use to generate a row of 4 space invaders.这是我用来生成一行 4 个空间入侵者的一段代码。 I reuse the same SKSpriteNode() variable as once the node has been added to the scene, the variable is no longer required:我重用了相同的 SKSpriteNode() 变量,因为一旦将节点添加到场景中,就不再需要该变量:

    for invaderPosition in 100.stride(to: 500, by: 120) {
        invader = SKSpriteNode(texture: texturesA[0])
        invader.position = CGPoint(x: CGFloat(invaderPosition), y: 200)
        invader.xScale = 8
        invader.yScale = 6
        invader.color = SKColor.redColor()
        invader.colorBlendFactor = 1.0
        invader.name = "InvaderA"
        invader.texture = texturesA[1]
          invader.runAction(SKAction.repeatActionForever(SKAction.animateWithTextures(texturesA, timePerFrame: self.timePerMove)))
        addChild(invader)
    }

invader is defined as an SKSpriteNode property.入侵者被定义为 SKSpriteNode 属性。 texturesA is just an array of SKTextures ; texturesA只是一个SKTextures数组; timePerMove is just a time interval after which the invader should move (in Update ) and change shape(texture). timePerMove 只是入侵者应该移动(在Update )并改变形状(纹理)的时间间隔。

var invader = SKSpriteNode()
var texturesA:[SKTexture] = []
let timePerMove: CFTimeInterval = 1.0

So you could do something similar except the invader's positions would be the 4 corners rather than a row.所以你可以做类似的事情,除了入侵者的位置是 4 个角落而不是一行。

只需使用复制命令,

let newNode = originalNode.copy() as! SKSpriteNode;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM