Inheritance

edited November 2016 in Questions about Code

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)

«1

Answers

  • Create an empty default constructor for the class Entity it will solve your problem

  • Does super(x, y, im); match Entity(int x, int y, PImage im, boolean ally) {}? =;

  • Well spotted GoToLoop there appears to be a constructor missing in Entity.

  • edited October 2016

    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 41

  • Your constructor should look like this

    Vaiseau(int x, int y, PImage im, boolean player) {
      super(x, y, im, player);
    }
    

    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.

  • 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 wrong

  • In 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 ^^

  • edited October 2016

    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 appear

    https://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.

  • edited October 2016

    @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

  • edited November 2016

    @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:

    The variable "tir" does not exist
    The variable "gauche" does not exist
    The variable "droit" does not exist
    The variable "haut" does not exist
    The variable "bas" does not exist
    The variable "fire" does not exist

  • edited November 2016

    @jeremydouglass Ho yes if I juste copy/paste my code ^^ i'll add the void keyPressed()

    Here it is : https://paste2.org/GJKEMZIL

  • edited November 2016

    @poisson86 -- Your code won't run for others because you are using loadImage() incorrectly. Use it like this:

     joueur = loadImage("https:// upload.wikimedia.org/wikipedia/commons/c/ce/Templaterocket.png");
    
    1. During setup, create your new ship once -- not every draw loop. That adds 60 new ships a second!
    2. During draw, get your ship from the first entry in the entities list with .get(0), then draw your ship with .render()

    Change your setup and draw, and you will see your new ship on the screen:

    void setup() 
    {
      size(1000, 600, P3D);
      background(0);
      joueur = loadImage("https:// upload.wikimedia.org/wikipedia/commons/c/ce/Templaterocket.png);
      tir = loadImage("https:// processing.org/img/processing-web.png");
      entities.add(new Joueur(100, 300));
    }
    void draw() 
    {
      entities.get(0).render();
    }
    

    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.

    thousands enemies how can I do that

    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.

    will the entities always keep their variables

    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 ?

  • edited November 2016

    You already used entities.add to create an object, then got it later with entities.get(0).

    So if you call entities.add 5 more times, you can get them with entities.get(1) ... (2) (3) (4) (5) etc.

    Read about Classes in the processing reference and tutorial. Then look at the class methods get and add 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 no

    wich code ?

  • edited November 2016

    Use a for loop which calls get 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 like for (Bullet b : entities) {e.render();}wich doesn't

  • There is something called instanceof but I don't find it very good, isn't there any different method ?

  • edited November 2016

    No, you can't filter a list by type with an "enhanced for loop".

    Some approaches:

    1. Loop through all objects in entities, then test whether the object is a bullet or not. OR:
    2. Don't have one big entities list. Keep your bullets in a separate list bullets! 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.

    // Method #2
    if (Bullet.class.isInstance(entity))
        ;
    
    // Method #3
    if (Bullet.class.isAssignableFrom(entity.getClass()))
        ;
    

    Note that most of the time this isn't recommended.

  • Okay i'm going, to try :) But what is recommended then ? ^^

  • edited November 2016

    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:

    1. Call entity.update() or entity.display() for each object, and you update and display all objects in the list (without filtering), but the update and display methods do different things.
    2. Alternately, entity has a stub method that does nothing (e.g. explode()). Bullet implements it. So you call explode on everything, and it does nothing, except the bullets. They explode.
    3. etc. etc.
  • edited November 2016

    Okay, from your previous post, what are the differences between the different methods :) done

  • edited November 2016

    Okay 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.

  • edited November 2016
    // forum.Processing.org/two/discussion/18736/inheritance#Item_46
    // GoToLoop (2016-Nov-03)
    
    import java.util.List;
    final List<Entity> entities = new ArrayList<Entity>();
    
    void setup() {
      for (int i = 0; i < 10; ++i) {
        final float rnd = random(1);
        entities.add(rnd < .3? new Entity() : rnd < .7? new Bullet() : new Ship());
      }
      println(entities.size(), entities, ENTER);
    
      removeAllBullets(entities);
      println(entities.size(), entities, ENTER);
    
      exit();
    }
    
    static final <T extends Entity> List<T> removeAllBullets(final List<T> ents) {
      for (int len = ents.size(), i = len; i-- != 0; )
        if (ents.get(i) instanceof Bullet) {
          ents.set(i, ents.get(--len));
          ents.remove(len);
        }
      return ents;
    }
    
    class Entity {
      @ Override String toString() {
        final String name = getClass().getName();
        return name.substring(max(name.indexOf('$') + 1, 0));
      }
    }
    
    class Bullet extends Entity {
    }
    
    class Ship extends Entity {
    }
    
  • edited November 2016

    Hi, what does this do precisly ? @GoToLoop

  • edited November 2016

    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?

  • Yes but it's using the List and I'm not,

    You mean this statement?: final List<Entity> entities = new ArrayList<Entity>();
    It's creating an ArrayList of Entity objects.

    ... does that do the same thing...

    Well, it's an ArrayList. It should do what an ArrayList does. ~O)

Sign In or Register to comment.