Entities in FlxTypedGroup are not being drawn to screen



  • EDITED: typos and grammar

    I am currently working on a sports game made up of teams with players inside the team.

    The PlayState has a private TypedGroup<Team>, that then creates the team instances based on a predefined number.

    The new() function for the Team class has a private FlxTypedGroup<Player> that creates players based on a predefined number.

    Those players are just extended FlxSprites

    I know the players are being added to the team entity by checking the length value, which matches up to the predefined number.

    However they are not being drawn to the screen. The draw() functions inside the player entity should take care of that (or so I thought??), or do I have to create a draw function higher up in the chain (either in the Team entity or Playstate)

    If I add a single Player entity (outside a team) to the PlayState, that draws to the screen no problems.

    And also, does my team entity really need to extend any other Flx class? I got an error a couple of times when debugging saying it should extend FlxBasic?

    I know it is a long winded question but happy to clarify further if needed.

    Thanks in advance!



  • Did you add your FlxTypedGroup<Player> to the state?



  • @DleanJeans Yep, I am on a different machine now, so I can't share code at the moment but the flow is:

    1. PlayState creates Team
    2. Team creates players
    3. Iterate until all players and teams created
    4. Teams added to the Playstate


  • From your list you only add teams to state. I think you miss adding players to teams as well?



  • @starry-abyss @DleanJeans ok finally managed to get my hands on my dev machine

    Here is the code in the playstate.create() function:

    _teams = new FlxTypedGroup<Team>();
    
    trace(_numOfTeams);
    for(x in 0..._numOfTeams) {
        trace('making a team');
        _teams.add( new Team() );
    }
    add(_teams);
    trace("teams variable now: ", _teams);
    

    and the Team.new() function:

        public function new() {
            // this class doesn't inherit anything so no super
            super();
    
            _grpPlayers = new FlxTypedGroup<Player>();
            for(x in 0..._teamSize) {
                trace('making a player');
    		    _grpPlayers.add( new Player(1 + (x*2),120) );
            }
            trace(_grpPlayers.length); 
        }
    

    any help is appreciated!



  • What type is a Team?
    If it's a FlxGroup or something like that, you can add _grpPlayers to it.



  • @DleanJeans I have done this:

    class Team extends FlxBasic {
    

    this is because I wasn't too sure if I needed to extend any other class or not. Basically the team is just a container for a bunch of players so it is easy to reference.

    But thanks to your post, I just am thinking there is probably no draw function inside FlxBasic - maybe that is why it is not being drawn?

    What is the best way to go with the Team object - should it extend anything at all since it is basically a container?


  • administrators

    Wouldn't it be more logical if Team extends FlxTypedGroup<Player>, instead of having a variable with that type inside of it?

    Anyway, it looks like you're only only adding objects to _grpPlayers, but you're not adding _grpPlayers itself, so its members can't be drawn or updated.



  • @Gama11 yes, you are completely right, that does make more sense and worked exactly how I was hoping it too! Thanks so much for all your help!


Log in to reply