RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Yet Another Problem

Cmonkey123

Wanderer
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.
 

Seanchen.net

Wanderer
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.
 

WarAngel

Wanderer
Seanchen.net said:
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. ;-)
 

Seanchen.net

Wanderer
WarAngel said:
Nope, this is for his graphics program he's been posting about. ;-)

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.

What ship is he talking about?
 

WarAngel

Wanderer
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.
 

Cmonkey123

Wanderer
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.
 

Sep102

Page
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.
 

Cmonkey123

Wanderer
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.
 
Top