Java - method.Invoke仅适用于一个类

时间:2014-10-10 05:54:30

标签: java methods invoke

一旦我尝试创建一个我想要模块化的Java应用程序,我就遇到了问题。我希望有一个实现的事件系统,但是,我想要调用的事件只从整个系统启动的文件中调用。我主要尝试了两件事:在父类上调用方法,并通过从类中获取方法来调用方法。这是代码:

    static HashMap< String, ArrayList< Method > > GameEventDatabase;
static HashMap< String, ArrayList< String > > GameEventDatabaseT;
static HashMap< String, ArrayList< String > > GameEventClasses;
public static void CreateDatabase() {
    EventHandler.GameEventDatabase = new HashMap< String, ArrayList< Method > >();
    EventHandler.GameEventDatabaseT = new HashMap< String, ArrayList< String > >();
    EventHandler.GameEventClasses = new HashMap< String, ArrayList< String > >();
}

public static void CallEvent( String EventName, Object[] args ) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException, InstantiationException{
    if( GameEventDatabaseT.containsKey( EventName ) && GameEventClasses.containsKey( EventName ) ){
        ArrayList< String > methods = GameEventDatabaseT.get( EventName );
        if( methods == null ){
            EventHandler.CallEvent( "GAME_Alert", new String[] {"Fatal","The specified event '"+ EventName +"' was loaded as a 'null'. Please report this to the developer, located at: Techdaan@gmail.com"});
        }else{
            for( int i=0; i<methods.size(); i++ ){
                System.out.println( EventName );
                //methods.get( i ).invoke( Class.forName( GameEventClasses.get( EventName ).get( i ) ).newInstance() , args );
                try {
                    Class.forName( GameEventClasses.get( EventName ).get( i ) ).getMethod( methods.get( i ), Class.forName( GameEventClasses.get( EventName ).get( i ) ) ).invoke( methods.get( i ), args );
                } catch (NoSuchMethodException e) {
                } catch (SecurityException e) {
                }
                System.out.println( "Called " + EventName + " in the class " + GameEventClasses.get( EventName).get( i ) );
            }
        }
    }else{
        EventHandler.CallEvent( "GAME_Alert", new String[] {"Fatal","The specified event '"+ EventName +"' could not be found, or the class index could not be found. Please report this to the developer, located at: Techdaan@gmail.com"});
    }
}
public static void NewEvent( String EventName, String MethodName, String classname ){
    if( GameEventDatabase.containsKey( EventName ) && GameEventClasses.containsKey( EventName ) ){
        GameEventDatabaseT.get( EventName ).add( MethodName );
        GameEventClasses.get( EventName ).add( classname );
    }else{
        ArrayList< String > TempArray = new ArrayList< String >();
        TempArray.add( MethodName );
        GameEventDatabaseT.put( EventName, TempArray);
        ArrayList< String > TempArrayTwo = new ArrayList< String >();
        TempArrayTwo.add( classname );
        GameEventClasses.put( EventName, TempArrayTwo );
    }
}

现在,如果我注册一个活动,它可以正常工作:

            EventHandler.NewEvent( "GAME_Alert", "Alert", "com.gmail.techdaan.pixldtowns.Core.Initialize" ); //From Initialize - Main class
        EventHandler.NewEvent( "GAME_Tick", "HandleGameTick", "com.gmail.techdaan.pixldtowns.Core.Initialize" ); // From Initialize - Main class

如果我打电话给他们,那么这两个工作正常,但是,如果我从&#34; Tick事件&#34;中调用另一个事件,它每1/60秒运行一次,它不会以某种方式达到它。完全没有错误。

    public DrawMainMenu( JFrame Frame ){
    this.gameFrame = Frame;
    setGameBGColor( Color.WHITE );
    try {
        EventHandler.NewEvent( "MENU_DrawMainMenu", "UpdateMainMenu", "com.gmail.techdaan.pixldtowns.Graphics.DrawMainMenu" );
    } catch (SecurityException e) {
    }
}

public void UpdateMainMenu(){
    System.out.println( "b" );
    if( new File( "Resources/interface_login.png" ).exists() ){
        System.out.println( "Yup =)");
    }else{
        System.out.println( "Nope =(");
    }
}

感谢阅读和帮助!

0 个答案:

没有答案