Jump to content

Best way to implement multiple gun types


Recommended Posts

Ok so Ive been messing with this for the last couple days and have looked at a bunch of post but I'm still completely confused on the best or most efficient way to add a bunch of different gun types then can easily be exchanged with each other.


So for an example I would like to start with a Handgun, but be able to go buy a rifle or other gun for X amount of points. Also whats a good way to set up multiple gun equipment spots and use the scroller to switch between them.


At first I tried adding a gunGroup then adding more groups like handgun, and rifle. But I can get the rendering to work right, it just seems really buggy looking.


Now I just read a post about adding a child to the player sprite, this method I haven't tried because I thought I would need them to be in a group to have multiple gun pickup locations through the map, can you do this without using groups?


So Ive come to the conclusion that I need someone to break it down a little better for me, any help is very much appreciated.

Link to comment
Share on other sites

I think this is a pretty broad topic, perhaps so broad that you're having trouble focusing on specific parts of it. You need to break it down into the various things it needs to do. I guess you need the following:

  • A series of 'slots' to hold guns, possibly use an array for this?
  • A visual representation of the slots that gets updated whenever the array does, so guns can't occupy the same slot etc.
  • An abstract representation of each gun, with associated price.
  • A value to hold the player's cash.
  • A way of buying a gun, which checks if the player has the cash, checks if they have an empty slot, and if so, puts the gun into the slot and subtracts the appropriate amount of cash.

It will probably turn out that the best way to do this is to separate rendering from logic - to have abstract data representations of what's going on (which are easy to handle) and then create functions which take that data and turn it into the visuals. In programming this is called 'separation of concerns', and is also fairly well represented by the 'Model/View/Controller' (MVC) pattern, where the model is your slots (an array) and guns (some objects with a name and price), the view is the rendering of those slots and guns as images, and the controller is the 'glue' that binds the two, handling the player inputs to buy/select weapons, updating the model and then telling the view what to render.

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...