• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

multi firing missles

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
}
}
}
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do you mean an array of missles?
 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}}
}
}
}
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IMHO, that code is make sense.
 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
reply
    Bookmark Topic Watch Topic
  • New Topic