Saturday, May 22, 2010

PLease help I can't get this C# code to work?

for some reason the below code won't work right I copied it from a ebook.





namespace WindowsFormsApplication1


{


public partial class Form1 : Form


{


int dx;


int dy;


public Form1()


{InitializeComponent();}


private void theForm_KeyDown(object sender,System.Windows.Forms.KeyEventArgs e)


{switch (e.KeyCode)


{case Keys.Right:


picArrow.Image = myPics.Images[0];


dx = 2;


dy = 0;


break;


case Keys.Down:


picArrow.Image = myPics.Images[1];


dx = 0;


dy = 2;


break;


case Keys.Left:


picArrow.Image = myPics.Images[2];


dx = -2;


dy = 0;


break;


case Keys.Up:


picArrow.Image = myPics.Images[3];


dx = 0;


dy = -2;


break;}


private void timer1_Tick(object sender, System.EventArgs e)


{


int newX, newY;


newX = picArrow.Location.X + dx;


if (newX %26gt; this.Width)


{newX = 0;}


if (newX %26lt; 0)


{newX = this.Width - picArrow.Width;}


newY = picArrow.Location.Y + dy;


if (newY %26lt; 0)


{newY = this.Height - picArrow.Height;}


if (newY %26gt; this.Height)


{newY = 0; }


Point newLoc = new Point(newX, newY);


picArrow.Location = newLoc;


}


}

PLease help I can't get this C# code to work?
Nobody is really going to be able to help you unless you at least give us the compiler error... however - I will point out a few things:





1) myPics.Images[x] doesn't seem to be defined anywhere in your code - so it isn't likely to do what you are hoping.


2) You have 2 methods here - but you said you just copied code... have you also added the control (timer1) to your Form, so that it can link up with the method?


No comments:

Post a Comment