-
October 28th, 2010, 04:20 AM
#1
Function Call Fail
I'm new to C# and I'm having an issue trying to call a function from within a timer. I'll include my code below. The only time I get this error is when I try to call the MovePlayer function within the timer, the second I comment that out it runs where am I going wrong and why?
public void MovePlayer(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W)
{
imgPlayer.Top = imgPlayer.Top + 10;
}
}
private void tmrPlayer_Tick(object sender, EventArgs e)
{
MovePlayer();
}
}
-
October 28th, 2010, 05:59 AM
#2
Re: Function Call Fail
You are not passing any arguments to the MovePlayer function.
-
October 28th, 2010, 07:22 AM
#3
Re: Function Call Fail
Originally Posted by jeet_xp
You are not passing any arguments to the MovePlayer function.
What arguments does it need, to me that should run with out needing anything. I would have thought that at each interval the timer would call the function MovePlayer, then because the function is called it would check for the keypress and then move imgPlayer
-
October 28th, 2010, 07:42 AM
#4
Re: Function Call Fail
But you didn't pass it any parameter... so how would it be able to check anything? That's kind of like taking meat out of the fridge, putting it on the counter, and expecting the skillet to cook it simply because you passed the meat to the counter event handler, even though you didn't pass it to the skillet handler.
That being said... the tick event doesn't have KeyCode as part of its data... so even if you were to pass the data, it's an incompatible type (EventArgs vs KeyEventArgs) and wouldn't contain the necessary data.
Just what is it exactly you're trying to accomplish? What's the point of the Timer if all it's going to do is call the event handler of another object?
-tg
-
October 28th, 2010, 12:03 PM
#5
Re: Function Call Fail
Also, why are you directly calling a KeyDown event handler method? That method will fire automatically when a key is pressed and the associated control has focus. You have named it in an odd manner, so maybe you just copied it from the internet and it is not actually handling any event. It seems that you should spend some more time learning the basics before going any further.
-
October 28th, 2010, 12:56 PM
#6
Re: Function Call Fail
I didn't think I'd been doing too bad haha, I've come from a VB6 background having done a term of it whilst at college and wanted to try and convert and expand a project that I'd started.
My aim is to create a pong based game and I was intending to use the timer to help with moving the players paddle(s). I thought that every 100ms the timer would call the MovePlayer function which would then move the paddle up or down dependant on which buttons being pressed, or stay static if nothings pressed.
I've got a square that will bounce around the screen and bounce off the players paddle with the next step planned to make the paddle move. My full code that follows might help make a bit more sence of what I'm doing/ not doing/ doing wrong.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1.Pong
{
public partial class frmHome : Form
{
int XAngle = -5;
int YAngle = 0;
public frmHome()
{
InitializeComponent();
}
private void tmrBall_Tick(object sender, EventArgs e)
{
imgBall.Left = imgBall.Left + XAngle;
imgBall.Top = imgBall.Top + YAngle;
HitWall();
HitPlayer();
}
public void HitWall()
{
//Checks if Top of ball has hit then inverses angle
if (imgBall.Top <= 0)
{
YAngle = YAngle * -1;
}
//Checks if Bottom of ball has hit then inverses angle
// -12 = offset of panels position from top
if (imgBall.Top + imgBall.Height >= pnl1.Top + pnl1.Height - 12)
{
YAngle = YAngle * -1;
}
//Checks if Left of ball has hit then inverses angle
if (imgBall.Left <= 0)
{
XAngle = XAngle * -1;
}
//Checks if Right of ball has hit then inverses angle
// -12 = offset of panels position from left
if (imgBall.Left + imgBall.Width >= pnl1.Left + pnl1.Width - 12)
{
XAngle = XAngle * -1;
}
}
public void HitPlayer()
{
if (imgBall.Left <= imgPlayer.Left + imgPlayer.Width &&
imgBall.Top + imgBall.Height >= imgPlayer.Top &&
imgBall.Top + imgBall.Height <= imgPlayer.Top + imgPlayer.Height)
{
//if (imgBall.Top + imgBall.Height >= imgPlayer.Top &&
// imgBall.Top + imgBall.Height < imgPlayer.Height / 2)
//{
lblInfo.Text = "true";
XAngle = XAngle * -1;
YAngle = YAngle * -1;
//}
}
}
public void MovePlayer(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W)
{
imgPlayer.Top = imgPlayer.Top + 10;
}
}
private void tmrPlayer_Tick(object sender, EventArgs e)
{
MovePlayer();
}
}
}
-
October 28th, 2010, 01:12 PM
#7
Re: Function Call Fail
Ok, but you're getting hung up on very basic things here. How to define and call a function, order of execution, etc. There is really no point in attempting to create an application if you don't yet understand how to call a function.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|