1

在 GMS2.x 中,使用如下代码关闭 UIFrame 窗口会导致 DM 崩溃(按下关闭按钮时)。

但是,相同的代码在 GMS 1.x 中也能正常工作。

有没有办法在 GMS 2.x 中解决这个问题?

class UIWindowCloseTest : UIFrame {

    void CloseSelf( object self ) self.GetFrameWindow().WindowClose(0);

    UIWindowCloseTest( object self ) {
        TagGroup tgDialog = DLGCreateDialog( "window close test" );
        tgDialog.DLGAddElement( DLGCreatePushButton( "Close", "CloseSelf" ));
        self.super.init(tgDialog);
        self.Display( "test" );
        result( self.ScriptObjectGetID().Hex() + " constructed\n" );
    };

    ~UIWindowCloseTest( object self ) \
        result( self.ScriptObjectGetID().Hex() + " destructed\n\n" );
};

alloc(UIWindowCloseTest);
4

2 回答 2

1

是的,在 GMS 2.x 中你必须使用

self.close();

代替

self.GetFrameWindow().WindowClose(0);

于 2015-01-22T17:28:58.610 回答
0

这是 GMS 3.X 这个问题的扩展:

从本质上讲,下面的答案对于 GMS 3 也是正确的,但仅限于其 3.2 版(也许 GMS 3.1.2 也是如此)。

正如 KEVIVI 在对答案的评论中指出的那样,早期版本的 GMS 3 有一个错误。

但是,有一个变通的解决方案,它稍微复杂:

Class myDLG : UIframe
{
    myDLG(object self)  result("\n Create DLG")
    ~myDLG(object self) result("\n Kill DLG")

    void DeferredClose( object self )
    {
        TagGroup tgs = GetPersistentTagGroup()
        number scriptID
        if ( tgs.TagGroupGetTagAsLong( "DummyTag_CloseWindow_ID", scriptID ) )
        {
            object obj = GetScriptObjectFromID( scriptID )
            if ( obj.ScriptObjectIsValid() )
            {
                obj.GetFrameWindow().WindowClose(0)
                return
            }
        }
        Debug( "\n Sorry, but could not close dialog." )
    }

    void CloseButtonAction( object self )    
    {
        // Normally, it would be save to use "self.close()" here,
        // but due to a bug, this is currenlty not possible in GMS 3.1
        // The alternative method of getting the window of the UIframe object
        // and closing it, is okay, but it must not be called directly here,
        // or it will crash DM.
        // As a work-around, one can store the object ID and have a separate
        // thread pick it up, get the object, and close the object's window.
        // This is, what we are doing below.


        // Write ScriptID into tags 
        TagGroup tgs = GetPersistentTagGroup()
        tgs.TagGroupSetTagAsLong( "DummyTag_CloseWindow_ID", self.ScriptObjectGetID() ) 

        // Launch separate thread just to close... (0.1 sec delay for safety)
        AddMainThreadSingleTask( self, "DeferredClose", 0.1 )
    }

    TagGroup CreateDLG(object self)
    {
        TagGroup DLGtg,DLGtgItems
        DLGtg=DLGCreateDialog("my Dialog",DLGtgItems)
        DLGtgItems.DLGAddElement(DLGCreatePushButton("Close","CloseButtonAction"))
        return DLGtg
    }
}

{
    object dialog=Alloc(myDLG)
    dialog.Init( dialog.CreateDLG() )
    dialog.display("")
}

所以:

  • 对于 GMS 3.2 及更高版本:使用self.close();

  • 对于 GMS 3.0 和 3.1(带有错误):使用解决方法。

  • 对于 GMS 2.x:使用self.close();

  • 对于 GMS 1.x:使用self.GetFrameWindow().WindowClose(0);

于 2017-01-31T10:48:41.067 回答