Keys,, hopelessness.. not yet...

Hi again…

I was looking for a particular style of pressing keys, for example:

let say that “a” is left and “d” is right.

if you press “a”, the player model will move to left, is the common system, but you must to keep pressed “a” to move far, while you want to move your player farest you must to keep pressed the button.

But i looking for the way to do just with one time key pressing, you press only 1 time the key for example “a” (pulse “a” and release it inmediately), and player will move to left and not stop, until you press “d” player will rotate 180º and go to right, the goal is player never stops moving, you dont need to keep pressed “a” or “d” to move, its move by itself…

If this is confusing, i can put a video to explain the system
If you know Elevator Repairman for Atari, you will understandme…

thankyou.

EDIT : I tried doing this with intervals… but does not work well…
I know, this is the most complicated i was experimented in panda, the keys… cant find a fit example, was seeing roaming ralph, i think i have all scripted, but not run, not move, …

Well, I thought that using an interval would be the way to do it, by having the inteval run continually until you press the key to stop the character, which would then stop the task.

If you didnt’ want to use an interval, try just setting a variable when a is pressed and each frame check if the variable is still set. If d is pressed flip the variable to false (if you use a bool) and when it’s false, the player moves the other way.

Jilliger this is that i have:

Whe I press A the player runs around the interval correctly, but, if you press A again before interval finish intime, A will multiply the factor and time will duplicate, so the result player´s move will be more slow, that is a problem…

Aurilliance :

Try a variable is a good idea too, but i cant imagine how to do that if not using intervals, how is your idea?.

Thanks both!
[/img]

Here an example the problem with the keys…

Is very tiny weight example (2kb), just to explain.

savefile.com/files/2140709

If something is wrong (the most possible), and some of you fix it adding or editing the script, will be glad…

Read the manual section on key handling a little more carefully. There are events for detecting when keys are released, and iirc, for detecting when they’re held after being pressed.

The behavior you want, fundamentally, is ‘when key pressed, start moving, when key released, stop moving.’ You could probably do this by looping a relative position interval on keypress and stopping it on release, but you may run into some problems keeping the motion completely relative rather than just looping in absolute space. More likely, you’ll want to start a motion task on keypress and remove it on release, with the task incrementing position/rotation proportionally to time passed.

Well, i tried all ways with intervals and could not get… but i think going to change again to the traditional move system.

watched the manual, keyboard support section:

http://www.panda3d.org/manual/index.php/Keyboard_Support

Tried all that and not work, with “up” and “-repeat”, it wont move fast, instantly,… -repeat works… but does not response fast…

in the above file (2kb), is an example how is the problem, just keep pressed “a” or “d” and the cube will not move instantly…

talking with a friend he tolds me that i must to create a directory, cause that of the beginning line of this dict type:

self.keyMap = {"left":0, "right":0}

but, directory is in the p3d root one ? or mine… or is another way…
:unamused:

Your sample code is being blocked by my corporate firewall for the moment, but I have the hunch that you’re still going about this in a confused manner.

In the code block you quoted initially, every time you press a key, it will create and start a new interval that moves player1. i.e.
[you press ‘a’] -> Panda creates the ‘a’ event automatically -> ‘a’ is caught (because of your accept statement) and runs _izquierda -> _izquierda creates a new posInterval to move player1 to the absolute position (-70,y,z), from wherever it is (let’s assume (0,0,0), over the course of 5 seconds (incidentally, you can just start the posInterval, you don’t need to wrap it in the Sequence)
[you press ‘a’ 1 second later] -> Panda creates the ‘a’ event automatically -> ‘a’ is caught (because of your accept statement) and runs _izquierda -> _izquierda creates a new posInterval to move player1 to the absolute position (-70,y,z), from wherever it is (as a function of the interval that’s still going), over the course of 5 seconds, so now you have 2 intervals fighting over player1’s position
[you press ‘a’ 1 second later] -> … -> now you have 3 intervals going. Assuming a start position of (0,0,0), one is moving player1 from (0,0,0) to (-70,0,0), one is simultaneously trying to move it from (-14,0,0) to (-70,0,0), one is trying to move it from (-28,0,0) to (-70,0,0)

Is this the behavior you want?

I would assume you’re interested in
[you press ‘a’] -> stuff happens -> player1 starts moving in the -x direction
[you release ‘a’] -> stuff happens -> player1 stops moving in the -x direction

Intervals are a very awkward way to do this, but something on the order of

move = self.player1.posInterval(duration=5, pos=(-70,0,0), startPos=(0,0,0), other=self.player1)
self.accept('a', move.loop)
self.accept('a-up', move.stop)

might be a little closer to what you want. Note the ‘other’ argument, specifying that the coordinates are relative to self.player1’s corrdinate space rather than (by default) self.player1’s parent’s coordinate space.

Personally, I’d have ‘a’ do a taskMgr.add(moveTask) and ‘a-up’ do taskMgr.remove(moveTask) with moveTask containing some riff on

changeInTime = task.time - globalOldTime
globalOldTime=task.time
self.player1.setX(self.player1.getX()-changeInTime*speed)
return Task.cont

This method should work:

self.charMoving = 0

self.accept("escape", sys.exit())
self.accept("a", self.moving)
self.accept("d", self.stop)

def moving(self):
    self.charMoving = 1
    taskMgr.add(self.move,"moveTask")

def move(self, task):
    if self.charMoving == 1:
        #Place your moving interval here
        return Task.cont
    #Place your stop method here - interval.pause(), interval.stop(), posInterval() etc.
    return Task.done

def stop(self):
    self.charMoving = 0

Let me know if it does.

The fundamental limitation with intervals in this application, however, is that unless you really know what you’re doing, they’re either going to time out and stop before you’ve necessarily told them to, or they’re going to loop the model back to its starting position. This is why I personally suggest the explicit position setting if you’re still a little unsure about Panda in general.

That, and the code as written above will only handle motion in one direction. You’d need another set of intervals to move and stop in the other direction.

all right!!, i´m going to test both of yours methods!!

Jillinger, i tried the code, but seems to be that the finish of interval line is not well indented…

def _keyAssign(self):
		
	self.charMoving = 0 

	self.accept("escape", sys.exit()) 
	self.accept("a", self.moving) 
	self.accept("d", self.stop) 

	def moving(self): 
	    self.charMoving = 1 
	    taskMgr.add(self.move,"moveTask") 

	def move(self, task): 
	    if self.charMoving == 1:
			
	        leftPosInterval = self.player1.posInterval(3, Point3(-76, self.player1.getY(), self.player1.getZ()), startPos = Point3(self.player1.getPos()))
	        
                        leftMov = Sequence(leftPosInterval, name = "leftMov")
			leftMov.start()	
		
                     return Task.cont 
	     leftMov.finish()
	     return Task.done 

	def stop(self): 
	     self.charMoving = 0

Console Python claims for an indented line just the “leftMov.finish()”

Ninja : I tried the code, its move right, but when i press the key to go the opposite side, interval does not cut,…

Excuse me for many question, intervals are one method, for now i would like to know the other method, like roaming ralph, without intervals, no matter if this not keep moving, is okey, i like to do something like super mario bross 2D, is easiest way?, …

Is possible some of you guys will tell me, see the ralph example, but believe me, i read all the script, and could not get the same code work in my game… :frowning:

I will let you my incomplete game, is not too long… and you will see what is the real problem… dont wait is a great game :laughing: learning this key step i hope finish it.

http://rapidshare.com/files/250081546/RGS.rar.html

thank you a lot for the help…

If I read the original post properly, he wants the a key to move left, but keep moving left even after the key is released. The only thing that stops moving left is pressing d, which then makes the avatar start and continue to move right until the a is pressed again. I don’t think (correct me if I am wrong) that either of those examples will do so.

I think this is probably pretty close to what is described above. Note that I did add the ‘x’ key that will stop moving completely…



self.moveLeft = 0
self.moveRight = 0

self.accept("escape", sys.exit)
self.accept("a",self.movingLeft)
self.accept("d",self.movingRight)
self.accept("x",self.movingStop)


taskMgr.add(self.motion,"self.motion")


def movingLeft(self):
    self.moveLeft = 1
    self.moveRight = 0

def movingRight(self):
    self.moveRight = 1
    self.moveLeft = 0

def movingStop(self):
    self.moveRight = 0
    self.moveLeft = 0

def motion(self,task):

    self.elapsed = task.time - self.prevtime
    self.prevtime = task.time

    if (self.movingLeft):
        self.avatar.setX(self.avatar.getX()-(self.elapsed*self.speed))

    if (self.movingRight):
        self.avatar.setX(self.avatar.getX()+(self.elapsed*self.speed))

    return task.cont

Set self.speed to be a speed value. If it is left at 0, it won’t move at all.

Hope this helps!

hi tutankamon will try that too.
thanks…!

Then stop it manually. The interval shouldn’t care if it’s stopped twice, the only issue is that, e.g., if you press left (move left), press right (stop move left, move right) and then release right while having held left the whole time, you won’t start moving left again. Yet another way intervals aren’t going to be ideal, but I get the sense that’s the way you want to go, so you’re going to need to accept that you aren’t going to learn to run before you can walk.

To get two methods into a single trigger is a proper use of the Sequence class:

changeDirLeft = Sequence(Func(moveRight.stop), Func(moveLeft.loop))
self.accept('a',changeDirLeft.start)

And of course, you’re going to need to mirror everything and hook up your ‘d’ key the same way. And you should probably read the manual to make sure I’m using the proper methods for start/play/resume/loop/stop/finish/pause. I can never remember which set of terms applies to intervals vs. animations vs. sounds.

, now is working well, a combination of all yours post and a friend, did it…

i hope i will not forget this thing for the rest of my life :laughing:

thank you all!