C# No overload for method 'loop' takes 0 arguments

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

C# No overload for method 'loop' takes 0 arguments

Post by mattykins »

So I was working on a little RPG game in windows forms. And it is giving me this error
"Error 1 No overload for method 'loop' takes 0 arguments"

So basically I have a picturebox that I want to move around the screen when the user presses W A S and D, I have a loop method that is constantly checking for movement. the loop method looks like this

Code: Select all

static public void loop(object sender, System.Windows.Forms.KeyEventArgs k)
        {
            do
            {
                if (k.KeyCode == Keys.W)
                {
                    HeroMovement.Y = HeroMovement.Y + 50;
                }
                else if (k.KeyCode == Keys.S)
                {
                    HeroMovement.Y = HeroMovement.Y - 50;
                }
                if (k.KeyCode == Keys.A)
                {
                    HeroMovement.X = HeroMovement.X - 50;
                }
                else if (k.KeyCode == Keys.D)
                {
                    HeroMovement.X = HeroMovement.X + 50;
                }
            }
            while (k.KeyCode != Keys.Escape);
        }
And That loop doesn't start untill a button is clicked, which looks like this:

Code: Select all

private void Begin_Click(object sender, EventArgs e)
        {
            gameloop.loop();
        }
My error report tells me that the error is on the gameloop.loop(); line of code. and again the error says
"
No overload for method 'loop' takes 0 arguments
"

Thanks in advanced, Matt :)

BTW this is in Visual C#
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: C# No overload for method 'loop' takes 0 arguments

Post by Chris »

I'm no expert in C# but by the looks of it you aren't passing any parameters to the loop() method when you call it:

Code: Select all

private void Begin_Click(object sender, EventArgs e)
{
    gameloop.loop(sender, e);
}
or simply remove the need for the parameters as the loop doesn't appear to need them

Code: Select all

static public void loop()
{
    //....
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Re: C# No overload for method 'loop' takes 0 arguments

Post by mattykins »

Hm. Its still giving me an error when i try both though..
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: C# No overload for method 'loop' takes 0 arguments

Post by Xaleph »

Well, as Chris mentioned, you need to pass the parameters in the loop() method to get it working. If a method has parameters ( in your case the object sender and the Keys k, you need to pass them once you call or invoke a method.

So:

gameloop.loop();

should be changed to:

KeyEventArgs k = new KeyEventArgs();
gameloop.loop(SenderObject, k);

However, I don`t see why you would need to keep creating new keyEventArgs objects all the time, one time is enough and store it in a class field. Make a getter and inside the Loop() method, just call this.k.

So, to recap, if a method requires 2 parameters, you should give 2 parameters. The overload story is that you can duplicate methods by changing different parameters, however this is not recommended.
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Re: C# No overload for method 'loop' takes 0 arguments

Post by mattykins »

Thanks Guys :)
Howerver in the meantime i redid my code to just look like this
So instead of calling a new method it just started the loop. However i still get the error
"
Error 1 No overload for 'button2_Click' matches delegate 'System.EventHandler'

"

Code: Select all

private void button2_Click(object sender, EventArgs e, System.Windows.Forms.KeyEventArgs k)
        {
            do
            {
                if (k.KeyCode == Keys.W)
                {
                    HeroMovement.Y = HeroMovement.Y + 50;
                    Hero.Location = new Point(HeroMovement.X, HeroMovement.Y);
                }
                else if (k.KeyCode == Keys.S)
                {
                    HeroMovement.Y = HeroMovement.Y - 50;
                    Hero.Location = new Point(HeroMovement.X, HeroMovement.Y);
                }
                if (k.KeyCode == Keys.A)
                {
                    HeroMovement.X = HeroMovement.X - 50;
                    Hero.Location = new Point(HeroMovement.X, HeroMovement.Y);
                }
                else if (k.KeyCode == Keys.D)
                {
                    HeroMovement.X = HeroMovement.X + 50;
                    Hero.Location = new Point(HeroMovement.X, HeroMovement.Y);
                }
            }
            while (k.KeyCode != Keys.Escape);
        }
Last edited by mattykins on Sun Jan 22, 2012 11:31 pm, edited 1 time in total.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: C# No overload for method 'loop' takes 0 arguments

Post by Jackolantern »

The KeyEventArgs are generated each time for each new key event. Why are you trying to get a keyEventArg out of what appears to be a click event? It isn't going to contain the key being pressed.
The indelible lord of tl;dr
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Re: C# No overload for method 'loop' takes 0 arguments

Post by mattykins »

Hm? I'm just now learning how to get keyboard input from the user so, basically i just took some code off of MDSN's website.
http://msdn.microsoft.com/en-us/library ... press.aspx
So what do you mean by that?
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: C# No overload for method 'loop' takes 0 arguments

Post by Jackolantern »

You are going to need to set up a velocity system and a key buffer to get around the accessibility key presses on Windows or the movement is going to be terrible and herky-jerky, even once you get it working.

Basically, each time through the loop you move the picturebox += vx and += vy. Then, you create your "key down" event listener, and inside that listener, you alter the vx and vy variables based on which key was being moved. These are the corresponding movement values:

Right: vx = 5
Left: vx = -5
Down: vy = 5
Up: vy = -5

So that way, each time through the loop, the picturebox's real x and y values have the vx and vy values added to them, respectively. That will have them move to the right, left, up, down, or in any diagonal, and gets rid of that "move one step when the key is pressed, wait 1 second, and then move again" thing that Windows' key buffer causes.

Then you have to add another event listener for the "key up" event, and set the vx or vy of the key just released back to 0. That way they stop moving once the key is released.

While I have never tried to make a game in a Windows Form application, I have this exact system in ActionScript 3, which you should be able to read and apply to C# without much trouble. Just keep in mind that in Flash, you use the onFrameEnter event listener to create the loop, so you just substitute that for your looping mechanism. Here is a complete, simple character movement system:

Code: Select all

package  {
	
	import flash.display.MovieClip;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flash.events.Event;
	
	public class moveCharacter_main extends MovieClip {
		private var vx:int;
		private var vy:int;
		private var myChar:Character;

		public function moveCharacter_main() {
			init();
			myChar = new Character();
			myChar.x = 50;
			myChar.y = 50;
			vy = 0;
			vx = 0;
			stage.addChild(myChar);
		}
		
		private function init():void {
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownF);
			stage.addEventListener(Event.ENTER_FRAME, onFrameEnterF);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUpF);
		}
		
		private function onKeyDownF(event:KeyboardEvent):void {
			if (event.keyCode == Keyboard.UP) {
				vy = -5;
			} else if (event.keyCode == Keyboard.DOWN) {
				vy = 5;
			} else if (event.keyCode == Keyboard.LEFT) {
				vx = -5;
			} else if (event.keyCode == Keyboard.RIGHT) {
				vx = 5;
			}
		}
		
		private function onFrameEnterF(event:Event):void {
			myChar.x += vx;
			myChar.y += vy;
		}
		
		private function onKeyUpF(event:KeyboardEvent):void {
			if (event.keyCode == Keyboard.UP) {
				vy = 0;
			} else if (event.keyCode == Keyboard.DOWN) {
				vy = 0;
			} else if (event.keyCode == Keyboard.LEFT) {
				vx = 0;
			} else if (event.keyCode == Keyboard.RIGHT) {
				vx = 0;
			}
		}

	}
	
}
EDIT: Sorry, but since I have never actually done this in Windows Forms, I am not that familiar with the way C#'s keyDown event system works in Forms. But I know it probably works like most other events in Windows Forms, meaning you would probably need to set the event on the main form itself. Probably click the form to select it, and then change to events in the Property Explorer, and add the key down event. Then the code will pop up, allowing you to add whatever code to the event you want. I think that is how it should work.
The indelible lord of tl;dr
User avatar
mattykins
Posts: 203
Joined: Sun Jan 15, 2012 10:15 pm

Re: C# No overload for method 'loop' takes 0 arguments

Post by mattykins »

Thanks! That was very informative.But i guess i forgot to mention that this game is gonna be a roguelike haha :) So the movement is meant to be herky-jerky cause its grid/tile based. Also i dunno if you saw my post above where I changed the code, its right above your first post i believe.
Post Reply

Return to “Beginner Help and Support”