We are about to switch to a new forum software. Until then we have removed the registration on this forum.
How do I make these aliens into one variable? Once I start shooting them, they become a mess of confusion.
I have no idea what do to.
If you answer, please talk to me like you'd talk to a five year old... Thanks.
Answers
Learn about ArrayList.
First, stop having three classes for three different types of aliens. Have one class of aliens, and add a type variable to that class that determines what a specific alien looks like.
Then, add a boolean to that class as well, that determines if an alien exists any more. Do not draw aliens that do not exist. Do not bother updating their position. Do not cause the aliens to change direction because of an alien that does not exist.
Now when you shoot them, if an alien gets hit, all you need to do is change that value from true to false...
Hi Tf, I work in p5 and i think classes in p5 are called constructor functions. I have three aliens, where or how do I start to make one class. Or how do I start or where do I look to get going with regards to your first paragraph?
https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor
https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/class
https://forum.Processing.org/two/discussion/21362/typeerror-undefined-is-not-an-object-evaluating-bunker-x#Item_7
@tim50 -- whether you choose to call p5.js classes "classes" or not...
https://github.com/processing/p5.js/wiki/JavaScript-basics#objects
...TfGuy's advice is that you have three of them, and you should combine them into one. So instead calling three different constructor functions "Alien()" "Alien1()" "Alien2()" you would pass a type argument to a single constructor function, "Alien()", which does something different depending on the argument: "Alien(0)" "Alien(1)" "Alien(2)"
I did this. Now I only have one class. I don't have an array. If I had an array, I'd only be able to make an unlimited amount of ONE of the ellipses- the first one. Does the term "type variable" mean something?
https://openprocessing.org/sketch/422108
Each of your Aliens has an X position, right? Even though it is different for each Alien, you have an X position variable in your Alien class (or object constructor function), so each Alien has their own copy of an X position variable.
Do the same thing for a new variable, but store a number for the TYPE of Alien that each alien is. So Some Aliens will have a type-value of 0, some will have 1, and some will be 2. Then draw the aliens differently based on the value each alien has stored in it's own copy of the type variable.
No. It short for "what-TYPE-of-alien-is-it variable."
edit: example written in Processing(Java), not p5.js
My x and y positions are a mess.
I don't know what I'm doing and I have no idea where to go from here. I don't know (and it's going to take me a while to know) what a different class is going to mean about anything... Would you guys say, my code is completely off?
I'm really stuck. I think you guys are saying: Make one class and put the three aliens in one constructor and put them into an array then you have them as [0, 1, 2].... so I guess if that is what you are saying, I should be able to figure that out... but will that help the overall problem I have with hit detection?
Here is an example. It has one Alien class, but there are three types of aliens. Notice that the class has a type variable, which determines the shape and color for each alien.
Yes, that is exactly what I mean. And then, once you have one class for Aliens, you won't have to have three copies of the hit-detecting code. You will also be able to add a single boolean to the class that gets set to true when an Alien is hit.
Jeremy left this above. If I were to do this, would it help me at all? I can do this but there is no array so why would I want to do it?
https://p5js.org/examples/objects-multiple-objects.html
I can make one class and one constructor but I can't get those methods or whatever they are called into one array because the array uses the class name, not the constructor part that says: this.alien1 = function(). I don't know processing. That will just confuse me more. Where does the type variable thing go? Do I use push?
NOTE: Should I just take a screenshot of the aliens, buy photoshop, create a png, and after a year of trying to figure all that out, use a png image for the aliens? or will that not work?
Okay, here is the same example done using P5.js:
See how there is one Alien function? Any object created by the Alien() constructor function is an Alien. But there are three types of Aliens, and the type is passed to the Alien() function as a parameter. The stored type then determines how the Alien actually looks when it is drawn.
The "type" variable could be named anything. The concept here is that each Alien has it's own copy of the variables used in the Alien() constructor function, and "type" is just one of them. You can now add other variables (and behavior) to this single class.
Thanks Tf... Sorry, just frustrated. I better go away from it for a bit.