Making a custom Event

973 views
Skip to first unread message

sakanac...@gmail.com

unread,
Jul 17, 2013, 8:31:32 PM7/17/13
to kivy-...@googlegroups.com
I'm a bit confused on how to make a custom event.

To put it shortly I want to make an event for a class i made.
I'm putting instances of the class in a Gridlayout.
The class is basically a representation of bricks. Each instance is a brick.
I want a collision between my character and the bricks to cause the bricks to break.

But it's impossible to directly access each and every child widget(brick) inside the GridLayout.

So I figured that if I make an event. Then I can bind it to my "brick breaking" function.
I could apply this rule to every instance using KV language,

But I have no idea about how to make events.....
Things like EventDispatcher confuse me

Thanks for any help

ZenCODE

unread,
Jul 18, 2013, 2:38:38 AM7/18/13
to kivy-...@googlegroups.com
Hi there

Try looking here?

http://kivy.org/docs/api-kivy.event.html

See the "register_event_type()" section. Then use the "dispatch()" to trigger it....

Hope that helps?

Peace out

mathieu....@gmail.com

unread,
Jul 18, 2013, 7:23:24 AM7/18/13
to kivy-...@googlegroups.com
I think I had the same problem wich I resolve this way.

First a little explanation of what i had and what i did.

I had several custom widgets into cells of a GridLayout, and bind the 'on_touch_down' to those widgets.
When I touch one the the widget, the event is fired x times ( x = number of instances of the widget ).
I expected the event to be sent only to the widget I touch.

So to get the correct widget at the 'on_touch_down' method, I used
    if self.collide_point ( touch.x, touch.y) :
            #filter to the touchded instance#
            my code. .......

I think your doing a "ball breaker" game ? Maybe you have a clock to move the ball like in the Pong tutorial ? if so, maybe you can check the collision at the ball moving function ?
In the widget class you have a collide_widget method explained here :
http://kivy.org/docs/api-kivy.uix.widget.html?highlight=collide_widget#kivy.uix.widget.Widget.collide_widget

I hope I understand what you where asking for, I post my experience here to get this answer :
why, in my case, the on_touch_down event is call as many times as instances of my widget ?

cheers.
Mathieu

sakanac...@gmail.com

unread,
Jul 18, 2013, 6:45:33 PM7/18/13
to kivy-...@googlegroups.com
Thank you, it worked!

I had the movement going with a clock, so I also decided to also let it check for collisions in my "Update" function
Works perfectly now thanks

I had it typed out like this:

 def update(self,dt):
        self.char.move()
        if self.theX==1:
            self.theY=self.theY-1
            
        if self.theY==0:
            self.char.jirachi= "nin8new.png"
            self.char.width= 100
        
        for child in self.brickHolder.children[:]:
            if child.collide_widget(self.char):
                self.brickBreak()
                print "colli made"

sakanac...@gmail.com

unread,
Jul 18, 2013, 6:55:22 PM7/18/13
to kivy-...@googlegroups.com
One more question

Now that I've got it to collide and break the bricks

is it possible to put this in the form of an Event?


      for child in self.brickHolder.children[:]:
            if child.collide_widget(self.char):
                self.brickBreak()
                print "colli made"

Like, in my brickHolder class

    class brickHolder

    self.register_event_type('on_contact')

    def on_contact(self):
        -When an object touches self......self.dispatch()
    

    .......later on in program

    if Event dispatched= on_contact........ self.brickBreak()

Mathieu Virbel

unread,
Jul 19, 2013, 5:18:22 AM7/19/13
to kivy-...@googlegroups.com
Hi,

All the widgets are based in top of EventDispatcher.

So you can "declare" events like this:

class MyCustomWidget(GridLayout):
    __events__ = ('on_brick_breacked', )
    def on_brick_breacked(self, instance, *args):
        pass

Then, you just need to call "self.dispatch('on_brick_breacked', ...)" somewhere in your widget :)

Mathieu

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages