How do I change weapons with a button.

  • Hello! I'm working on a shooter and got stuck with something simple, I hope you can help me.

    --

    The character have 2 weapons, one called buster and the other a homing attack.

    I created 2 global variables (Buster level (1-3), and Homing level (1-3)).

    So if the buster is set to 1 and above and homing to 0 it will shoot the buster. And if the homing is set to 1 and above and the buster to 0 it will shoot the homing.

    When I press A to change the values it works on the first time, but not on the second time.

    For example:

    1)Press A;

    2) If buster is set to 1, change it to 0 and change homing to 1. <<--- Ok, it works;

    3) If homing is set to 1, change it to 0 and change buster to 0. <<--- The same thing, but don't work.

    --

    The strange thing is that if i change the order of the events, only the first one works. I'm getting frustrated here, trying to solve this thing.

    Can you guys help me out?

  • If you have only two choices then having two variables isn't necessary - just have a Weapon = 1 and do something like Weapon = abs(Weapon -1) on switch. This produces 0 1 0 1 0 1, etc on every activation. Or, simpler in a way - Have it as 1 and do Weapon * -1 on switch. Then compare <0 - one weapon >0 another weapon.

  • I'd list all weapons in an array (weaponList), together with their current power (I'm guessing 1-3 is the power for each weapon, right?) so I could add as many weapons as I needed later. Also, I'd use a variable (selectedWeapon) to store which weapon is currently selected.

    So I could use the following code to switch:

    On <key> pressed >> Set selectedWeapon to (selectedWeapon + 1) % weaponList.Width

    When shooting, I'd just check selectedWeapon and the power of that weapon in weaponsList, and create the blasts accordingly.

  • brunopalermo - very elegant way of doing a size check with the modulus, I like it. Gotta write it down to remember.

  • I don't have time right now, but tomorrow i'll check it out.

    Does it fix the problem where you can't change the variable twice using the same button?

  • I had a little trouble to understand the arrays, but i got it to work! Thanks guys!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • So, with a simple array, let's say we tried to recreate Jazz Jackrabbit's weapons: http://budwin.net/insectoid/dos/jazz/jazzweapons.htm

    Width of 5 for the weapons (fast fire is not a weapon)

    and in height we can have 1 cell for the current ammo (all begin 0 besides blaster with, say, 10)

    another cell for hits required to kill enemy

    another for the max amount of rounds before you can't collect any more ammo per weapon

    and I guess a third to act like a boolean for fast-fire on or off (not sure if this could be done just via events though)

    Now, would this be the best way of setting up an array for this type of simple weapons system? If so, could you get away with adding and subtracting/retrieiving figures from the array without touching the expression editor?

  • I'd list all weapons in an array (weaponList), together with their current power (I'm guessing 1-3 is the power for each weapon, right?) so I could add as many weapons as I needed later. Also, I'd use a variable (selectedWeapon) to store which weapon is currently selected.

    So I could use the following code to switch:

    On <key> pressed >> Set selectedWeapon to (selectedWeapon + 1) % weaponList.Width

    When shooting, I'd just check selectedWeapon and the power of that weapon in weaponsList, and create the blasts accordingly.

    Do you have an example of this kind of idea that I could compare my own to? I've never used arrays and I think mine looks okay but I'm having a bit of trouble keeping my head together with it

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)