CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2010
    Posts
    7

    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();
    }
    }

  2. #2
    Join Date
    Dec 2005
    Posts
    8

    Re: Function Call Fail

    You are not passing any arguments to the MovePlayer function.

  3. #3
    Join Date
    Oct 2010
    Posts
    7

    Re: Function Call Fail

    Quote Originally Posted by jeet_xp View Post
    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

  4. #4
    Join Date
    Dec 2007
    Posts
    234

    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
    * I don't respond to private requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help - how to remove eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to???
    * On Error Resume Next is error ignoring, not error handling(tm). * Use Offensive Programming, not Defensive Programming.
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN
    MVP '06-'10

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    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.

  6. #6
    Join Date
    Oct 2010
    Posts
    7

    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();
    }
    }
    }

  7. #7
    Join Date
    Jun 2008
    Posts
    2,477

    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
  •  





Click Here to Expand Forum to Full Width

Featured