Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Incrementing a variable when pushing a button  (Read 3691 times)

0 Members and 1 Guest are viewing this topic.

Treena

  • Newbie
  • *
  • Posts: 7
    • View Profile
Incrementing a variable when pushing a button
« on: October 05, 2010, 06:33:15 am »
I'm trying to create an asteroids game clone, and I'm stuck on creating the bullets. I'm pretty sure I've isolated the problem to being when I push space and increment the variable, numberofbullets, that's when the program crashes. When I removed incrementing the variable in that if statement, the program ran fine, the only problem was I only had one bullet being drawn over and over again. Here's the code:

Code: [Select]

#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;

#include "Bullet.h"
#include "Ship.h"

int main()
{
// Create the main rendering window
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Asteroid Clone");

//Loading ship image
Ship ship1;
ship1.loadImage("ship.png");
Bullet bullet[10]; //Bullet array, will make it larger when I get bullets working

int numberofbullets = 0; // Index to count array of bullets
int i;

while(App.IsOpened())
{
sf::Event Event;

while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

if(App.GetInput().IsKeyDown(sf::Key::Up))
ship1.moveForward(App);
if(App.GetInput().IsKeyDown(sf::Key::Right))
ship1.rotateRight(App);
if(App.GetInput().IsKeyDown(sf::Key::Left))
ship1.rotateLeft(App);
if(App.GetInput().IsKeyDown(sf::Key::Space))
{
bullet[numberofbullets].SetPosition(ship1); //Set position of bullet  based on ship
numberofbullets++; // Incrementing so next time player presses space a new bullet is made
}
App.Clear(sf::Color(0, 0, 0));

//Drawing of sprites
ship1.drawShip(App);
for(i=0; i <= numberofbullets; i++) // Draws number of bullets that have been made
{
bullet[numberofbullets].draw(App);
}
if (numberofbullets == 10) // Resets array, will make larger when I get it working
{
numberofbullets = 0;
}


App.Display();

}

return EXIT_SUCCESS;
}


PeterWelzien

  • Newbie
  • *
  • Posts: 38
    • View Profile
Incrementing a variable when pushing a button
« Reply #1 on: October 05, 2010, 07:32:27 am »
The problem might be the for loop where you draw the bullets. Try changing the condition to i < numberofbullets.
/Peter Welzien

Canadadry

  • Hero Member
  • *****
  • Posts: 1081
    • View Profile
Incrementing a variable when pushing a button
« Reply #2 on: October 05, 2010, 07:49:01 am »
What's going on when you've got 11 bullet. :wink:

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
Re: Incrementing a variable when pushing a button
« Reply #3 on: October 05, 2010, 12:45:50 pm »
Code: [Select]

Bullet bullet[10]; //0 to 9, make 10 bullets
int numberofbullets = 0; // Index to count array of bullets

//Here, you are positionning a bullet
//And incrementing the counter : why not
if(App.GetInput().IsKeyDown(sf::Key::Space))
{
bullet[numberofbullets].SetPosition(ship1);
numberofbullets++;
}

//Here you are drawing from 0 to "numberofbullets" which is one over and very problematic when reaching 10 or more !!!
for(i=0; i <= numberofbullets; i++)
{
bullet[numberofbullets].draw(App);
}
//And here, you are correcting the numberofbullets too late
if (numberofbullets == 10)
{
numberofbullets = 0;
}

Try to draw on paper what you are doing. Verify and block your increment before trying to draw it. Be carful with arrays.

Quick and dirty corrections :
Code: [Select]
//Initialization
std::vector<Bullet> bullets;

//Adding a bullet
if(App.GetInput().IsKeyDown(sf::Key::Space))
{
   bullets.push_back(new Bullet(ship1.GetPosition());
}

/Drawing bullets
for (i=0;i<bullets.size();i++)
{
   App.Draw(bullets[i]);
}

Isn't it more clear like that for example ?
Mindiell
----

Treena

  • Newbie
  • *
  • Posts: 7
    • View Profile
Incrementing a variable when pushing a button
« Reply #4 on: October 05, 2010, 06:49:13 pm »
So my problem wasn't incrementing the variables, but when the bullet number got past 9, I had my checking statement too late.

And thanks for the vector suggestion Mindiell. I had only known about arrays and dynamic arrays, so after reading up a little on vectors, it seems like using them would solve my problems. I'm not at home so I can't change my program, but I'm sure that "quick fix" would fix my problem.

 

anything