Hello,

I'm using Java and Python together with Jython and I'm having trouble with some python syntax. I have a custom listener that I want to add to an object. Usually I'd do it like this:

class ballCollisionListener(animation.CollisionListener):
    def collisionDetected(self,event):
        if event.getSource()==boundary:
            if ball.getX()<=0 or ball.getX()+ball.getWidth()>=boundary.getWidth():
                ball.setXSpeed(-ball.getXSpeed())
            elif ball.getY()<=0 or ball.getY()+ball.getHeight()>=boundary.getHeight():
                ball.setYSpeed(-ball.getYSpeed())
        elif event.getSource()==paddle:
            ball.setYSpeed(-ball.getYSpeed())

ball.addCollisionListener(ballCollisionListener())

but I think I shouldn't have to create a new class everytime.

In java if I wanted to do this it would be something like:

ball.addCollisionListener(new CollisionListener()
{

}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

The problem with doing it the way I'm doing it is that I'm trying to instantiate lots of ball objects but the listener is only getting applied to the last one. I think it's because I'm redeclaring the class everytime.

for i in range(1,3): 
    ball = animation.AutoMoveItem(images.get("Ball"),(200+40*i),50,20,20,-5,5)
    print "Python: ",ball,ball.getX(),ball.getY()
    class ballCollisionListener(animation.CollisionListener):
        def collisionDetected(self,event):
            print "Python Collision: ",ball,ball.getX(),ball.getY()
            if event.getSource()==boundary:
                if ball.getX()<=0 or ball.getX()+ball.getWidth()>=boundary.getWidth():
                    ball.setXSpeed(-ball.getXSpeed())
                elif ball.getY()<=0 or ball.getY()+ball.getHeight()>=boundary.getHeight():
                    ball.setYSpeed(-ball.getYSpeed())
            elif event.getSource()==paddle:
                ball.setYSpeed(-ball.getYSpeed())
    ball.addCollisionListener(ballCollisionListener())
    game.addElement(ball)

I think it's because I'm redeclaring the class everytime.

Right.. so try something like this:

# My class
class ballCollisionListener(animation.CollisionListener):
    def collisionDetected(self,event):
        print "Python Collision: ",ball,ball.getX(),ball.getY()
        if event.getSource()==boundary:
            if ball.getX()<=0 or ball.getX()+ball.getWidth()>=boundary.getWidth():
                ball.setXSpeed(-ball.getXSpeed())
            elif ball.getY()<=0 or ball.getY()+ball.getHeight()>=boundary.getHeight():
                ball.setYSpeed(-ball.getYSpeed())
        elif event.getSource()==paddle:
            ball.setYSpeed(-ball.getYSpeed())


for i in range(1,3): 
    ball = animation.AutoMoveItem(images.get("Ball"),(200+40*i),50,20,20,-5,5)
    print "Python: ",ball,ball.getX(),ball.getY()
    my_ball_listener = ballCollisionListener()
    ball.addCollisionListener(my_ball_listener)
    game.addElement(ball)
This article has been dead for over six months. Start a new discussion instead.