We are about to switch to a new forum software. Until then we have removed the registration on this forum.
So I have a problem with a code when I compile it, it says implicit super constructor Entity() is undefined for default constructor. Must define an explicit constructor
https://paste2.org/ty3zUmxx (the class Entity is abstract)
Answers
Create an empty default constructor for the class Entity it will solve your problem
Does
super(x, y, im);
matchEntity(int x, int y, PImage im, boolean ally) {}
? =;Well spotted GoToLoop there appears to be a constructor missing in Entity.
If I do super(x, y, im, player) nothing changes ^^
Using
super (x, y, im, player) ;
In the Child class constructor simplifies the situation as well.
That's ... what I just told right ? ^^
You still need the default constructor in Entity
Ok I don't have this error anymore but, why do I need an empty one and I have another error wich is
Return type for the method is missing
on line40 &Constructor call must be the first stement in a constructor
on line 41Your constructor should look like this
The original error message said you needed a default constructor. Hence my post.
My constructor does look like this
Can you post your modified code here please.
https://paste2.org/2Gyj57VB
The name of the class and constructor don't match. Don't need code now
I don't see what you're talking about Oo
The constructor name is different from the class name. Check the spelling.
Oh my god, how is that even possible x) sorry about that and thank you
I have a problem with
void tir() { if (delay < 10) return; delay = 0; entities.add(new Bullet(x+(ally ? w : -tir.width), y+h/2-tir.height), ally); }
I think i've done it wrongIn the posted code you have
bullets.add(
Do you get an error message?
yup because it should be entities.add, It says
The function "add()" expects parameters like "add(int,Entity)"
Neverming i just failed ^^ the parenthesis should be after ally and not before ^^
Okay so now I want to make a ship spawn, so I should just do
entities.add(new Joueur(100, 300));
For example, but nothing appearhttps://paste2.org/WXDVOk90
Any idea ? :(
@poisson86 -- if you don't post your updated code, nobody can answer your question. You've changed your code (somehow) but nobody can see it but you. You say you added
entities.add(new Joueur(100, 300));
-- but where did you add it, is it in the right place? I have no idea; I can't see your code.@jeremydouglass The last link is my updated code, but when I want to have one object from my class I just do
entities.add(new Joueur(100, 300));
outside the class, in my void draw().But if i'm not clear enough, this is an example of what i'd like to do https://paste2.org/CHUMKsPA
@poisson86 - apologies, I didn't see your earlier paste2 link when I asked for code.
Your latest code paste will not run--perhaps poorly encapsulated. I get a pile of errors:
@jeremydouglass Ho yes if I juste copy/paste my code ^^ i'll add the void keyPressed()
Here it is : https://paste2.org/GJKEMZIL
Up ? :(
@poisson86 -- Your code won't run for others because you are using loadImage() incorrectly. Use it like this:
Change your setup and draw, and you will see your new ship on the screen:
This is complex multi-class code, and you don't seem to understand some basic things about how arrays work, how drawing works, or how classes work in this example. You might want to start with something simpler that you write yourself! It will be much easier to understand.
The loadImage("...") was like this because the path couldn't work for you ^^ Okay but if I want to do a game, i'll have like one or two players and thousands enemies how can I do that ? And second question, if I do what you just said, will the entities always keep their variables ? For exemple beetween two levels my ship must return at it's spawn point and have a certain number of HP or something else.
And actually my only big problem is arrays ^^ so i'm trying to learn how it works
Good luck with arrays! Array reference, Array tutorial, Array exampes.
Understanding the draw loop is also important:
Even if you have 1000 enemies, don't create them all every draw loop. If you do, then after 1 seconds (60fps) you will have 60,000 enemies. After 100 seconds you will have 6,000,000 enemies. Have an event controlled by an
if
statement that triggers the creation of an enemy of a batch of enemies.Understanding classes and objects is also important:
Entities are objects -- all objects keep their variables unless you change them or remove / destroy the object. Between two levels, you may want to delete all the enemies and add new ones -- or you may want to keep them. It depends on your game.
Yes my last post was actually stupid (both of them in fact x) ), my question, after thinking, is how can I render 10 ennemies wich are from number 2 to 12 ? do I need to to 10 get(...) or there is something else ?
You already used
entities.add
to create an object, then got it later withentities.get(0)
.So if you call
entities.add
5 more times, you can get them withentities.get(1)
... (2) (3) (4) (5) etc.Read about Classes in the processing reference and tutorial. Then look at the class methods
get
andadd
in your code.These aren't stupid questions, but this very hard code to learn beginning concepts. Where did you get this code?
Okay that was what I was doing ^^ just asked if there was something like
entities.get(1-5)
but nowich code ?
Use a
for
loop which callsget
for i=1, 2, 3...The alternative is to write an Entities class method, like Entities.get, which returns an array of enemies or an ArrayList of enemies from A to B. Something like
getBatch(2,5)
. Then you still have to loop through that list and do something. But this is Advanced. I don't recommend it right now.Re:which code? -- where did you find these classes, Entities etc. etc... where did you find this code?
Okay, and i made ths code myself
Also is it possible to choose which element in my Arraylist I want, for example in my code I have 3 class extended from Entity and I would like to display only the objects from the class Bullet.
for (Entity e : entities) {e.render();}
this is possible but is it possible to have something working likefor (Bullet b : entities) {e.render();}
wich doesn'tThere is something called instanceof but I don't find it very good, isn't there any different method ?
No, you can't filter a list by type with an "enhanced for loop".
Some approaches:
entities
, then test whether the object is a bullet or not. OR:entities
list. Keep your bullets in a separate listbullets
! For each class of objects with different add/remove logic and a different display layer, have a separate list -players
,enemies
,bullets
etc.Okay but for the first one how do I do that, I want to display my bullet, that's it ^^ And I know but I prefer to do that, it will e a lot more easier for the things to come.
There are many different ways -- here is one summary of ways to determine an object's class.
Note that most of the time this isn't recommended.
Okay i'm going, to try :) But what is recommended then ? ^^
Well, it depends. As I mentioned, one way is to keep separate lists.
If you really want one big list, another way is to use polymorphic method calls.
For example:
https://forum.Processing.org/two/discussion/17645/check-sub-class-with-instanceof#Item_4
Okay, from your previous post, what are the differences between the different methods :)doneOkay thank :) , so this works
for (Entity e : entities) { if (e instanceof Bullet) { e.display(); e.update(); }
but when I press a precise Key i'd like to remove those bullet so I tried
for (Entity e : entities) { if (e instanceof Bullet) { for (int i = entities.size() - 1; i >= 0; i--) { entities.remove(i); } } }
But it doesn't work again :/Or maybe there is a way to "reset" all my arraylist without deleting it because i'll have to .add in the loop then.
Hi, what does this do precisly ? @GoToLoop
What your last code post was trying to do: remove() all Bullet instances from your ArrayList entities. (:|
Yes but it's using the List and I'm not, does that do the same thing or I must do as you did and change the other parts of my code?
You mean this statement?:
final List<Entity> entities = new ArrayList<Entity>();
It's creating an ArrayList of Entity objects.
Well, it's an ArrayList. It should do what an ArrayList does. ~O)