|
||
|
|
#1 (permalink) |
|
Join Date: May 2003
Posts: 2,848
|
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. |
|
|
|
|
|
#2 (permalink) | |
|
Account Terminated
Join Date: Jun 2004
Location: Cincinnati, Ohio
Age: 20
Posts: 3,954
|
Quote:
|
|
|
|
|
|
|
#5 (permalink) |
|
Join Date: May 2003
Posts: 2,848
|
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;
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;
}
}
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;
}
}
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. |
|
|
|
|
|
#6 (permalink) |
|
Forum Expert
|
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|