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

Author Topic: Bullet problem  (Read 1802 times)

0 Members and 1 Guest are viewing this topic.

Fantasy

  • Newbie
  • *
  • Posts: 47
    • View Profile
    • Email
Bullet problem
« on: July 24, 2011, 02:28:30 pm »
hay guys

ok so i have made a bullet array and when i click the left button on the mouse the bullet get the player position and move from there, but the problem is whenever i click on the mouse it doesn't generate a new Bullet but it just remove the old one and get the player position again and move from there.

can you guys please help? thankx

Code: [Select]

#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
   sf::RenderWindow Window(sf::VideoMode(800, 600, 32), "Zombie Game Alpha");

   sf::Image PlayerImage, BulletImage;
   sf::Sprite PlayerSprite, BulletSprite[10];

   PlayerImage.LoadFromFile("player.png");
   BulletImage.LoadFromFile("bullet.png");

   PlayerSprite.SetImage(PlayerImage);

for(int x=0; x<=9; x++)
BulletSprite[x].SetImage(BulletImage);

   PlayerSprite.SetPosition(200,200);

   bool Draw=false;
   int Counter=0;

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

 float ElapsedTime = Window.GetFrameTime();

 if (Window.GetInput().IsKeyDown(sf::Key::W))  PlayerSprite.Move(0, -425 * ElapsedTime);
      if (Window.GetInput().IsKeyDown(sf::Key::S))  PlayerSprite.Move(0,  425 * ElapsedTime);
      if (Window.GetInput().IsKeyDown(sf::Key::A))  PlayerSprite.Move(-425 * ElapsedTime, 0);
      if (Window.GetInput().IsKeyDown(sf::Key::D))  PlayerSprite.Move(425 * ElapsedTime, 0);
     
 while (Window.GetEvent(Event))
      {
         if (Event.Type == sf::Event::Closed)
            Window.Close();
         if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
            Window.Close();

if(Event.Type == sf::Event::MouseButtonPressed && Event.MouseButton.Button == sf::Mouse::Left)
{
Draw=true;
Counter++;
for(int x=0; x<=Counter; x++)
BulletSprite[x].SetPosition(PlayerSprite.GetPosition().x,PlayerSprite.GetPosition().y);
}

      }
 
 Window.Clear();
      Window.Draw(PlayerSprite);

 if(Draw==true)
 {
 for(int x=0; x<=Counter; x++)
 {
BulletSprite[x].Move(200 * ElapsedTime, 0);
Window.Draw(BulletSprite[x]);
 }
 std::cout << Counter;
 }
      Window.Display();
   }
}

Fantasy

  • Newbie
  • *
  • Posts: 47
    • View Profile
    • Email
Bullet problem
« Reply #1 on: July 24, 2011, 04:58:22 pm »
guys please someone help

slotdev

  • Sr. Member
  • ****
  • Posts: 385
    • View Profile
Bullet problem
« Reply #2 on: July 24, 2011, 05:14:15 pm »
Well, for one thing, your "Counter" variable will exceed the size of the BulletSprite array if you click the mouse enough times, with the result probably being unexpected behaviour/a crash.

Also you don't reset the "Draw" variable to false..........
SFML 2.1

Fantasy

  • Newbie
  • *
  • Posts: 47
    • View Profile
    • Email
Bullet problem
« Reply #3 on: July 24, 2011, 05:47:28 pm »
Quote from: "slotdev"
Well, for one thing, your "Counter" variable will exceed the size of the BulletSprite array if you click the mouse enough times, with the result probably being unexpected behaviour/a crash.

Also you don't reset the "Draw" variable to false..........

well i know everything you said but this isnt my problem, my problem is that when i press the mouse button 3 times i dont get 3 bullets, what happens is when i click the fist time the bullet show and move when i press the second and third time the first and second bullet dispersers and only the third show up at the player position and then it moves

Haikarainen

  • Guest
Bullet problem
« Reply #4 on: July 24, 2011, 06:46:16 pm »
Create a class for a bulletmanager, and then create function called "TraceBullet()" or something, wich traces the path of the bullet until it hits something, and then returns that object/position.

If you dont want instant hit, just make a std::vector<bullet> bullets; and make bullets always move in its direction until it hits something ,and when they do remove them from the vector.

 

anything