简体   繁体   中英

libgdx InputListener: Detecting when an actor has been touched from another class

I have just begun working with LibGDX, in order to create a card game, but am struggling with the input. Currently, my program has several classes: the game, which creates the various players and controls the turn order etc. This is of course where the main create() and render() methods are called from.

There is also the player class, as well as a card class, and classes for the deck, hand and area of play.

The player class is responsible for moving the cards between the various card stacks (the deck, hand and playing area). The card class is the actor class for the various cards in the game. This is where the input listener is created.

The problem I am having is that when the card input listener detects any input for that particular card (say if that card is clicked on by the user), then I cannot seem to find a way for the player class to then register that input, and act on the card accordingly. How can i accomplish letting the player class know when the card class has registered some input from the user?

A common problem in games is everything needs to know about everything. You could keep references to each other object within every object but that means your code gets very tightly coupled and inflexible. A better way this can be solved is with a messaging system. You can look into integrating gdx-ai into your project which has a messaging system: https://github.com/libgdx/gdx-ai

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM