Skip to content

Nested FlxGroups call update() functions in the wrong order #35

Open
@moly

Description

@moly

In an ideal world, Flixel should call preUpdate() on all game objects, then update(), then postUpdate().

When FlxGroups are nested, this is not the case. The inner FlxGroup will call preUpdate()/update()/postUpdate() during the outer FlxGroup's update() function.

For me, this behavior causes subtle off-by-one-frame errors where objects seem to lag one frame behind.

Solution: Explicitly call preUpdate() and postUpdate() at the FlxGame level, and have those calls trickle down the FlxGroups.

// in FlxGame.as
protected function update():void
        {           
            var mark:uint = getTimer();

            //FlxG.elapsed = FlxG.timeScale*(_step/1000);
            FlxG.updateSounds();
            FlxG.updatePlugins();
            _state.preUpdate();
            _state.update();
            _state.postUpdate();
            FlxG.updateCameras();

            if(_debuggerUp)
                _debugger.perf.flixelUpdate(getTimer()-mark);
        }

// in FlxGroup.as
override public function update():void
        {
            var basic:FlxBasic;
            var i:uint = 0;
            while(i < length)
            {
                basic = members[i++] as FlxBasic;
                if((basic != null) && basic.exists && basic.active)
                {
                    basic.update();
                }
            }
        }
override public function preUpdate():void
        {
            var basic:FlxBasic;
            var i:uint = 0;
            while(i < length)
            {
                basic = members[i++] as FlxBasic;
                if((basic != null) && basic.exists && basic.active)
                {
                    basic.preUpdate();
                }
            }
        }
override public function postUpdate():void
        {
            var basic:FlxBasic;
            var i:uint = 0;
            while(i < length)
            {
                basic = members[i++] as FlxBasic;
                if((basic != null) && basic.exists && basic.active)
                {
                    basic.postUpdate();
                }
            }
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions