【问题标题】:create multiple sprites(nodes) with same texture swift快速创建具有相同纹理的多个精灵(节点)
【发布时间】:2015-09-19 12:25:23
【问题描述】:

所以我正在尝试制作一个游戏,我必须生成一排向上加速的节点。但是每次如果我添加节点我都会得到一个错误(因为节点已经有一个父节点)显然我添加了Sprite.removeFromParent()

在图片中,您可以看到我正在尝试实现的内容。 无论如何,积木还必须记录它们被触摸的次数。

【问题讨论】:

    标签: swift sprite-kit sprite skspritenode


    【解决方案1】:

    回答标题中的问题:

    很简单,创建一个SKTexture 并为每个SKSpriteNode 设置纹理为SKTexture。其代码如下所示:

    // Create the texture
    var boxTexture = SKTexture(imageNamed: "boxImage")
    for (var i = 0; i < 10; i++) {
        // Create box with defined texture
        var box = SKSpriteNode(texture: boxTexture);
        // Set position of box dynamically
        box.position = CGPointMake(CGFloat(i * 20), CGFloat(512));
        // Name for easier use (may need to change if you have multiple rows generated)
        box.name = "box"+String(i);
        // Add box to scene
        addChild(box);
    }
    

    您在描述中遇到的问题似乎是,当您需要多个通过底部时,您只创建了一个SKSpriteNode。只需使用for 循环创建多个,并将它们全部添加到场景中(基本上,上面的代码相同)。您可能希望为它们提供所有name 属性,以便稍后确定哪个框(如果您不将它们存储为全局变量)。如果您有多行同时移动的框,请务必更改命名。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-23
      • 1970-01-01
      • 2017-04-05
      • 1970-01-01
      相关资源
      最近更新 更多