Author Topic: Fire at button release  (Read 1400 times)

superp

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Fire at button release
« on: April 12, 2014, 09:11:27 PM »
I have code that while a button is pressed, strength goes up and when it is released, it launches missile at the strength.
Sometimes, even while i am pressing the button, it thinks i released and launches missile.
I think sometimes Update() doesn't think the key is pressed?

Here is my code
  1. bool pressed;
  2.         void OnPress(bool pressed)
  3.         {
  4.                 this.pressed = pressed;
  5.         }
  6.        
  7.         public const float PowerMultiplier = 15.0f;
  8.         bool previouslyPressed;
  9.         void Update()
  10.         {
  11.                 if(LaunchMode)
  12.                 {
  13.                         if(pressed)
  14.                         {
  15.                                 if(!previouslyPressed)
  16.                                 {
  17.                                         Messenger.Broadcast("PLAYERTANK_RESET_POWER");
  18.                                         previouslyPressed = true;
  19.                                 }
  20.                                 Messenger.Broadcast("PLAYERTANK_UPDATE_POWER");
  21.                         }
  22.                         else if(previouslyPressed)
  23.                         {
  24.                                 previouslyPressed = false;
  25.                                 LaunchMode = false;
  26.                                 Messenger.Broadcast("PLAYERTANK_LAUNCHED");
  27.                         }
  28.                 }
  29.                 else
  30.                 {
  31.                         //todo: shield mode
  32.                 }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Fire at button release
« Reply #1 on: April 13, 2014, 01:02:02 AM »
Why are you doing that in the Update() function? Why not just do it inside OnPress? I can see the update power call belonging in Update perhaps, but the other two? Not so much.