We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello Sages,
My Missile Command game is starting to look nice, but in trying to get the bombs to drop at helpful (while still slightly random) angles, I am getting some confusing results.
Basically, I have divided the 800 pixels of my game window into quarters.
Bombs fall from random X positions along the top of the screen:
- A bomb from the far left quarter should be assigned a highly positive X change so that it angles to the right.
- X change of bomb from the center-left quarter: slightly negative to moderately positive
- X change of bomb from the center-right quarter: moderately negative to slightly positive
- X change of bomb from the far right quarter: highly negative
I use bombV to hold the X change:
if (bombX < 200) {
bombV = random(0, .05);}
else if ((200 <= bombX) && (bombX < 400)) {
bombV = random(-.025, .04);}
else if ((400 <= bombX) && (bombX < 600)) {
bombV = random(-.04, .025);}
else {
bombV = random(-.05, 0);}
(Yes, I am really using variables for the dimensions of the quarters, but 200/400/600 seems clearer now.)
Somehow, the bombs are angling incorrectly. I am getting console values like Bombs 5-9 in a burst of 9 bombs:
Bomb 1 bombX = 517.0 , bombV = 0.030513832
Bomb 2 bombX = 638.0 , bombV = -6.9901906E-4
Bomb 3 bombX = 248.0 , bombV = -0.042253446
Bomb 4 bombX = 41.0 , bombV = 0.014684731
Bomb 5 bombX = 148.0 , bombV = -0.010228617
Bomb 6 bombX = 624.0 , bombV = 0.040641826
Bomb 7 bombX = 16.0 , bombV = -0.021269897
Bomb 8 bombX = 720.0 , bombV = 0.016892198
Bomb 9 bombX = 19.0 , bombV = -0.023663083
I'd love to know what I'm doing wrong. Thanks much for any help!
Answers
I'd add a line of debug to each block so I could see the bombX value and which block was executing.
How is bombX defined? What type is it?
Thanks for this advice. That debug is smarter than the println()s I added.
I may also have caused a problem with the bombX type:
In the bomb() class, bombX is a float but it's populated by an int when an individual bomb is instantiated. Could that be a problem?
Thanks again!
int
.long
,float
anddouble
.float
, all number types butdouble
are auto-coerced tofloat
.*=
, evendouble
is coerced tofloat
!I am not sure how you got the values you did but the if statement is overly complicated and can be simplified to
I tested it with the code below with this result. The Y axis goes from -0.05 at the top to +0.05 at the bottom.
Wow, I'm so grateful for everyone's attention and advice. I'm also so humbled because (believe it or not) I teach this stuff and can't believe how ridiculously I bungled the IF statement. I corrected it a la quark's and the bombs are dropping perfectly now.
quark, thanks for leading me out of the confusion of my IF statement. Your scatter plot is beautiful and compelling evidence of the clarity of your thought. I was actually thinking about ELIFs executing sequentially as I wrote my monstrosity, but somehow forgot that I didn't need to include the lower bounding limit each time.
GoToLoop, thanks for your summary of data type auto-coercion. I can't wait to say "date type auto-coercion" in class.
koogs, thanks again for your great debugging advice. By inserting quadrant declarations at the points you specified, the nature of the problem became very visible. My println() debugs will be much more helpful to me from now on.
All best to all of you until my next glitch, BrookLev