brython and pixi.js need help

66 views
Skip to first unread message

Damien Polizzi

unread,
May 5, 2016, 5:40:30 PM5/5/16
to brython
Hello,
I try to program a game with brython and the js graphic library pixi.js and I need help in translating how to handle events.

I have this code written in javascript :

in the constructor of one JS object I wrote this. Image is a sprite into a canva  :

    function CaseJeu(x,y) {
        this.x=x;
        this.y=y;
        ...
       let image = new PIXI.Sprite(textureAssiette);
        image.anchor.x = 0;
        image.anchor.y = 0;
        image.interactive=false;
        image.hitArea=zoneCliquable;
        image.on('click',this.click.bind(this));
       ...
   
and after that I define a function for the event

CaseJeu.prototype.click = function(event){
    ....
}


I translated all in Brython and all is ok, but not the event and I don't know how to write it, I tried a few things but wihtout results

for example I whrote that
            image.on('click',self.click.bind);
whith the function :
    def click(self) :
        print("click")

and when I click I obtain the folowing error


TypeError: Function.prototype.bind called on incompatible null
Trace de la pile :
$B.pyobj2jsobj/<@https://cdn.rawgit.com/brython-dev/brython/3.2.6/www/src/brython.js:6617:8
[10]</n.prototype.emit@file:///home/damien/Dropbox/projets_info/towerdefense/brython/javascript/pixi.min.js:2:1514
[117]</i.prototype.dispatchEvent@file:///home/damien/Dropbox/projets_info/towerdefense/brython/javascript/pixi.min.js:9:13638
[117]</i.prototype.processMouseUp@file:///home/damien/Dropbox/projets_info/towerdefense/brython/javascript/pixi.min.js:9:15461
[117]</i.prototype.processInteractive@file:///home/damien/Dropbox/projets_info/towerdefense/brython/javascript/pixi.min.js:9:14397
[117]</i.prototype.processInteractive@file:///home/damien/Dropbox/projets_info/towerdefense/brython/javascript/pixi.min.js:9:14129
[117]</i.prototype.onMouseUp@file:///home/damien/Dropbox/projets_info/towerdefense/brython/javascript/pixi.min.js:9:15150

Could someone help me ?

Pierre Quentel

unread,
May 6, 2016, 2:33:51 AM5/6/16
to brython
You use a constructor in Javascript, so you can use a class in Brython.

The equivalent of

function CaseJeu(name) {
    this.name = name;
    var button = document.createElement('BUTTON');
    button.appendChild(document.createTextNode(name));
    document.body.appendChild(button);
    $(button).on('click', this.click.bind(this))
}

CaseJeu.prototype.click = function(event){
    console.log('click on', this.name)
}
new CaseJeu('coucou JS')


would be in Brython

from browser import document, html

class CaseJeu:

    def __init__(self, name):
        self.name = name;
        button = html.BUTTON(name)
        button.bind('click', self.click)
        document <= button

    def click(self, event):
        print('click on', self.name)

CaseJeu('coucou Brython')


Hope this helps

Damien Polizzi

unread,
May 8, 2016, 4:37:46 PM5/8/16
to brython
Hello Pierre

and thank you for your answer exuse me but I wil write it in french because you are french too.

Ce qui change entre le code que vous m'aviez donné réside dans le fait que lorsqu'on utilise un objet DOM on va utiliser "bind" mais dans PIXIJS on utilise des objets qui ne sont pas des objets DOPM mais des SPRITES localisés dans un CANVAS.
Donc le bind ne fonctionne pas dans ce cas et c'est bien on qu'il fallait utiliser. Par contre au niveau de la définition du contenu de la fonction il faut bien utiliser la forme que vous avez donné.
J'avais donc un message d'erreur m'indiquant :
 TypeError: self.js.addEventListener is not a function
Donc il ne pouvait trouver cette fonction non définie sur ce type d'objet.

En tout cas merci, et j'ai été flatté que le concepteur du langage me réponde !


Je repasse en anglais

Then I wrote this
image.on('click', self.click)



And my function :
def click(self,event) :
       
print("click")


Pierre Quentel

unread,
May 12, 2016, 4:53:25 AM5/12/16
to brython


Le dimanche 8 mai 2016 22:37:46 UTC+2, Damien Polizzi a écrit :
Hello Pierre
Bonjour Damien,
Est-ce que ce code fonctionne ou y a-t-il toujours une erreur ?

Damien Polizzi

unread,
May 12, 2016, 6:26:32 AM5/12/16
to brython

Tout est bon maintenant, et j'ai bien avancé sur mon projet,
merci !

Pierre Quentel

unread,
May 12, 2016, 7:18:24 AM5/12/16
to brython


Le jeudi 12 mai 2016 12:26:32 UTC+2, Damien Polizzi a écrit :

Tout est bon maintenant, et j'ai bien avancé sur mon projet,
merci !
nickel !
Reply all
Reply to author
Forward
0 new messages