Are you absolutely sure only two bullets are fired? You can try inserting System.out.println("Hello, Bullet World!")
in the run()
method of FireTask
. You might be surprised to find that the line will print three times.
What you're experiencing is the associativity of casts. Consider this part of your code:
(int) seconds * 1000
Since seconds
is a double, and you want an int (or perhaps a long), it's natural that you'll want to cast it. However, the cast does not cast the entire expression seconds * 1000
to an int - just seconds
. Next, consider your instantiations of your DelayFire
class:
new DelayFire(2.0,this,false);
new DelayFire(2.5,this,false);
new DelayFire(3.0,this,true);
Here, you use the double values 2.0
, 2.5
, and 3.0
. However, the 2.5
is casted to an int before the multiplication, causing it to become a flat 2
. As a result, your program will schedule two bullets to be fired after 2 seconds, and one bullet to be fired after 3 seconds.
To get around this problem, stick some parentheses around the entire expression with the multiplication in it:
(int) (seconds * 1000)
This should result in the correct behavior. There may be other bugs in your code, though. But try that first.
Edit: As a side note, the conditions in your if
-statements are overcomplicated. You are basically turning a boolean-valued expression into another boolean-valued expression by writing if (unfire == true)
. Since unfire
is a boolean expression, there is no need for the ==
operator. You can simply write if (unfire)
. And instead of checking if the value is false, use the unary !
-operator to negate the expression, if (!unfire)
.