Code does not do anything?


#1
def __init__(self):
	ShowBase.__init__(self)
	
	self.acceptonce("a", self.moveleft)

def moveleft(self):
		self.setpos(30,0,0)

I’ve been working hours on figuring out why this wouldn’t work.


#2

Could you please describe what you mean by “wouldn’t work”? Is the code above giving an error message? If so, please post the error message. Otherwise, please describe what you are expecting to see and what you are seeing.

Could you post the whole, original code? As-is, the code has several errors, such as the use of “acceptonce” instead of “acceptOnce”, and “setpos” instead of “setPos”, wrong intendation level for “def moveleft”, and it doesn’t show what you are intending to call “setPos” on.


#3

It doesn’t give me an error, just pressing A does nothing. The entire code is under a class. I’d assume that Self would know I am referring to the class I set up. The class is the entire Model so fari think.

Here’s the code minus all of that showbases and directs on top.

class Window(ShowBase):
	
	Whiskers = loader.loadModel((redacted)) #The character.
		
#end making the model

	
	#Start moving left
	def __init__(self):
		ShowBase.__init__(self)
		
		self.acceptOnce("a", self.moveleft)
	
	def moveleft(self):
		self.setPos(30,0,0)

#4

Well, you’re calling setPos on the ShowBase instance, and not on the model. ShowBase doesn’t have a setPos(), so I would expect you to get an error message.

You probably meant to do self.Whiskers.setPos(30, 0, 0), though the lack of error indicates that maybe your moveleft() function isn’t being called at all.


#5

I did a little test and tried putting moveleft() underneath
the code and it gave me an error saying that it wasn’t
defined… even though it is?

NameError: name ‘moveleft’ is not defined


#6

You would need to call self.moveleft(), since it is defined as a method on the Window class, so you should call it on an instance of your Wnidow class.


#7

Yeah still nothing. Trying to use Ralphie code doesn’t make it do anything either. This is where i am at now

    def __init__(self):
    	ShowBase.__init__(self)
		
		
    	self.keyMap = {
		"left": 0, "right": 0}
		
	self.accept("arrow_left", self.setKey, ["left", True])
	self.accept("arrow_right", self.setKey, ["right", True])
	
	def moveleft(self):
		if self.keyMap["left"]:
			self.Whiskers.setH(self.Whiskers.getH() + 300 * dt)
		if self.keyMap["right"]:
			self.Whiskers.setH(self.Whiskers.getH() - 300 * dt)
	self.moveleft()

In addition I noticed Ralphie doesn’t use such a thing at the bottom.


#8
from direct.showbase.ShowBase import ShowBase

class Window(ShowBase):

    def __init__(self):
        ShowBase.__init__(self)
        
        base.disableMouse()
        camera.set_pos(0, -10, 0,)

        self.Whiskers = loader.loadModel("box") #The character.
        self.Whiskers.reparent_to(render)

        self.acceptOnce("a", self.moveleft)
        
    def moveleft(self):
    
        self.Whiskers.setPos(3,0,0)
        

app = Window()
app.run()

Did you want this?


#9

Huh. That actually worked, thanks.