XNA/C# projectiles firing issue - need one projectile at a time to fire on spacebar press - c

Problem: Upon spacebar press i need my projectile to fire and continue its life regardless of another press or letting go of the spacebar.
below is both the code adding, updating, and drawing the projectiles. I have tried numerous reworkings of this code but alas am not experienced enough to have found a suitable solution yet.
AddProjectile code below
enter code here
//add projectile if spacebar is pressed
private void AddProjectile(Vector2 position)
{
//i need somthing like this to make bullets autofire on phone
//while (TouchPanel.IsGestureAvailable)
//{
// Projectile projectile = new Projectile();
// projectile.Initialize(GraphicsDevice.Viewport, projectileTexture, position);
// projectiles.Add(projectile);
//}
if (currentKeyboardState.IsKeyDown(Keys.Space) ||
currentGamePadState.Buttons.A == ButtonState.Pressed)
{
Projectile projectile = new Projectile();
projectile.Initialize(GraphicsDevice.Viewport, projectileTexture, position);
projectiles.Add(projectile);
}
}
Update Projectile code below
private void UpdateProjectiles()
{
//update projectiles
for (int i = projectiles.Count - 1; i >= 0; i--)
{
if (currentKeyboardState.IsKeyDown(Keys.Space) ||
currentGamePadState.Buttons.A == ButtonState.Pressed)
{
//adds particle to first projectile but not again until the next fire butotn press
//particleEngine.EmitterLocation = new Vector2(projectiles[i].Position.X, projectiles[i].Position.Y);
projectiles[i].Update();
projectileOn = true;
}
//if projectiles not being fired remove them from the game screen
else
{
projectiles.RemoveAt(i);
}
}
}
the Draw method that draws projectiles to screen
//draw the projectiles
//***********************************************************
//using the if here allows control of projectiles to pass...
//...to the "currentstate of the spacebar (is pressed = fire)
/***/
if (currentKeyboardState.IsKeyDown(Keys.Space) ||
currentGamePadState.Buttons.A == ButtonState.Pressed)
{
for (int i = 0; i < projectiles.Count; i++)
{
projectiles[i].Draw(spriteBatch);
}
}
//remove projectiles and reset
if (currentKeyboardState.IsKeyUp(Keys.Space) ||
currentGamePadState.Buttons.A == ButtonState.Released)
{
UpdateProjectiles();
}
So, this is what i have so far and as stated can get the projectile to worl fine , i just cant get them to continue their life (until a collision or they reach the end of screen) once i let go of the keyboard spacebar.

A quick overview of the problems I see that are causing your isses:
You're checking if keys/buttons are pressed during your add, update, and draw methods.
If a key isn't pressed, you're removing the item during an update and not drawing it during a draw.
To accomplish what I think you're trying to accomplish, I'd do something along these lines (tailored and cleaned up for your needs, of course):
private void Update(GameTime gameTime)
{
// Only add a new projectile if Space or A is pressed
if (currentKeyboardState.IsKeyDown(Keys.Space) ||
currentGamePadState.Buttons.A == ButtonState.Pressed)
{
AddProjectile(); // some method that adds a new projectile to 'projectiles'
}
// Update all existing projectiles, regardless of button press.
// This will allow your projectiles to continue flying after they have been fired.
for (int i = 0; i < projectiles.Count; i++)
{
projectiles[i].Update();
if (projectiles[i].Dead) // check if projectiles[i] is out of bounds or has collided with something
{
projectiles.RemoveAt(i);
i--;
}
}
}
private void Draw(GameTime gameTime)
{
// Draw all existing projectiles, regardless of button press.
for (int i = 0; i < projectiles.Count; i++)
{
projectiles[i].Draw(spriteBatch);
}
}
Your space bar or A button is only used to fire a new projectile. You want that projectile to fly until it hits something or flies off the side of the screen, and it shouldn't depend on whether you have a button pressed in order to do that. Neither should drawing the bullet.
Make sense?

Related

My loop only terminates when the button is pressed after the last frame has passed

I'm trying to run some pixels on a loop in main. When I press a button the pixels are supposed to stop, but this only works if I press the button right after the last pixel has run. How can I make it so that if I press the button anywhere the loop just stops?
Here is the code
int main() {
while(1) {
update();
frames();
}
}
void update() {
if (button_pressed) {
buttonPresses = !buttonPresses;
}
void frames() {
if (buttonPresses == 1) {
pixel1();
pixel2();
pixel3();
}
}
I tried changing the method to use while loop instead of if, but still does not work.
If you are trying to stop at any pixels you should check it after every pixel drawn. Something like this should do the trick.
void frames() {
if (buttonPresses == 1)
pixel1();
if (buttonPresses == 1)
pixel2();
if (buttonPresses == 1)
pixel3();
}

How to return back when the button is clicked?

When I first press the button, it performs the first function, on the second press, it performs the second task when the third press I want to return and perform the same first function.
void blinkcursor(void);
int count = 0;
void blinkcursor(void)
{
label:
if (button == 1) { //button pressed
if (count == 0) {
count++;
set_cursor_position (1, 2);
lcd_cout(0x0f); //cursor blink
button = 0;
}
else if(count == 1) {
lcd_cout(0x0c); //blink off
button = 0;
}
goto label;
}
}
You didn't say much about the overall structure of your program, so I am just going to assume that you have some main loop running that regularly checks the state of the button, performs any necessary debouncing, and sets button to 1 whenever it detects that the button was pressed. I will also assume that the cursor is not blinking by default, and that the button is only used to toggle the blinking of the cursor.
Now just add a call to the following function in your main loop:
bool blink_enabled = false;
void update_blink(void)
{
if (button) {
// Button was pressed.
if (blink_enabled) {
// Turn off blinking.
lcd_cout(0x0c);
blink_enabled = false;
}
else {
// Turn on blinking.
set_cursor_position(1, 2);
lcd_cout(0x0f);
blink_enabled = true;
}
button = 0;
}
}
(You might need to add #include <stdbool.h> at the top to get the bool type.)

SDL_GetKeyboardState not working

I am trying to make a controller for a game with SDL 2(didn't want to ask on gamedev since it is not a game issue directly) I use SDL_GetKeyboardEvent to see if the navigation arrows are being pressed but it apparently doesn't work, it is supposed to print a value 1 or -1 if one of those keys is pressed but it doesn't it just prints 0 even if I hold the key down for several seconds, it is like it doesn't detect that the key is being pressed. I searched all over the internet and this is how they do it, but it doesn't work for me.
#define SDL_MAIN_HANDLED
#include <stdio.h>
#include "SDL.h"
/* I'll add some code later
so something that isn't used
might be initialized
*/
int main (void)
{
int a = 1;
int x;
int z;
SDL_Event quit;
const Uint8 *keys = SDL_GetKeyboardState(NULL);
SDL_Init(SDL_INIT_VIDEO);
while(a)
{
SDL_PollEvent(&quit);
if(quit.type == SDL_QUIT)
a = 0;
if(keys[SDL_SCANCODE_UP])
z = 1;
else if(keys[SDL_SCANCODE_DOWN])
z = -1;
else
z = 0;
if(keys[SDL_SCANCODE_LEFT])
x = -1;
else if(keys[SDL_SCANCODE_RIGHT])
x = 1;
else
x = 0;
printf("%d, %d\n", x, z);
//This is supposed to print
//x, z values so if up arrow is pressed it will print 0, 1 and if
//down arrow is pressed it will print 0, -1: the same with horizontal ones.
//1 or -1, 1 or -1
}
SDL_Quit();
return 0;
}
Read the documentation: wiki
Note: This function gives you the current state after all events have been processed, so if a key or button has been pressed and released before you process events, then the pressed state will never show up in the SDL_GetKeyboardState() calls
What it means is?
You need to process all events. How? Looping the PollEvent, after the loop (or if you want to check in the loop, check at the end), the SDL_GetKeyboardState is usable.
So, go through the loop, check for keyboards states. Do not forget to always go through the loop before checking for keys
e.g.
while (game)
{
/*! updates the array of keystates */
while ((SDL_PollEvent(&e)) != 0)
{
/*! request quit */
if (e.type == SDL_QUIT)
{
game = false;
}
}
if (keys[SDL_SCANCODE_RIGHT])
std::cout << "Right key";
}
You'll need to create a window with SDL_SetVideoMode to get mouse and keyboard events.
Here is my InputManager update function that updates the user input from the keyboard and mouse. Notice the const_cast needed to be able to update the class variable Uint8* keysArray;. This way, more than one event can be processed at once.
void update() {
SDL_PumpEvents();
// update keyboard state
keysArray = const_cast <Uint8*> (SDL_GetKeyboardState(NULL));
if (keysArray[SDL_SCANCODE_RETURN])
printf("MESSAGE: <RETURN> is pressed...\n");
if (keysArray[SDL_SCANCODE_RIGHT] && keysArray[SDL_SCANCODE_UP])
printf("MESSAGE: Right and Up arrows are pressed...\n");
// update mouse location and button states
SDL_GetMouseState(&x, &y);
getMouseButtonStates();
}

Why this code is skipping points while moving the mouse fast?

I started learning the SDL library yesterday and, after some reading and asking, made a very simple program that draws a block when the left button is down.
The problem is that it skips points when the mouse is moved fast, so you get a bunch of squares instead of a line, the following screenshot shows one line made moving the mouse with regular speed and one while moving it fast:
What is causing it to skip all those points?
Here's the code:
//keep the window open
while(running){
//handle events
while(SDL_PollEvent(&event)){
switch(event.type){
case SDL_MOUSEBUTTONDOWN:
//left button down draws black block
if(event.button.button == SDL_BUTTON_LEFT) boxColor = black;
//right button "erases" a point
else
if(event.button.button == SDL_BUTTON_RIGHT) boxColor = blue;
//middle button clears the screen
else {
clearScreen(display,blue);
break;
}
//where to draw
drawing = 1;
boxRect.x = event.button.x - BOX_WIDTH / 2;
boxRect.y = event.button.y - BOX_HEIGHT / 2;
break;
case SDL_MOUSEMOTION:
//keep drawing if the button is pressed
if(drawing == 1){
boxRect.x = event.motion.x - BOX_WIDTH / 2;
boxRect.y = event.motion.y - BOX_HEIGHT / 2;
}
break;
//stop drawing when the button is no longer pressed
case SDL_MOUSEBUTTONUP:
drawing = 0;
break;
//quit if window closing button is pressed
case SDL_QUIT:
running = 0;
break;
}
}
//draw
if(drawing == 1){
SDL_FillRect(display,&boxRect,boxColor);
SDL_Flip(display);
}
}
Because the system doesn't actually get the points as a continuous stream, it has to poll for the mouse position. This means that if you move the mouse too fast, the difference between two polls will be big enough for there to be a gap.

Set a switch on until a flag is reached and then set the switch to off

What would be the best and easiest(simplest) way to do this..?
I am writing code in C or Capel (Vector CANoe)
I have a switch on a panel which I have designed in Vector CANoe , turns a motor on and off (0 == off and 1 == on) and I want to turn the motor off when it reaches its limit .. to stop the motor burning out ! (we know it has reached its limit when the a sensor says that the limit is reached (sensor = 1 for limit reached and 0 for not reached)
How would I do this?
i want to do something like this
WHILE (clutchMotorPower==1 & sensorClutchDisengaged !=1)
break;
else clutchMotorPower==0
WHILE (clutchMotorPower==1 & sensorClutchEngaged !=1)
break;
else clutchMotorPower==0
I don't know much about the system you're working on, so this is going to be some bare-bones code. Anyway, let's go through this step-by-step.
Assumptions:
button is a variable that is 1 if the button is currently pressed and 0 otherwise.
motor is a variable that we set to 1 to turn the motor on, or 0 to turn it off.
sensor is a variable that is 1 when the sensor is activated, or 0 otherwise
First, we need code to toggle the motor state when the button is pressed. (Assume all code samples are inside a function called from your main loop)
//values of static variables are preserved between function calls
static char bActivateMotor = 0; //1 if we should activate motor, 0 otherwise
static char bButtonHeld = 0; //1 if button was pressed last loop
//(to differentiate between button being held and it being released and pressed again)
if(!button) {
bButtonHeld = 0; //if button is not being pressed, it can't be being held down.
}
if(!bActivateMotor) { //we aren't running the motor
if(button && !bButtonHeld) { //button was pressed this loop (not held from previous loop)
bButtonHeld = 1; //Don't toggle motor state next loop just because button was held down
bActivateMotor = 1; //we should be running the motor
}
else {
motor = 0;
}
}
if(bActivateMotor) { //not else because could be activated this loop
if(button && !bButtonHeld) { //button toggles motor; if we're running, stop.
bButtonHeld = 1;
bActivateMotor = 0;
}
else {
motor = 1;
}
}
Now, the next part is to stop the motor when the sensor has activated:
//values of static variables are preserved between function calls
static char bActivateMotor = 0; //1 if we should activate motor, 0 otherwise
static char bButtonHeld = 0; //1 if button was pressed last loop
//(to differentiate between button being held and it being released and pressed again)
if(!button) {
bButtonHeld = 0; //if button is not being pressed, it can't be being held down.
}
if(!bActivateMotor) { //we aren't running the motor
if(button && !bButtonHeld) { //button was pressed this loop (not held from previous loop)
bButtonHeld = 1; //Don't toggle motor state next loop just because button was held down
bActivateMotor = 1; //we should be running the motor
}
else {
motor = 0;
}
}
if(bActivateMotor) { //not else because could be activated this loop
if(button && !bButtonHeld) { //button toggles motor; if we're running, stop.
bButtonHeld = 1;
bActivateMotor = 0;
}
/////////////////////
// Added Code Here //
/////////////////////
else if(sensor) {
bActivateMotor = 0; //motor will shut off next loop
}
else {
motor = 1;
}
}
With the lack of specifics about your code, it's hard to figure out exactly what difficulties you might be having, but hopefully the algorithm above will be a good starting point.

Resources