Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

multi firing missles

 
dawna brost
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to fire multiple missles how can i adjust this to make it fire more than one missles?
Please


fire missile from given x coordinate
public void fireMissile(int x) {
if (!missile.isActive()) { // if missile sprite
// isn't active
if (x <= min_x) {
missile.init(mis_min_x);
}
else if (x >= max_x) {
missile.init(mis_max_x);
}
else {
missile.init(x-1); // initialize missile
}
}
}
 
Rob Spoor
Sheriff
Posts: 20827
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a collection of missiles, iterate over that collection and use the code for each missile.
 
dawna brost
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do you mean an array of missles?
 
Rob Spoor
Sheriff
Posts: 20827
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use an array if the number of missiles is fixed yes.
However, the word "collection" was a hint - there is an interface called Collection in the java.util package, with subinterfaces Set and List. If you ever need a collection that can grow, please look into it, and also the Collections Tutorial. Please don't hesitate - you'll really need collections eventually.
 
dawna brost
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is this what you were talking about?



private MissileSprite missile[15];

// fire missile from given x coordinate
public void fireMissile(int x) {
maxMis = 15;
for (int i= 0; i < maxMis; i++){
if (!missile[i].isActive()) { // if missile sprite
// isn't active
if (x <= min_x) {
missile[i].init(mis_min_x);
break;
}
else if (x >= max_x) {
missile[i].init(mis_max_x);
break;
}
else {
missile[i].init(x-1); // initialize missile
if (zinsound != null) {
zinsound.play(); //play zing to shoot
break;
}}
}
}
}
 
tito bayuadji
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMHO, that code is make sense.
 
Rob Spoor
Sheriff
Posts: 20827
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just one remark: you can get the actual size of the missile array by calling missile.length. That way, the number of missiles is coded in only one place, and if you change it you only have to change it in one place. Another solution would be to use static final variables (constants).
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!