HaxeFoundation / haxe

Haxe - The Cross-Platform Toolkit
https://haxe.org
6.21k stars 657 forks source link

Update(elapsed:Float) returns a null object reference exception #10900

Closed itsoutchy-projects closed 1 year ago

itsoutchy-projects commented 1 year ago

I get a null object reference exception when I hover over an FlxButton, a crash handler tells me that it's due to super.Update()'s elapsed parameter is null.

Code: `package;

if desktop

import Discord.DiscordClient;

end

import flixel.FlxG; import flixel.FlxObject; import flixel.FlxSprite; import flixel.FlxCamera; import flixel.addons.transition.FlxTransitionableState; import flixel.effects.FlxFlicker; import flixel.graphics.frames.FlxAtlasFrames; import flixel.group.FlxGroup.FlxTypedGroup; import flixel.text.FlxText; import flixel.math.FlxMath; import flixel.tweens.FlxEase; import flixel.tweens.FlxTween; import flixel.util.FlxColor; import lime.app.Application; import Achievements; import editors.MasterEditorMenu; import flixel.input.keyboard.FlxKey; import flixel.ui.FlxButton; import flixel.math.FlxPoint;

using StringTools;

class MainMenuState extends MusicBeatState { public static var psychEngineVersion:String = '0.6.2'; //This is also used for Discord RPC public static var curSelected:Int = 0;

var menuItems:FlxTypedGroup<flixel.ui.FlxButton>;
private var camGame:FlxCamera;
private var camAchievement:FlxCamera;

var optionShit:Array<String> = [
    'story_mode',
    'freeplay',
    'credits',
    'options'
];

var magenta:FlxSprite;
var camFollow:FlxObject;
var camFollowPos:FlxObject;

var debugKeys:Array<FlxKey>;

// var gfDance:FlxSprite;      //to put the gf on the menu mme

// var danceLeft:Bool = false;

// //Gf Main Menu

// gfDance = new FlxSprite(FlxG.width * 0.4, FlxG.height * 0.07);

// gfDance.frames = Paths.getSparrowAtlas('gfDanceTitle');

// gfDance.animation.addByIndices('danceLeft', 'gfDance', [30, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29], "", 24, true);

// add(gfDance);

// if(gfDance != null) {

//  danceLeft = !danceLeft;

//  if (danceLeft) {
//      gfDance.animation.play('danceLeft');
//  }
// }
function scaleButton(button:FlxButton, width:Int, height:Int) {
    button.scale.x = width;
    button.width = width;
    button.height = height;
    button.scale.y = height;
    button.label.fieldWidth *= width + height;
    //button.label.y = button.y + 500;
    //button.label.x = button.x - 500;
    button.labelOffsets = [FlxPoint.get(button.x - 100, button.y + 100)];
    button.label.size = 25;
    button.centerOffsets(true);
    button.updateHitbox();
}

override function create()
{
    #if MODS_ALLOWED
    Paths.pushGlobalMods();
    #end
    WeekData.loadTheFirstEnabledMod();

    #if desktop
    // Updating Discord Rich Presence
    DiscordClient.changePresence("In the Menus", null);
    #end
    debugKeys = ClientPrefs.copyKey(ClientPrefs.keyBinds.get('debug_1'));

    camGame = new FlxCamera();
    camAchievement = new FlxCamera();
    camAchievement.bgColor.alpha = 0;

    FlxG.cameras.reset(camGame);
    FlxG.cameras.add(camAchievement, false);
    FlxG.cameras.setDefaultDrawTarget(camGame, true);

    transIn = FlxTransitionableState.defaultTransIn;
    transOut = FlxTransitionableState.defaultTransOut;

    persistentUpdate = persistentDraw = true;

    var yScroll:Float = Math.max(0.25 - (0.05 * (optionShit.length - 4)), 0.1);
    var bg:FlxSprite = new FlxSprite(-80).loadGraphic(Paths.image('menuBG'));
    bg.scrollFactor.set(0, 0);
    bg.setGraphicSize(Std.int(bg.width * 1.175));
    bg.updateHitbox();
    bg.screenCenter();
    bg.antialiasing = ClientPrefs.globalAntialiasing;
    add(bg);

    camFollow = new FlxObject(0, 0, 1, 1);
    camFollowPos = new FlxObject(0, 0, 1, 1);
    add(camFollow);
    add(camFollowPos);

    magenta = new FlxSprite(-80).loadGraphic(Paths.image('menuDesat'));
    magenta.scrollFactor.set(0, yScroll);
    magenta.setGraphicSize(Std.int(magenta.width * 1.175));
    magenta.updateHitbox();
    magenta.screenCenter();
    magenta.visible = false;
    magenta.antialiasing = ClientPrefs.globalAntialiasing;
    magenta.color = 0xFFfd719b;
    add(magenta);

    // magenta.scrollFactor.set();

    menuItems = new FlxTypedGroup<flixel.ui.FlxButton>();
    add(menuItems);

    var scale:Float = 1;
    /*if(optionShit.length > 6) {
        scale = 6 / optionShit.length;
    }*/
    var storyBttn:FlxButton = new FlxButton(100, 68, "Story Mode", function() {
        MusicBeatState.switchState(new StoryMenuState());
    });
    scaleButton(storyBttn, 2, 3);
    //storyBttn.loadGraphic("assets/images/mainmenu/menu_story_mode.png");
    add(storyBttn);

    var freeplayBttn:FlxButton = new FlxButton(100, 146, "Freeplay", function() {
        MusicBeatState.switchState(new FreeplayState());
    });
    scaleButton(freeplayBttn, 2, 3);
    //freeplayBttn.loadGraphic("assets/images/mainmenu/menu_freeplay.png");
    add(freeplayBttn);

    var creditsBttn:FlxButton = new FlxButton(100, 224, "Credits", function() {
        MusicBeatState.switchState(new CreditsState());
    });
    scaleButton(creditsBttn, 2, 3);
    //creditsBttn.loadGraphic("assets/images/mainmenu/menu_credits.png");
    add(creditsBttn);

    var optionsBttn:FlxButton = new FlxButton(100, 302, "Options", function() {
        LoadingState.loadAndSwitchState(new options.OptionsState());
    });
    scaleButton(optionsBttn, 2, 3);
    //optionsBttn.loadGraphic("assets/images/mainmenu/menu_options.png");
    add(optionsBttn);

    // for (i in 0...optionShit.length)
    // {
    //  var offset:Float = 108 - (Math.max(optionShit.length, 4) - 4) * 80;
    //  var menuItem:FlxButton = new FlxButton(100, (i * 140 + offset), optionShit[i], function() {
    //      var daChoice:String = optionShit[menuItem.ID];

    //          switch (daChoice)
    //          {
    //              case 'story_mode':
    //                  MusicBeatState.switchState(new StoryMenuState());
    //              case 'freeplay':
    //                  MusicBeatState.switchState(new FreeplayState());
    //              #if MODS_ALLOWED
    //              case 'mods':
    //                  MusicBeatState.switchState(new ModsMenuState());
    //              #end
    //              case 'awards':
    //                  MusicBeatState.switchState(new AchievementsMenuState());
    //              case 'credits':
    //                  MusicBeatState.switchState(new CreditsState());
    //              case 'options':
    //                  LoadingState.loadAndSwitchState(new options.OptionsState());
    //              }
    //  });
    //  menuItems.add(menuItem);
    //  var scr:Float = (optionShit.length - 4) * 0.135;
    //  if(optionShit.length < 6) scr = 0;
        //menuItem.setGraphicSize(Std.int(menuItem.width * 0.58));
    //}

    FlxG.camera.follow(camFollowPos, null, 1);

    // char1 = new Character(500, -130, 'bf2', true);
    // char1.setGraphicSize(Std.int(char1.width * 0.8 / 2));
    // add(char1);
    // char1.visible = true;
    // char1.dance();

    var versionShit:FlxText = new FlxText(12, FlxG.height - 44, 0, "Psych Engine v" + psychEngineVersion, 12);
    versionShit.scrollFactor.set();
    versionShit.setFormat("VCR OSD Mono", 16, FlxColor.WHITE, LEFT, FlxTextBorderStyle.OUTLINE, FlxColor.BLACK);
    add(versionShit);
    var versionShit:FlxText = new FlxText(12, FlxG.height - 24, 0, "Friday Night Funkin' v" + Application.current.meta.get('version'), 12);
    versionShit.scrollFactor.set();
    versionShit.setFormat("VCR OSD Mono", 16, FlxColor.WHITE, LEFT, FlxTextBorderStyle.OUTLINE, FlxColor.BLACK);
    add(versionShit);

    // NG.core.calls.event.logEvent('swag').send();

    changeItem();

    #if ACHIEVEMENTS_ALLOWED
    Achievements.loadAchievements();
    var leDate = Date.now();
    if (leDate.getDay() == 5 && leDate.getHours() >= 18) {
        var achieveID:Int = Achievements.getAchievementIndex('friday_night_play');
        if(!Achievements.isAchievementUnlocked(Achievements.achievementsStuff[achieveID][2])) { //It's a friday night. WEEEEEEEEEEEEEEEEEE
            Achievements.achievementsMap.set(Achievements.achievementsStuff[achieveID][2], true);
            giveAchievement();
            ClientPrefs.saveSettings();
        }
    }
    #end

    super.create();
}

#if ACHIEVEMENTS_ALLOWED
// Unlocks "Freaky on a Friday Night" achievement
function giveAchievement() {
    add(new AchievementObject('friday_night_play', camAchievement));
    FlxG.sound.play(Paths.sound('confirmMenu'), 0.7);
    trace('Giving achievement "friday_night_play"');
}
#end

var selectedSomethin:Bool = false;

override function update(elapsed:Float)
{
    if (FlxG.sound.music.volume < 0.8)
    {
        FlxG.sound.music.volume += 0.5 * FlxG.elapsed;
        if(FreeplayState.vocals != null) FreeplayState.vocals.volume += 0.5 * elapsed;
    }

    var lerpVal:Float = CoolUtil.boundTo(elapsed * 7.5, 0, 1);
    camFollowPos.setPosition(FlxMath.lerp(camFollowPos.x, camFollow.x, lerpVal), FlxMath.lerp(camFollowPos.y, camFollow.y, lerpVal));

    if (!selectedSomethin)
    {
        // menuItems.forEach(function(b:FlxButton) {
        //  if (b.HIGHLIGHT < 0) {
        //      curSelected = b.ID;
        //  }
        // });

        if (controls.BACK)
        {
            selectedSomethin = true;
            MusicBeatState.switchState(new TitleState());
        }

        if (controls.ACCEPT)
        {
            if (optionShit[curSelected] == 'donate')
            {
                CoolUtil.browserLoad('https://ninja-muffin24.itch.io/funkin');
            }
            else
            {
                selectedSomethin = true;
                FlxG.sound.play(Paths.sound('confirmMenu'));

                if(ClientPrefs.flashing) FlxFlicker.flicker(magenta, 1.1, 0.15, false);

                menuItems.forEach(function(spr:FlxButton)
                {
                    // if (curSelected != spr.ID)
                    // {
                    //  FlxTween.tween(spr, {alpha: 0}, 0.4, {
                    //      ease: FlxEase.quadOut,
                    //      onComplete: function(twn:FlxTween)
                    //      {
                    //          spr.kill();
                    //      }
                    //  });
                    // }
                    // else
                    // {
                    //  FlxFlicker.flicker(spr, 1, 0.06, false, false, function(flick:FlxFlicker)
                    //  {
                    //      var daChoice:String = optionShit[curSelected];

                    //      switch (daChoice)
                    //      {
                    //          case 'story_mode':
                    //              MusicBeatState.switchState(new StoryMenuState());
                    //          case 'freeplay':
                    //              MusicBeatState.switchState(new FreeplayState());
                    //          #if MODS_ALLOWED
                    //          case 'mods':
                    //              MusicBeatState.switchState(new ModsMenuState());
                    //          #end
                    //          case 'awards':
                    //              MusicBeatState.switchState(new AchievementsMenuState());
                    //          case 'credits':
                    //              MusicBeatState.switchState(new CreditsState());
                    //          case 'options':
                    //              LoadingState.loadAndSwitchState(new options.OptionsState());
                    //      }
                    //  });
                    // }
                });
            }
        }
        #if desktop
        else if (FlxG.keys.anyJustPressed(debugKeys))
        {
            selectedSomethin = true;
            MusicBeatState.switchState(new MasterEditorMenu());
        }
        #end
    }

    super.update(elapsed);
}

function changeItem(huh:Int = 0)
{
    curSelected += huh;

    if (curSelected >= menuItems.length)
        curSelected = 0;
    if (curSelected < 0)
        curSelected = menuItems.length - 1;
}

}`

kLabz commented 1 year ago

This is not a Haxe issue, please check with HaxeFlixel community (on their repository https://github.com/HaxeFlixel/flixel/ or discord server https://discordapp.com/invite/rqEBAgF, channel #flixel)

Or even on https://github.com/FunkinCrew/Funkin