admin管理员组

文章数量:1122826

I wanted to do a cool transition effect with random stickers filling your screen and then peeling off one by one but whenever I switch states the substate closes despite being told not to. It's just like switching states forcefully kills any substates that are currently open. this is how the substate's code looks like:

package;

import flixel.addons.transition.FlxTransitionableState;
import flixel.util.FlxTimer;
import flixel.FlxState;
import flixel.FlxSprite;
import flixel.FlxG;
import flixel.FlxCamera;
import flixel.group.FlxGroup;

class StickerTransition extends MusicBeatSubstate
{
    var numStickers:Int;
    var stickerx:Float = -100;
    var stickery:Float = -100;

    var stickerArray:Array<FlxSprite> = [];
    var curStickerNum:Int = 0;

    public static var shouldStick:String = '';

    var speeee = 100;
    var timer:FlxTimer;

    var nextTarget:FlxState;
    var stickerCamera:FlxCamera;
    public static var switchingState:Bool = false;

    public function new(target:FlxState)
    {
        super();
        resetVariables();
        nextTarget = target;

        stickerCamera = new FlxCamera(0, 0, FlxG.width, FlxG.height);
        stickerCamera.bgColor.alpha = 0;
        FlxG.cameras.add(stickerCamera, false);

        numStickers = Std.parseInt(Paths.getTextFromFile('images/stickers/howmuch.txt', false));

        while(stickery <= FlxG.height)
        {
            addSticker(FlxG.random.int(1, numStickers), stickerx, stickery);
        }
        FlxG.random.shuffle(stickerArray);
        shouldStick = 'yeah';

    }

    override function create()
        {
            if (timer != null) timer.cancel(); // Cancel existing timer
            timer = new FlxTimer(); // Assign new timer
            timer.start(1 / speeee + FlxG.random.float(-0.005, 0.005), function(tmr:FlxTimer) {
                timerTick();
            }, 0);
        }

    function resetVariables()
        {
            curStickerNum = 0;
            shouldStick = '';
            stickerx = -100;
            stickery = -100;
            stickerArray = [];
            switchingState = false;
        }

    public function stateLoaded()
    {
        shouldStick = 'nah';
        switchingState = false;
    }

    function showSticker()
    {
        FlxG.sound.play(Paths.sound('sticker/'+FlxG.random.int(1, 8)));
        add(stickerArray[curStickerNum]);
        curStickerNum++;
        if (curStickerNum == stickerArray.length)
        {
            shouldStick = '';
            if(nextTarget != null)
            {
                FlxTransitionableState.skipNextTransIn = true;
                FlxTransitionableState.skipNextTransOut = true;
                switchingState = true;
                FlxG.switchState(nextTarget);
            }
        }
    }
    function removeSticker()
    {
        FlxG.sound.play(Paths.sound('sticker/'+FlxG.random.int(1, 8)));
        remove(stickerArray[curStickerNum]);
        curStickerNum--;
        if(curStickerNum < 0)
        {
            shouldStick = '';
            close();
        }
    }

    function timerTick()
    {
        if (shouldStick == 'yeah')
        {
            showSticker();
        }
        if (shouldStick == 'nah')
        {
            removeSticker();
        }
    }


    function addSticker(num:Int, x:Float, y:Float)
    {
        var stickeridk:FlxSprite = new FlxSprite(x, y);
        stickeridk.loadGraphic(Paths.image('stickers/'+num));
        stickeridk.angle = FlxG.random.int(-45, 45);
        var randscale:Float = FlxG.random.float(0.85, 1.1);
        stickeridk.scale.x = randscale;
        stickeridk.scale.y = randscale;
        stickeridk.cameras = [stickerCamera];
        stickeridk.antialiasing = ClientPrefs.globalAntialiasing;
        stickeridk.updateHitbox();

        //add(stickeridk);

        stickerx += stickeridk.width / 2;
        if (stickerx >= FlxG.width - 50)
        {
            stickerx = -100;
            stickery += stickeridk.height / 2;
        }

        stickerArray.push(stickeridk);
        //trace('made sticker');
    }

    override public function destroy()
        {
            if(switchingState) return;
            super.destroy();
            trace('destroyed.');
        }

    override public function close()
    {
        if(switchingState) return;
        super.close();
        resetVariables();
        FlxG.cameras.remove(stickerCamera);
        stickerCamera.destroy();
        stickerCamera = null;
        trace('closed.');
    }
}

I tried to return the close and destroy functions, but to no use.

本文标签: haxeflixelFlxSubState force closing on state switchStack Overflow