I'm trying to program PONG from scratch, by myself, just to learn how. I have experience in VB, and I was told to program PONG back then to learn how, so I'm now trying it with C#.
I have a form, with a label (lblBall) which I'm trying to get to move around the screen. My first step was to use a Button click to move the ball (lblBall.Left = lblBall.Left +10). That worked fine.
Now I'm trying to use a timer and loop to keep the ball moving for a few seconds.
I get an error: Error 1 An object reference is required for the non-static field, method, or property 'WindowsFormsApplication8.Form1.lblBall' c:\users\documents\visual studio 2010\Projects\WindowsFormsApplication8\WindowsFormsApplication8\Form1.cs 42 17 WindowsFormsApplication8
I understand the error (in English), but I don't know how to properly "reference" the "object".
So I guess my question is;
"What is the Syntax for referencing an object?" (obviously I am not a professionl OOP programmer)
All answers to the question are greatly appreciated.
Thanks!
Here's the code, just in case anyone wants to see it.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Timers;
namespace
WindowsFormsApplication8
{
public partial classForm1 : Form
{
private static System.Timers.Timer aTimer;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
privatevoid button1_Click_1(object sender, EventArgs e)
{
aTimer = new System.Timers.Timer(1000);
aTimer.Elapsed += newElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 500;
aTimer.Enabled = true;
}
private static void OnTimedEvent( object source, ElapsedEventArgs e)
{
for (int i = 5; i > 0; i = i - 1)
{
lblBall.Left = lblBall.Left + 10;
lblBall.Top = lblBall.Top + 10;
}
}
}
}
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.