c# - xna charge power on how long the key is pressed -


i working on shooting game. in number of shoot fix. make variable based on how long pressed keyboard key. more power should generated if key pressed longer.

here code.

        {             // create projectile             worldentities.add(new sphere(graphicsdevice, 0.1f, 0.2f, character.eyeposition() + character.looktowards(), 0.8f, color.blue));             // add gravity projectile             (worldentities[worldentities.count - 1] rigidbody).acceleration = new vector3(0f, -10f, 0f);              // calculate launch velocity             vector3 launchvelocity = character.looktowards() * 10f;               // set particle velocity launch velocity             (worldentities[worldentities.count - 1] rigidbody).velocity = launchvelocity;              // reset timer             timer = 1f;         } 

have variable power multiplier, cause velocity 0% @ first press, , 100% when has been held down maximum time

float power; const float maxtime = 1000; //1 second 

in update method need check key pressed , add elapsed time since last frame.

if (keyboard.getstate().iskeydown(keys.space))     power += (float)gametime.elapsedgametime.totalmilliseconds; 

we need check if power has reached max power, , shoot it.

if (power >= maxtime)     //fire! 

also, case incase user lets go of key before time fire. (lastkeyboardstate last frames state, set keyboard.getstate() @ end of each frame)

if (lastkeyboardstate.iskeydown(keys.space) && !currentkeyboardstate.iskeydown(keys.space) { //fire, without full power } 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -