omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Scene transitions

    Pythonista
    scene
    7
    27
    26441
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ccc
      ccc last edited by ccc

      Scenes have changed a lot since those posts were written and now multi scene support is builtin.

      See: scene.Scene.present_modal_scene() and scene.Scene.presented_scene.

      1 Reply Last reply Reply Quote 0
      • mrcoxall
        mrcoxall last edited by

        I am having a problem presenting the second scene with the present_modal_scene().
        Does anyone have an example of the best practises using this?

        This is what I have so far:
        (note: I am using a UI.view since I want to place it in the PythonistaAppTemplate and then into Xcode)

        # for use with https://github.com/omz/PythonistaAppTemplate
        
        from scene import *
        import ui
        
        class FirstScene (Scene):
            def setup(self):
                self.background_color = 'midnightblue'
                self.ship = SpriteNode('spc:PlayerShip1Orange')
                self.ship.position = self.size / 2
                self.add_child(self.ship)
        
            def touch_began(self, touch):
                # question?
                # how do you get to the second scene? 
                self.present_modal_scene(my_second_scene)
                
        class SecondScene (Scene):
            def setup(self):
                self.background_color = '#704414'
                self.ship = SpriteNode('spc:EnemyGreen2')
                self.ship.position = self.size / 2
                self.add_child(self.ship)
        
            def touch_began(self, touch):
                pass
        
        my_scene = FirstScene()
        my_second_scene = SecondScene()
        # Instead of...
        # run(MyScene())
        
        # ..use:
        main_view = ui.View()
        scene_view = SceneView(frame=main_view.bounds, flex='WH')
        main_view.add_subview(scene_view)
        scene_view.scene = my_scene
        main_view.present(hide_title_bar=True, animated=False)
        
        1 Reply Last reply Reply Quote 0
        • omz
          omz last edited by

          @mrcoxall Your code works for me, but note that modal scenes don't draw any background (i.e. the background_color attribute is ignored). You might want to use a colored SpriteNode to prevent the first scene from shining through. This behavior is intentional because a common use case for present_modal_scene is to present a menu overlay with transparent background (as in the included game examples).

          1 Reply Last reply Reply Quote 0
          • mrcoxall
            mrcoxall last edited by

            So omz was correct and the background was the first problem.
            When I do it for just a single scene, it all seems to work fine.

            My next problem is that I tried to make it move from one scene to a second.
            Problem is I am getting a "Value error: inconsistent Node hierarchy", when I try to move from the second scene back to the first one.

            The good news is that I placed it in Xcode and the App template works great.
            Here is my code:

            # for use with https://github.com/omz/PythonistaAppTemplate
            
            from scene import *
            import ui
            
            class MainScene (Scene):
                def setup(self):
                    # this is the starting scene, for ui.View
                    # all other scenes will come from
                    
                    self.background_color = 'black'
                    self.present_modal_scene(first_scene)
            
            
            class FirstScene (Scene):
                def setup(self):
                    # add background color
                    self.background = SpriteNode(color = 'white')
                    self.background.size = self.size
                    self.background.position = self.size / 2
                    self.add_child(self.background)
                    
                    self.ship = SpriteNode('spc:PlayerShip1Orange')
                    self.ship.position = self.size / 2
                    self.add_child(self.ship)
                
                def update(self):
                    pass
                #x, y, z = gravity()
                #pos = self.ship.position
                #pos += (x * 15, y * 15)
                # Don't allow the ship to move beyond the screen bounds:
                #pos.x = max(0, min(self.size.w, pos.x))
                #pos.y = max(0, min(self.size.h, pos.y))
                #self.ship.position = pos
                
                def touch_began(self, touch):
                    # question?
                    # how do you get to the second scene?
                    #self.dismiss_modal_scene()
                    self.present_modal_scene(second_scene)
                
                def touch_moved(self, touch):
                    pass
                
                def touch_ended(self, touch):
                    pass
            
            
            class SecondScene (Scene):
                def setup(self):
                    # add background color
                    self.background = SpriteNode(color = 'green')
                    self.background.size = self.size
                    self.background.position = self.size / 2
                    self.add_child(self.background)
                    
                    self.ship = SpriteNode('spc:EnemyGreen2')
                    self.ship.position = self.size / 2
                    self.add_child(self.ship)
                
                def touch_began(self, touch):
                    self.present_modal_scene(first_scene)
            #pass
            
            
            main_scene = MainScene()
            first_scene = FirstScene()
            second_scene = SecondScene()
            # Instead of...
            # run(MyScene())
            
            # ..use:
            main_view = ui.View()
            scene_view = SceneView(frame=main_view.bounds, flex='WH')
            main_view.add_subview(scene_view)
            scene_view.scene = first_scene
            main_view.present(hide_title_bar=True, animated=False)
            
            1 Reply Last reply Reply Quote 0
            • omz
              omz last edited by

              @mrcoxall Use dismiss_modal_scene instead of present_modal_scene to get back from the second scene to the first one.

              1 Reply Last reply Reply Quote 0
              • ccc
                ccc last edited by ccc

                from scene import *
                
                
                class FirstScene(Scene):
                    def setup(self):
                        # add white background
                        SpriteNode(anchor_point=(0, 0), color='white', parent=self,
                                   size=self.size)
                        self.ship = SpriteNode('spc:PlayerShip1Orange', parent=self,
                                               position=self.size / 2)
                    
                    def touch_began(self, touch):
                        self.present_modal_scene(SecondScene())
                
                
                class SecondScene(Scene):
                    def setup(self):
                        # add green background
                        SpriteNode(anchor_point=(0, 0), color='green', parent=self,
                                   size=self.size)
                        self.ship = SpriteNode('spc:EnemyGreen2', parent=self,
                                               position=self.size / 2)
                    
                    def touch_began(self, touch):
                        self.dismiss_modal_scene()
                
                
                if __name__ == '__main__':
                    scene_view = SceneView()
                    scene_view.scene = FirstScene()
                    scene_view.present(hide_title_bar=True, animated=False)
                
                1 Reply Last reply Reply Quote 0
                • mrcoxall
                  mrcoxall last edited by

                  Thanks again to everyone for their help, I am getting close to a "scene solution" I can use.

                  The problem I have now is, I would like to use a timer, so that after a second it automatically moves to the next scene. I would like to use it for a splash screen at the beginning. Because it is modal though, the scene is still running in the background.

                  Is there any way to not have it modal or to actually really completely remove the scene, so it is not running and then go to the new one? There will be some scenes I will only ever use once, so removing them from memory would be the best.

                  # for use with https://github.com/omz/PythonistaAppTemplate
                  
                  from scene import *
                  import ui
                  import time
                  
                  class FirstScene (Scene):
                  
                      # global variable to class
                      start_time = None
                      
                      def setup(self):
                          # add background color
                          self.background = SpriteNode(color = 'white')
                          self.background.size = self.size
                          self.background.position = self.size / 2
                          self.add_child(self.background)
                          
                          # get starting time
                          global start_time
                          start_time = time.time() 
                          
                          self.ship = SpriteNode('spc:PlayerShip1Orange')
                          self.ship.position = self.size / 4
                          self.add_child(self.ship)
                          
                      def update(self):
                          # move to new scene after 1 second
                          # problem is it keeps happening, since it is modal
                          # is there a way to actually really dispose 
                          # of a scene and then move forward?
                          
                          current_time = time.time()
                          if current_time - start_time > 1:
                              #self.present_modal_scene(second_scene)
                              pass
                  
                      def touch_began(self, touch):
                          #self.dismiss_modal_scene()
                          #self.present_modal_scene(second_scene)
                          pass
                          
                      def touch_moved(self, touch):
                          pass
                          
                      def touch_ended(self, touch):
                          if self.ship.frame.contains_point(touch.location):
                              self.present_modal_scene(second_scene)
                          
                          
                  class SecondScene (Scene):
                  	
                      # global variable to class
                      start_time = None
                      
                      def setup(self):
                          # add background color
                          self.background = SpriteNode(color = 'green')
                          self.background.size = self.size
                          self.background.position = self.size / 2
                          self.add_child(self.background)
                          
                          # get starting time
                          global start_time
                          start_time = time.time()
                          
                          self.ship = SpriteNode('spc:EnemyGreen2')
                          self.ship.position = self.size / 2
                          self.add_child(self.ship)
                          
                      def update(self):
                          # move to new scene after 1 second
                          current_time = time.time()
                          if current_time - start_time > 1:
                              #self.present_modal_scene(second_scene)
                              pass
                  
                      def touch_began(self, touch):
                          
                          self.present_modal_scene(main_menu_scene)
                      
                      
                  class MainMenuScene (Scene):
                      def setup(self):
                          # add background color
                          self.background = SpriteNode(color = 'blue')
                          self.background.size = self.size
                          self.background.position = self.size / 2
                          self.add_child(self.background)
                          
                          self.ace = SpriteNode('card:ClubsA')
                          self.ace.position = (683.0, 512.0)
                          self.add_child(self.ace)
                          
                          self.king = SpriteNode('card:ClubsK')
                          self.king.position = (683.0, 750.0)
                          self.add_child(self.king)
                          
                          self.queen = SpriteNode('card:ClubsQ')
                          self.queen.position = (683.0, 238.0)
                          self.add_child(self.queen)
                          
                      def update(self):
                          pass
                  
                      def touch_began(self, touch):
                          #self.dismiss_modal_scene()
                          #self.present_modal_scene(a_scene)
                          pass
                          
                      def touch_moved(self, touch):
                          pass
                          
                      def touch_ended(self, touch):
                          # move to new scenes
                          if self.ace.frame.contains_point(touch.location):
                              self.present_modal_scene(a_scene)
                          elif self.king.frame.contains_point(touch.location):
                              self.present_modal_scene(b_scene)
                          elif self.queen.frame.contains_point(touch.location):
                              self.present_modal_scene(c_scene)
                          
                          
                  class AScene (Scene):
                      def setup(self):
                          # add background color
                          self.background = SpriteNode(color = 'white')
                          self.background.size = self.size
                          self.background.position = self.size / 2
                          self.add_child(self.background)
                           
                          self.a_choise = SpriteNode('card:BackBlue1')
                          self.a_choise.position = self.size / 2
                          self.add_child(self.a_choise)
                          
                      def update(self):
                          pass
                  
                      def touch_began(self, touch):
                          self.dismiss_modal_scene()
                          #self.present_modal_scene(b_scene)
                          
                      def touch_moved(self, touch):
                          pass
                          
                      def touch_ended(self, touch):
                          pass
                  
                  
                  class BScene (Scene):
                      def setup(self):
                          # add background color
                          self.background = SpriteNode(color = 'white')
                          self.background.size = self.size
                          self.background.position = self.size / 2
                          self.add_child(self.background)
                          
                          self.ship = SpriteNode('card:BackGreen1')
                          self.ship.position = self.size / 2
                          self.add_child(self.ship)
                          
                      def update(self):
                          pass
                  
                      def touch_began(self, touch):
                          self.dismiss_modal_scene()
                          #self.present_modal_scene(c_scene)
                          
                      def touch_moved(self, touch):
                          pass
                          
                      def touch_ended(self, touch):
                          pass
                  
                  
                  class CScene (Scene):
                      def setup(self):
                          # add background color
                          self.background = SpriteNode(color = 'white')
                          self.background.size = self.size
                          self.background.position = self.size / 2
                          self.add_child(self.background)
                          
                          self.ship = SpriteNode('card:BackRed1')
                          self.ship.position = self.size / 2
                          self.add_child(self.ship)
                          
                      def update(self):
                          pass
                  
                      def touch_began(self, touch):
                          self.dismiss_modal_scene()
                          #self.present_modal_scene(second_scene)
                          
                      def touch_moved(self, touch):
                          pass
                          
                      def touch_ended(self, touch):
                          pass
                  
                  
                  first_scene = FirstScene()
                  second_scene = SecondScene()
                  main_menu_scene = MainMenuScene()
                  a_scene = AScene()
                  b_scene = BScene()
                  c_scene = CScene()
                  # Instead of...
                  # run(MyScene())
                  
                  # ..use:
                  main_view = ui.View()
                  scene_view = SceneView(frame=main_view.bounds, flex='WH')
                  main_view.add_subview(scene_view)
                  scene_view.scene = first_scene
                  main_view.present(hide_title_bar=True, animated=False)
                  
                  1 Reply Last reply Reply Quote 0
                  • mrcoxall
                    mrcoxall last edited by

                    The following is the best I have been able to come up with as a template for scene management. I have a timer for the fist scene. Unfortunately I cannot get one working for the second.

                    I am still looking for a "non-modal" way to present a scene or a way to completely remove a scene, so that it is no longer running.
                    Thanks again for any help.

                    # for use with https://github.com/omz/PythonistaAppTemplate
                    
                    from scene import *
                    import ui
                    import time
                    
                    class FirstScene (Scene):
                    
                        # global variable to class
                        start_time = None
                           
                        def setup(self):
                            # add background color
                            self.background = SpriteNode(color = 'white')
                            self.background.size = self.size
                            self.background.position = self.size / 2
                            self.add_child(self.background)
                            
                            # get starting time
                            global start_time
                            start_time = time.time() 
                                   
                            self.ship = SpriteNode('test:Pythonista')
                            self.ship.position = self.size / 2
                            self.ship.size = self.size
                            self.add_child(self.ship)
                            
                        def update(self):
                            # move to new scene after 2 second
                            # problem is it keeps happening, since it is modal
                            # is there a way to actually really dispose 
                            # of a scene and then move forward?
                                  
                            current_time = time.time()
                            
                            #global scene_moved_away_from
                            if current_time - start_time > 2:
                                self.present_modal_scene(second_scene)
                    
                        def touch_began(self, touch):
                            #self.present_modal_scene(second_scene)
                            pass
                            
                        def touch_moved(self, touch):
                            pass
                            
                        def touch_ended(self, touch):
                            if self.ship.frame.contains_point(touch.location):
                                self.present_modal_scene(second_scene)
                            
                            
                    class SecondScene (Scene):
                    
                        # global variable to class
                        start_time = None
                        
                        def setup(self):
                            # add background color
                            self.background = SpriteNode(color = 'green')
                            self.background.size = self.size
                            self.background.position = self.size / 2
                            self.add_child(self.background)
                            
                            start_time = time.time()
                            
                            self.ship = SpriteNode('spc:EnemyGreen2')
                            self.ship.position = self.size / 2
                            self.add_child(self.ship)
                            
                        def update(self):
                            # move to new scene after 2 second
                            current_time = time.time()
                            
                            # have to comment out because get "max recursion error"
                            # code keeps reloading first then second scene over and
                            # over again, since time has lapsed
                            #if current_time - start_time > 2:
                                #self.present_modal_scene(second_scene)
                                #pass
                    
                        def touch_began(self, touch):
                            self.pause = True
                            self.present_modal_scene(main_menu_scene)
                            
                        def touch_moved(self, touch):
                            pass
                            
                        def touch_ended(self, touch):
                            pass
                        
                        
                    class MainMenuScene (Scene):
                        
                        def setup(self):
                            # add background color
                            self.background = SpriteNode(color = 'blue')
                            self.background.size = self.size
                            self.background.position = self.size / 2
                            self.add_child(self.background)
                            
                            self.ace = SpriteNode('card:ClubsA')
                            self.ace.position = (683.0, 512.0)
                            self.add_child(self.ace)
                            
                            self.king = SpriteNode('card:ClubsK')
                            self.king.position = (683.0, 750.0)
                            self.add_child(self.king)
                            
                            self.queen = SpriteNode('card:ClubsQ')
                            self.queen.position = (683.0, 238.0)
                            self.add_child(self.queen)
                            
                        def update(self):
                            pass
                    
                        def touch_began(self, touch):
                            pass
                            
                        def touch_moved(self, touch):
                            pass
                            
                        def touch_ended(self, touch):
                            # move to new scenes
                            if self.ace.frame.contains_point(touch.location):
                                self.present_modal_scene(a_scene)
                            elif self.king.frame.contains_point(touch.location):
                                self.present_modal_scene(b_scene)
                            elif self.queen.frame.contains_point(touch.location):
                                self.present_modal_scene(c_scene)
                            
                            
                    class AScene (Scene):
                        
                        def setup(self):
                            # add background color
                            self.background = SpriteNode(color = 'white')
                            self.background.size = self.size
                            self.background.position = self.size / 2
                            self.add_child(self.background)
                             
                            self.blue_card = SpriteNode('card:BackBlue1')
                            self.blue_card.position = self.size / 2
                            self.add_child(self.blue_card)
                            
                        def update(self):
                            pass
                    
                        def touch_began(self, touch):
                            pass
                            
                        def touch_moved(self, touch):
                            pass
                            
                        def touch_ended(self, touch):
                            # moving back to previous scene, so remove modal makes sense
                            if self.blue_card.frame.contains_point(touch.location):
                                self.dismiss_modal_scene()
                    
                    
                    class BScene (Scene):
                        
                        def setup(self):
                            # add background color
                            self.background = SpriteNode(color = 'white')
                            self.background.size = self.size
                            self.background.position = self.size / 2
                            self.add_child(self.background)
                            
                            self.green_card = SpriteNode('card:BackGreen1')
                            self.green_card.position = self.size / 2
                            self.add_child(self.green_card)
                            
                        def update(self):
                            pass
                    
                        def touch_began(self, touch):
                            pass
                            
                        def touch_moved(self, touch):
                            pass
                            
                        def touch_ended(self, touch):
                            # moving back to previous scene, so remove modal makes sense
                            if self.green_card.frame.contains_point(touch.location):
                                self.dismiss_modal_scene()
                    
                    
                    class CScene (Scene):
                        
                        def setup(self):
                            # add background color
                            self.background = SpriteNode(color = 'white')
                            self.background.size = self.size
                            self.background.position = self.size / 2
                            self.add_child(self.background)
                            
                            self.red_card = SpriteNode('card:BackRed1')
                            self.red_card.position = self.size / 2
                            self.add_child(self.red_card)
                            
                        def update(self):
                            pass
                    
                        def touch_began(self, touch):
                            pass
                            
                        def touch_moved(self, touch):
                            pass
                            
                        def touch_ended(self, touch):
                            # moving back to previous scene, so remove modal makes sense
                            if self.red_card.frame.contains_point(touch.location):
                                self.dismiss_modal_scene()
                    
                    
                    first_scene = FirstScene()
                    second_scene = SecondScene()
                    main_menu_scene = MainMenuScene()
                    a_scene = AScene()
                    b_scene = BScene()
                    c_scene = CScene()
                    # Instead of...
                    # run(MyScene())
                    
                    # ..use:
                    main_view = ui.View()
                    scene_view = SceneView(frame=main_view.bounds, flex='WH')
                    main_view.add_subview(scene_view)
                    scene_view.scene = first_scene
                    main_view.present(hide_title_bar=True, animated=False)
                    
                    1 Reply Last reply Reply Quote 0
                    • ccc
                      ccc last edited by ccc

                      If you want transitory scenes then create the main scene in your main program but then only create overlaying scenes when you actually need them. That is, do not create scenes in your main() if you want them to be transitory.

                      • Try creating sceneA inside the body of the MainScene class.
                      • Try creating sceneB inside the body of the sceneA class.
                      • Try creating sceneC inside the body of the sceneB class.

                      As variables like "sceneC" fall out of scope, they can be garbage collected but those scenes that are defined in main() can not be garbage collected until main() ends.

                      See the code above where FirstScene.touch_begin() creates and displays a SecondScene. Then SecondScene.touch_begin() destroys self to go back to the FirstScene.

                      1 Reply Last reply Reply Quote 0
                      • ccc
                        ccc last edited by ccc

                        My "Pull Request"...

                        • Uses self.presented_sceneto know if there is a modal scene above self.
                        • Removes both class variables and global variables -- they are NOT the same thing.
                        • Does a two second hop from 1st scene to 2nd scene and a two second hop from 2nd scene to 3rd scene without recursion issues.
                        • Creates scenes on-the-fly when needed to allow them to be garbage collected once they have been closed.
                        • Adds card_back_colorto allow one scene class to show card backs of different colors.
                        # for use with https://github.com/omz/PythonistaAppTemplate
                        
                        from scene import *
                        import time
                        
                        
                        class FirstScene(Scene):
                            def setup(self):
                                # get starting time
                                self.start_time = time.time()
                                # add background color
                                SpriteNode(anchor_point=(0, 0), color='white', parent=self,
                                           size=self.size)
                                self.ship = SpriteNode('test:Pythonista', anchor_point=(0, 0),
                                                       parent=self, size=self.size)
                                
                            def update(self):
                                # move to new scene after 2 seconds
                                if not self.presented_scene and time.time() - self.start_time > 2:
                                    self.present_modal_scene(SecondScene())
                        
                            def touch_began(self, touch):
                                # self.present_modal_scene(SecondScene())
                                pass
                        
                            def touch_ended(self, touch):
                                if self.ship.frame.contains_point(touch.location):
                                    self.present_modal_scene(SecondScene())
                                
                                
                        class SecondScene(Scene):
                            def setup(self):
                                self.start_time = time.time()
                                # add background color
                                SpriteNode(anchor_point=(0, 0), color='green', parent=self,
                                           size=self.size)
                                self.ship = SpriteNode('spc:EnemyGreen2', parent=self,
                                                       position=self.size/2)
                                
                            def update(self):
                                if not self.presented_scene and time.time() - self.start_time > 2:
                                    self.present_modal_scene(MainMenuScene())
                        
                            def touch_began(self, touch):
                                self.present_modal_scene(MainMenuScene())
                            
                        
                        class MainMenuScene (Scene):
                            def setup(self):
                                # add background color
                                SpriteNode(anchor_point=(0, 0), color='blue', parent=self,
                                           size=self.size)
                                
                                self.ace = SpriteNode('card:ClubsA', parent=self, position=(683, 512))
                                self.king = SpriteNode('card:ClubsK', parent=self, position=(683, 750))
                                self.queen = SpriteNode('card:ClubsQ', parent=self, position=(683, 238))
                                
                            def touch_ended(self, touch):
                                # move to new scenes
                                if self.ace.frame.contains_point(touch.location):
                                    self.present_modal_scene(CardBackScene('blue'))
                                elif self.king.frame.contains_point(touch.location):
                                    self.present_modal_scene(CardBackScene('green'))
                                elif self.queen.frame.contains_point(touch.location):
                                    self.present_modal_scene(CardBackScene('red'))
                                
                                
                        class CardBackScene(Scene):
                            def __init__(self, card_back_color='blue'):
                                Scene.__init__(self)
                                self.img_name = 'card:Back{}1'.format(card_back_color.title())
                        
                            def setup(self):
                                # add background color
                                SpriteNode(anchor_point=(0, 0), color='white', parent=self,
                                           size=self.size)
                                self.card = SpriteNode(self.img_name, parent=self,
                                                       position=self.size/2)
                        
                            def touch_ended(self, touch):
                                # moving back to previous scene, so remove modal makes sense
                                if self.card.frame.contains_point(touch.location):
                                    self.dismiss_modal_scene()
                        
                        
                        main_view = SceneView()
                        main_view.scene = FirstScene()
                        main_view.present(hide_title_bar=True, animated=False)
                        
                        1 Reply Last reply Reply Quote 0
                        • mrcoxall
                          mrcoxall last edited by

                          Works great.
                          Thanks so much.

                          Patrick

                          1 Reply Last reply Reply Quote 0
                          • PeterG
                            PeterG last edited by

                            @ccc, just something I need , very nice indeed! I import a game, that for grand-son and daughter, and run it before the background cardimages are shown, meeaning I am on the way for an 4-level game ...
                            Next try, just using one of the marvallous available games , as next levels ..
                            .. Topscore files will be used for further interaction ..

                            Thanks to your example ... Helps a lot 😊 , happy OpaPeter

                            1 Reply Last reply Reply Quote 0
                            • First post
                              Last post
                            Powered by NodeBB Forums | Contributors