代码之家  ›  专栏  ›  技术社区  ›  grahamcracker1234

我可以让SKSpriteNode在过渡期间出现在不同的场景上吗

  •  0
  • grahamcracker1234  · 技术社区  · 9 年前

    我正在制作一个游戏,有一个我正在处理的自定义过渡。当你点击TitleScene上的playButton时,它会移动游戏场景。这是它的gif。

    enter image description here

    现在我想让这些扣子掉下来 结束 或者在另一场景的顶部看到。因此,基本上按钮需要在游戏场景上,即使它们是标题场景的一部分。这是我的转换代码:

    let transition = SKTransition.revealWithDirection(SKTransitionDirection.Up, duration: 2)
    transition.pausesIncomingScene = false
    transition.pausesOutgoingScene = false
    
    currentScene.view?.presentScene(futureScene, transition: transition)
    

    我不知道该怎么做。需要任何帮助。

    更新

    我已经意识到,唯一的方法是将SKSpriteNodes从TitleScene复制到GameScene。签出此代码:

    func createTransition(transitionFrom presentScene: SKScene, to futureScene: SKScene, withTransition transition: transitionTypes) {
    
    var yPositions : [CGFloat] = []
    for child in presentScene.children as! [SKSpriteNode] {
        yPositions.append(child.position.y)
    }
    let topSprite = yPositions.maxElement() // finds the sprite closest to the top
    
    for child in presentScene.children as! [SKSpriteNode] {
        if child.position.y == topSprite {
            var vel = CGFloat.random(-1, 1)
            if (vel < 0.3 && vel >= 0) {
                vel += 0.5
            }
            if (vel > -0.3 && vel <= 0) {
                vel -= 0.5
            }
            let fallAction = SKAction.applyTorque(vel, duration: 0.5) // slightly turns the Sprite
    
            presentScene.physicsWorld.gravity.dx = CGFloat.random(-0.3,0.3)
    
            child.physicsBody?.affectedByGravity = true // Become affected by gravity and falls
    
            child.runAction(fallAction, completion: {
                for otherChild in presentScene.children as! [SKSpriteNode] {
    
                    if otherChild.name != "background" { //copies all Sprites except the background 
    
                        let copiedChild = otherChild.copy() as! SKSpriteNode
                        copiedChild.position.y += futureScene.frame.size.height
    
                        futureScene.physicsWorld.speed = presentScene.physicsWorld.speed
                        futureScene.physicsWorld.gravity = presentScene.physicsWorld.gravity //makes it so they have the same physicsWorlds
    
                        futureScene.addChild(copiedChild) 
                    }
                }
            })
    
            let transition = SKTransition.pushWithDirection(SKTransitionDirection.Up, duration: 2)
            transition.pausesIncomingScene = false
            transition.pausesOutgoingScene = false
    
            presentScene.view?.presentScene(futureScene, transition: transition)
        }
    }
    

    }

    这就是它的样子:

    enter image description here

    你可以看到他们没有排好队,有人知道我遗漏了什么吗?

    2 回复  |  直到 9 年前
        1
  •  1
  •   Community CDub    7 年前

    场景1:一个大场景

    +------------+   }
    |            |   }
    |   Title    |   }
    |            |   }
    |            |   }
    |            |   }   Initially visible half of full scene.
    | btn    btn |   }
    |    Play    |   }
    | btn    btn |   }
    +------------+   }  }
    |            |      }
    |            |      }
    |            |      }
    |            |      }
    |   game     |      }   Half of full scene visible after play button is pressed
    |            |      }    (the game)
    |            |      }
    |            |      }
    +------------+      }
    

    什么时候 Play 按下时,这个大矩形(14x19)会向上移动,因此顶部矩形不可见,底部矩形可见。因为从技术上讲,这是一个大场景,按钮/标题可以在两个“场景”中自由移动(这实际上只是一个大的场景)。

    场景2:两个“场景”重叠

                 +------------+   }
                 |            |   }
                 |   Title    |   }
                 |            |   }
                 |            |   }
                 |            |   }   Initially visible half of big scene.
                 | btn    btn |   }     z-pos of the rectangle on the right is higher than
                 |    Play    |   }      the z-pos of the one on the left, thereby allowing
                 | btn    btn |   }     its contents to be visible above those of the gamescene.
    +------------+            |   }  }
    |            |            |      }
    |            |            |      }
    |            |            |      }
    |            |            |      }
    |   game     |            |      }   Half of scene and gamescene visible after play button 
    |            |            |      }    is pressed (the game)
    |            |            |      }
    |            |            |      }
    +------------+------------+      }
    

    这些到场景重叠,最右侧场景的下半部分与最左侧场景匹配。当按下播放按钮时,两个场景以相同的速率向上移动,直到只有底部场景可见。实际上,最右侧场景的下半部分仍然位于较小场景的顶部。

    这个 alpha 更大的场景是 0 。对于大场景的上半部分,可以添加一个背景节点;这个设置允许游戏可见,按钮仍然落在游戏上方。

    为了确保不会因为技术上有一个场景(可能会妨碍交互)而无法与游戏场景交互,请移除较大的场景或将其z位置更改为低于游戏场景。这可能不是必要的,但万一是……你知道的。

    请记住,您不能真正嵌入场景(至少根据 this ),所以你不能在场景2中真正使用两个场景。你可以选择将游戏场景放在SKNode或类似的场景中作为解决方案。

        2
  •  0
  •   Luca Angeletti    9 年前

    您应该创建按钮的副本,并将它们添加到 GameScene .

    在上设置按钮的初始位置 游戏场景 等于上按钮的最后位置 TitleScene .

    希望效果会是你想要的。