Jump to content

PIXI.Container events


loryruta
 Share

Recommended Posts

Hello all, I'm having a weird issue that I can't figure out, I've also compared my code with the PIXI's examples code but it's still not working.

Basically I have a class called Bag that extends PIXI.Container that is made up of a Sprite (texture, that can be "opened" or "closed"), and a text telling how many cards there are inside.
The Bag can be hovered or clicked by the cursor and do something based on those events.
So I register all the needed events directly on the Bag (that is a PIXI.Container), but none of the is called!

import * as PIXI from "pixi.js";
import {app} from "../index";
import Texture = PIXI.Texture;

export class Bag extends PIXI.Container {
    cards: Array<Card>;

    sprite: PIXI.Sprite;
    text: PIXI.Text;

    static BAG_OPENED: Texture;
    static BAG_CLOSED: Texture;

    static fetchResources() {
        const resources = PIXI.Loader.shared.resources;
        let spritesheet;

        spritesheet = resources["bag"].spritesheet;
        Bag.BAG_OPENED = spritesheet.textures["bag_opened.png"];
        Bag.BAG_CLOSED = spritesheet.textures["bag_closed.png"];
    }

    constructor(cards: Array<Card>) {
        super();
        this.cards = cards;

        Bag.fetchResources();

        this.sprite = new PIXI.Sprite(Bag.BAG_CLOSED);

        this.text = new PIXI.Text(
            this.cards.length.toString(),
            new PIXI.TextStyle({
                fill: 0xffffff,
            })
        );
        this.text.y = this.sprite.height;

        this.addChild(this.sprite);
        this.addChild(this.text);

        this.interactive = true;
        this.buttonMode = true;
    }

    listen() {
        this.on("pointerdown", function () {
            console.log("Pointer down!"); // NOT CALLED! <----------------------
        });
    }

    unlisten() {
        console.log("[Bag] Unlistening...");
    }

    static fromModality(modality: string) {
        const resource = PIXI.Loader.shared.resources["modalities/" + modality];
        return new Bag(resource.data);
    }
}

I instantiate the Bag and listen() in another part of code:

// init the bag
this.bag = Bag.fromModality("classical");

// add the bag to a newly app.stage and listen for events
app.stage = new PIXI.Container();
app.stage.addChild(this.bag);
this.bag.listen();

What I learned from debugging is:
- listen() is reached
- the event "pointermove" works but it's not relative to the PIXI.Container
- the bag is successfully added to the stage since I can clearly view it in the screen

Where am I wrong? ?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...