Forum Archive

Scene module create my own extra module

Drizzel

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)
JonB

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

JonB

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.

Drizzel

Great thank you,
It works now :D