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 module create my own extra module

    Pythonista
    2
    4
    1799
    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.
    • Drizzel
      Drizzel last edited by

      Hi guys, I'm coding a game of mine, and it's becoming a bit too long to navigate around easily, so I was wondering if I could possibly create my own module and store all of my definitions in it? Whenever I create a definition in my module and then call it in my main program I get an error: "module 'Module' has no attribute 'removeTower'" Also Im self taught, so it may very well be that I'm missing some simple piece.

      This here is my module:

      from scene import *
      import sound
      import random
      import math
      A = Action
      
      class MyScene (Scene):
      	def setup(self):
      		pass
      	
      	def did_change_size(self):
      		pass
      	
      	def update(self):
      		pass
      	
      	def touch_began(self, touch):
      		pass
      	
      	def touch_moved(self, touch):
      		pass
      	
      	def touch_ended(self, touch):
      		def removeTower():
      			global rockTowerBoolean
      			global archerTowerBoolean
      			global fireTowerBoolean
      			if rockTowerBoolean == True:
      				self.rockTower.remove_from_parent()
      				rockTowerBoolean = False
      			elif archerTowerBoolean == True:
      				self.archerTower.remove_from_parent()
      				archerTowerBoolean = False
      			elif fireTowerBoolean == True:
      				self.fireTower.remove_from_parent()
      				fireTowerBoolean = False
      		pass
      
      if __name__ == '__main__':
      	run(MyScene(), show_fps=False)
      
      1 Reply Last reply Reply Quote 0
      • JonB
        JonB last edited by

        Rather than global rocktower, etc, create instance variables. i.e in setup, define self.rocktower=..., then elsewhere you can refer to those instance variables.

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

          also, removeTower is a function within touchEnded. What you want is

          def removeTower(self)

          make sure the indent level is the same as the level of touchEnded, etc.

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

            Great thank you,
            It works now :D

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