Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C#

C# C# Discussion

Reply
 
Thread Tools Display Modes
Old 12-17-2005, 01:35 AM   #1 (permalink)
 
Join Date: May 2003
Posts: 2,848
Default Yet Another Problem

I've hit tons of bumps doing this little project of mine, but I'm learning tons of new things in the process.

My newest problem is in creating a bunch of the same object separately on the screen at once. This sounds kinda weird, but what I want to do is shoot lasers from this little ship I have. The lasers are the things I'm having trouble with. I can't figure out a way to have multiple lasers on screen without creating a bunch of separate laser objects.

I tried creating an array, but I can't get an array to work with an Image. I tried other things, but nothing is even close to working. I'm sure this seems confusing, but I'm not sure how else to phrase this problem.

I'm sure there's a simple solution to this, but I have no idea what it is or where to look for even a hint. So I've come here again for help and as usual any help is appreciated.
Cmonkey123 is offline   Reply With Quote
Old 12-17-2005, 03:54 AM   #2 (permalink)
Account Terminated
 
Join Date: Jun 2004
Location: Cincinnati, Ohio
Age: 20
Posts: 3,954
Default

Quote:
Originally Posted by Seanchen.net
Post what you have, then we can help you, I assume this is for a script for your shard.

Just to keep the forum all nice and organized, in the future questions inregards to your shard should be posted in Script Support. You can do what you want, I don't care one way or another.
Nope, this is for his graphics program he's been posting about. ;-)
WarAngel is offline   Reply With Quote
Old 12-17-2005, 04:01 AM   #3 (permalink)
Account Terminated
 
Join Date: Jun 2004
Location: Cincinnati, Ohio
Age: 20
Posts: 3,954
Default

I'm guessing a space ship of some sort. I haven't really followed his other posts on the subject of what it is he's making.
WarAngel is offline   Reply With Quote
Old 12-17-2005, 10:26 AM   #4 (permalink)
 
Join Date: May 2003
Posts: 2,848
Default

Nevermind, I think I got it figured out. This program is gonna be sweet when I'm done with it.
Cmonkey123 is offline   Reply With Quote
Old 12-17-2005, 06:15 PM   #5 (permalink)
 
Join Date: May 2003
Posts: 2,848
Default

I lied. I didn't have it figured out.

Here's part of the code I'm having trouble with:
Code:
				case "space":
					if( lasersindex <= 9 && canfire == true )
					{
						lasers[lasersindex] = new Laser();
						lasers[lasersindex].laserX = playerX + 19;
						lasers[lasersindex].laserY = playerY + 5;

						drawlasersindex = lasersindex;

						lasersindex++;

						canfire = false;
						firetimer.Enabled = true;
					}
					break;
That part creates a new laser and fires it when you press space. The screen is refreshed every 3 seconds. The following code is how the lasers in the laser[] array are drawn when the screen is refreshed. I'm pretty sure the problem lies in here somewhere:
Code:
			for( int i = 0; i <= drawlasersindex; i++ )
			{
				if( lasers[i] != null )
				{
					if( lasers[i].laserX < this.ClientSize.Width / 2 )
					{
						g.DrawImage( lasers[i].laser, lasers[i].laserX, lasers[i].laserY );
						lasers[i].Refresh();
					}
					else
					{
						lasers[i].destroylaser = true;
						if( lasersindex != 0 )
							lasersindex--;
						else
							lasersindex = 0;
					}
				}
And finally, here's the Laser() class code, even though I'm sure it's not where the problem lies:
Code:
	public class Laser
	{
		public Image laser;

		public float laserX, laserY;

		public bool destroylaser;

		const string laser_file_name = "..\\..\\lasers.ico";

		public Laser()
		{
			laser = Image.FromFile( laser_file_name );

			laserX = 0;
			laserY = 0;

			destroylaser = false;
		}

		public void Refresh()
		{
			if( destroylaser == false )
				laserX = laserX + 5;
		}
	}
Okay, now the problem is that when the first laser in the laser[] array is larger than the client screen (it should be the only one deleted), lasersindex is subtracted from until it hits zero. So this causes every laser behind the first laser that goes off screen to not be drawn.

I know the problem lies in the draw method because it is the only place where lasersindex is subtracted from. I have no idea how to fix this problem though... I've fiddled with this script for a long time trying to figure this out...

So, as usual, I'd appreciate any help.

And on a side note Seanchen.net, this has nothing to do with RunUO scripting.
Cmonkey123 is offline   Reply With Quote
Old 12-18-2005, 10:57 PM   #6 (permalink)
Forum Expert
 
Join Date: Aug 2004
Location: Redmond, WA
Age: 21
Posts: 1,288
Send a message via AIM to Sep102 Send a message via MSN to Sep102
Default

Easiest way? Use the bounds and null check you already have to your advantage. If it passes the bounds check you want to draw it and if it passes the null test it exists, right? So loop through the entire array each time instead of just to drawlasersindex, that way if it's in the required bounds, you draw it, and if it doesn't exist or is out of bounds, you don't.

A much better way would be to create all Laser's in the array at once, then set the parts of it as you are doing. Then you can get rid of the null check as well.

Last edited by Sep102; 12-18-2005 at 11:17 PM.
Sep102 is offline   Reply With Quote
Old 12-19-2005, 07:08 PM   #7 (permalink)
 
Join Date: May 2003
Posts: 2,848
Default

Yea, thanks. I was subtracting from the index instead of setting it to null, so it was getting all messy and stuff. But it's all fixed and dandy now.
Cmonkey123 is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5