2
\$\begingroup\$

I'll start off by saying I'm way off in how this should be done. After spending much time on getting this to be clean it wasn't going to happen so I just did it anyway I could.

I'm trying to make some FlxStates that will show off some nice menus that I want to create. What I'm doing in this code is moving UI buttons from off the screen to their final position.

What I tried to do in this class was take the button and place it in my own custom Class that would hold all the needed information so that it would be easy to read. Instead I needed to put everything in the MenuState2 because I needed the _btnMenu1 to have the callback call clickMenu1(). If I didn't create the button in MenuState2 then I couldn't use the callback.

MenuState2:

package;

import flixel.FlxG;
import flixel.FlxObject;
import flixel.FlxState;
import flixel.math.FlxPoint;
import flixel.text.FlxText;
import flixel.ui.FlxButton;

class MenuState2 extends FlxState
{
    var _organizer:ObjectOrganizer;

    private var _txtTitle:FlxText;

    //One button
    private var _btnMenu1:FlxButton;
    private var _btnMenu1FinalPosistion:FlxPoint;
    private var _btnMenu1Flag:Bool;


    override public function create():Void
    {
        _organizer = new ObjectOrganizer();

        _txtTitle = new FlxText(0,0, "Slide Right", 27);
        _organizer.positionObjectInCenter(_txtTitle);
        add(_txtTitle);

        _btnMenu1FinalPosistion = new FlxPoint(0,0);//Init FlxPoint
        _btnMenu1 = new FlxButton(0,0, "Main Menu", clickMenu1);//Init FlxButton
        _organizer.positionObjectInCenter(_btnMenu1);//Use helper class to layout buttons

        _btnMenu1FinalPosistion = _organizer.trackFinalPosition(_btnMenu1);//Find buttons final position
        _btnMenu1.x = 0 - _btnMenu1.width;//Move Button off screen
        _btnMenu1Flag = false;//Mark button dirty to allow update() to move it
        add(_btnMenu1);

        super.create();
    }

    override public function update(elapsed:Float):Void
    {
        if (_btnMenu1Flag == false) {//Check to see if we should move button
            _btnMenu1.x = _btnMenu1.x + (600 * elapsed);//Move button
            if (_btnMenu1.x >= _btnMenu1FinalPosistion.x) {//Check to see if we should stop moving button
                _btnMenu1.x = _btnMenu1FinalPosistion.x;//Correct for overshoot
                _btnMenu1Flag = true;//Button is clean
        }
    }
    super.update(elapsed);
    }

    private function clickMenu1():Void {
        FlxG.switchState(new MenuState1());
    }
}

ObjectOrganizer:

import flixel.FlxG;
import flixel.FlxObject;
import flixel.math.FlxPoint;

class ObjectOrganizer extends FlxG{
    var numberOfItems:Int = 1;
    var amountOfSpaceBetweenObjects:Float = 40;

    public function new() {

    }

    public function positionObjectInCenter(item:flixel.FlxObject):Void {
        var screenWidth = FlxG.width;
        //Check to see if designer has changed placement of object
        //Changed placement indicates a desire to place the object by hand and should be left alone
        if (item.x == 0) {
            item.x = (screenWidth / 2) - (item.width / 2);
        }
        if (item.y == 0) {
            item.y = numberOfItems * amountOfSpaceBetweenObjects;
        }
        if (item.x != 0 || item.y != 0) {
            numberOfItems ++;
        }
        // add(item);
    }

    public function trackFinalPosition(obj:FlxObject):FlxPoint {
        var finalPosition = new FlxPoint(0,0);
        finalPosition.x = obj.x;
        finalPosition.y = obj.y;
        return(finalPosition);
    }
}

I picked up HaxeFlixel because it was more programming centric and I to learn how to code this myself rather then have Unity or Game Maker do it for me.

\$\endgroup\$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.