-1

簡単なゲームを作ろうとしていますが、この例外で失敗します。

System.ArgumentException: Parameter is not valid.
   at System.Drawing.Graphics.GetHdc()
   at System.Drawing.BufferedGraphics.RenderInternal(HandleRef refTargetDC, BufferedGraphics buffer)
   at System.Drawing.BufferedGraphics.Render()
   at System.Windows.Forms.Control.WmPaint(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at GECS.Core.Game.runGame(Game g) in c:\Users\sriharshachilakapati\Documents\SharpDevelop Projects\GECS\GECS\Core\Game.cs:line 16
   at GECS.Test.GameTest.Main(String[] args) in c:\Users\sriharshachilakapati\Documents\SharpDevelop Projects\GECS\GECS\Test\GameTest.cs

これは私が使用するコードです。

public class Game : Form {

    public Game() : base() {
        gameLoop();
    }

    public static void runGame(Game g){
        Application.Run(g);
    }

    public virtual void initResources(){}

    public virtual void update(long elapsedTime){}

    public virtual void render(Graphics g){}

    private void gameLoop(){
        DoubleBuffered = true;
/** The following line gives me exception **/
        Paint += delegate(object sender, PaintEventArgs e) { render(e.Graphics); };
        initResources();
        int now = getCurrentTime();
        int gameTime = getCurrentTime();
        long elapsedTime = 1000 / Global.STEPS_PER_SECOND;
        while (this.Created){
            this.Text = Global.TITLE;
            now = getCurrentTime();
            while (now>gameTime){
                update(elapsedTime);
                gameTime += (int)elapsedTime;
            }
            Refresh();
        }
    }

    public static int getCurrentTime(){
        return System.Environment.TickCount;
    }

}

ありがとう。

4

1 に答える 1

0

解決策を見つけました。これは、を呼び出す前にループを呼び出しているためですApplication.Run

于 2013-01-04T16:46:24.203 に答える