Quantcast
Channel: Cocos Forums - Latest topics
Viewing all articles
Browse latest Browse all 17079

Dynamically create sprite on custom event

$
0
0

@MrPiponazo wrote:

Hi guys,

I'm developing a small multiplayer card game with cocos2d-x. Server is up and running, and I can kinda play with a text-based client. Now it's time for a graphical client. I have subclassed Layer in order to create my play scene, and in my init() I start a new thread for communicating with the server. On server message, a custom "server_msg" event is dispatched. All this stuff works, but I need to create a new sprite to represent a server message (namely a card played). My scene class have a public playCard() method, but it doesn't seem to work inside the event dispatcher (it does outside).

This is the code:

Match* Match::playCard() {
    myCard = Sprite::create("card.png"); // myCard is a public Sprite* member of Match
    myCard->setPosition(Vec2(240, 66));
    myZone->addChild(myCard); // myZone is a public Zone* member of Match. Zone is a subclass of LayerColor
    return this;
}

bool Match::init() {
    if (!Layer::init()) return false;
    // ...
    myZone = Zone::create();
    this->addChild(myZone);
    // If I issue a this->playCard() here, it works
    // ...
    auto serverListener = EventListenerCustom::create("server_msg", [&](EventCustom* event) {
        // ...
        if (cmdCode == CARD_PLAYED) {
            this->playCard(); // This doesn't work
            // But if playCard() is called outside the listener, I can manipulate (i.e scale) the sprite here
        }
    }
    this->_eventDispatcher->addEventListenerWithFixedPriority(serverListener, 1);
    // ...
}

Any help?

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 17079

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>