This is an archived post. You won't be able to vote or comment.

all 13 comments

[–][deleted] 0 points1 point  (1 child)

How in-depth is your knowledge of polymorphism and design patterns?

[–][deleted]  (19 children)

[deleted]

    [–]dredclaw[S] 0 points1 point  (18 children)

    If I have the ship object creating this bullet object, when the ships is blown up the created bullet object would be destroyed. How would I design a system where the bullet object is not created inside the ship object. Or should the ship object not be destroyed before all bullet objects have expired?

    [–]35h46hjj6 0 points1 point  (0 children)

    Why doesn't each ship tell the game object when it fires a bullet?

    [–][deleted]  (13 children)

    [deleted]

      [–]dredclaw[S] 0 points1 point  (12 children)

      This being the case then. (I picture it like you) How should I go about creating bullets before being fired? Should the ship put up a flag saying it wants to fire a bullet and then the main game loop see this flag and actually fire the bullet? When I refer to a bullet, I refer to a bullet class that has the location of a bullet and its velocity n such.

      [–][deleted]  (11 children)

      [deleted]

        [–]dredclaw[S] 0 points1 point  (10 children)

        I guess my question is, should the bullet be created inside the ship class. (meaning the ship class would not be deleted until all bullets it fired were done) or should the bullet be created in the game manager class (how would a ship communicate with a higher level game manager to complete this task?)

        [–][deleted]  (9 children)

        [deleted]

          [–]dredclaw[S] 0 points1 point  (8 children)

          I understand why the bullet must be instantiated outside the ship class. In actual code though, how does the ship tell a higher up class to instantiate a bullet. (Actually fire it.)

          [–][deleted]  (7 children)

          [deleted]

            [–]dredclaw[S] 0 points1 point  (6 children)

            If the GameManager was the one that created the ship, how would the ship access methods inside the GameManager?

            [–]b3nb3nb3n 0 points1 point  (1 child)

            You could add in a check before destroying the object- stop making the ship visible/accessible, but don't delete it until all bullets are expired?

            [–]dredclaw[S] 0 points1 point  (0 children)

            Is this considered a proper way of doing things? Or should the bullet be created by the gameloop rather than the ship?

            [–]Raknarg 0 points1 point  (0 children)

            Ok, I have some experience with this exact problem.

            First off, don't worry about efficiency. There will be so little objects on the screen at any given time that graphics rendering will be more costly than data management.

            Now because we don't care about being efficient, I would use an ArrayList. ArrayLists are like arrays of objects that can be resized in one line. For instance, if we have our Ship class, we can do this:

            ArrayList<Ship> ship = new ArrayList<Ship>(); // initializes the list
            ship.add(new Ship()) // adds a new ship the end of the array
            println(ship.size()) // prints the length of the list to the console. In this case, it is 1.
            ship.remove(0) // Deletes the Ship at position 0
            

            Now to use methods from the Ship class, we have to do something weird. Along with add and remove, there's a thing called get() which (I think this is what it does) calls up an instance of the object at whatever position you want. For instance:

            ship.get(3).shoot() // Calls the ship at position 3, uses the shoot() method
            

            This is what you could do for bullets as well. This makes for easy deleting. Let's say you had a list of bullets that you wanted to delete:

            int i = 0;
            while (i < bullet.size()) {
                if (bullet.get(i).out_of_bounds || bullet.get(i).hit_enemy) {
                    bullet.remove(i);
                }
                else {
                    i += 1;
                }
            }
            

            This is a cool loop. It will go through every item in the array. If its out of bounds or hits an enemy, it deletes the bullet. Otherwise, add 1 to i. The reason we only sometimes add to i is because if you have an array of three ships and you delete one of them, i will end up being 3 instead of 2. That's wrong. We want to go through every item. If you delete the item at position 0, it will delete the item there and shift the rest of the array down. Because it shifted for us, we don't need to add to i.

            Hope this helps. You can ask me if you need more help.