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

Author Topic: laser event not working  (Read 2030 times)

0 Members and 1 Guest are viewing this topic.

killer13666

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
laser event not working
« on: November 03, 2014, 01:53:44 am »
I am trying to make a Space Invaders game. When you press the Space key, it is supposed to make a laser spawn in front of the ship and go up, but it only works the first time, then not again. It is not finished yet. If someone could please tell me what is wrong, I will appreciate it.

#include <SFML/Graphics.hpp>

int main()
{
        using namespace sf;

    Sprite enemyShips [12];
    int isEnemyLiving [12];
    Sprite enemyLasers [12];
    int isEnemyLaserActive [12];
    Sprite playerLasers [12];
    int isPlayerLaserActive [12];
    int playerPosition = 370;
    int laserInactive = 0;

    for (int firstSet=0; firstSet<12; firstSet++)
        {
                isEnemyLaserActive [firstSet] = 0;
                isEnemyLiving [firstSet] = 0;
                isPlayerLaserActive [firstSet] = 0;
        }

    // Create the main window
    RenderWindow spaceInvaders(VideoMode(800, 660), "Space Invaders");

    Texture removeEntity;
    if (!removeEntity.loadFromFile("empty.png"))
    {
        return EXIT_FAILURE;
    }

    Texture enemyShip;
    if (!enemyShip.loadFromFile("enemyShip.png"))
        {
                return EXIT_FAILURE;
        }

        Texture boom;
        if (!boom.loadFromFile("boom.png"))
        {
                return EXIT_FAILURE;
        }

        Texture laser;
        if (!laser.loadFromFile("laser.png"))
        {
                return EXIT_FAILURE;
        }

        Texture spaceShip;
        if (!spaceShip.loadFromFile("spaceShip.png"))
        {
                return EXIT_FAILURE;
        }

    Sprite player;
    player.setTexture(spaceShip);
    player.setPosition(370, 600);

        // Start the game loop
    while (spaceInvaders.isOpen())
        {
                // Process events
        Event isWindowClosed;
        while (spaceInvaders.pollEvent(isWindowClosed))
                {
                        // Close window : exit
            if (isWindowClosed.type == Event::Closed)
                spaceInvaders.close();
        }

        if (playerPosition>=0)
                {
                                if (Keyboard::isKeyPressed(Keyboard::Left))
                                {
                                        player.move(-1,0);
                                        playerPosition--;
                                }
                }

                if (playerPosition<=760)
                {
                        if (Keyboard::isKeyPressed(Keyboard::Right))
                        {
                                player.move(1,0);
                                playerPosition++;
                        }
                }

                if (Keyboard::isKeyPressed(Keyboard::Space))
                {
            for (int playerLaserChecker=0; playerLaserChecker<12; playerLaserChecker++)
                        {
                                if (laserInactive==0)
                                {
                                        if (isPlayerLaserActive [playerLaserChecker]==0)
                                        {
                                                playerLasers [playerLaserChecker].setTexture(laser);
                                                isPlayerLaserActive [playerLaserChecker] = 1;
                                                playerLasers [playerLaserChecker].setPosition(playerPosition+20, 540);
                                                laserInactive = 1;
                                        }
                                }
                        }
                }

                laserInactive = 0;

                for (int laserMover=0; laserMover<12; laserMover++)
                {
                        if (isPlayerLaserActive [laserMover]==true)
                        {
                                playerLasers [laserMover].move(0,-1);
                        }
                }

        // Clear screen
        spaceInvaders.clear();

                // Draw the sprite
        spaceInvaders.draw(player);

        for (int laserDraw=0; laserDraw<12; laserDraw++)
        {
                spaceInvaders.draw(playerLasers[laserDraw]);
        }

                // Update the window
        spaceInvaders.display();
    }

    return EXIT_SUCCESS;
}
 

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: laser event not working
« Reply #1 on: November 03, 2014, 02:54:28 am »
Have you tried putting it through a debugger and going step by step through the space press? Learn to debug, it'll save you time, and a lot of it.

My guess would be: if (isPlayerLaserActive [playerLaserChecker]==0) These never get reset back to 0 after a space press?

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: laser event not working
« Reply #2 on: November 03, 2014, 08:32:40 am »
You're probably filling up your array of 12 lasers so fast that it seems you're firing only one.
And since it's never reset, it stays full.

killer13666

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: laser event not working
« Reply #3 on: November 03, 2014, 01:59:16 pm »
That's what I was assuming, but I wasn't 100% sure. I can try making it reset and see if that helps.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: laser event not working
« Reply #4 on: November 03, 2014, 02:04:30 pm »
You should really learn to debug and find mistakes systematically. It is much more efficient than guessing ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything