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

Author Topic: only one bullet moves  (Read 1364 times)

0 Members and 1 Guest are viewing this topic.

bax533

  • Newbie
  • *
  • Posts: 2
    • View Profile
only one bullet moves
« on: August 06, 2016, 12:12:15 am »
Only the last bullet i shot has speed and move. When another bullet is shot, previous stays in the ship position please help i dont know what to do :(

#include "SFML/Graphics.hpp"
#include "bits/stdc++.h"
#include "bullet.hpp"

using namespace sf;
const float PI=3.141592654;
float DEGTORAD = 0.017453f;
const int W=1200, H=800;

int main()
{
       
        RenderWindow Window( VideoMode( W, H ), "app!" );

        Texture shipTex, bckTex, bulletTex;
        Sprite  shipImage, bckImage, cel;
        Bullet bul;
        std::vector<Bullet> bullets;
        std::vector<Bullet>::iterator it;      


if(!shipTex.loadFromFile("images/spaceship.png") or !bckTex.loadFromFile("images/background.jpg")
or !bulletTex.loadFromFile("images/bullet.png"))
                std::cout<<"nope\n";
   
        bul.zrob();
    Window.setVerticalSyncEnabled(1);
    Window.setFramerateLimit(60);
       
        shipImage.setTexture(shipTex); bckImage.setTexture(bckTex); cel.setTexture(bulletTex);
        shipImage.setTextureRect(IntRect(40,0,40,40));
        shipImage.setPosition(200, 200);       

        shipImage.setOrigin(20,20);
       
//////DEKLARACJE//////

        float x=300, y=300, angle=0, dx=0, dy=0;
        const int maxSpeed=15;
        float xcel=300, ycel=300;
        float distance=0;
        float vX=0, vY=0;
        float a=0; int r=50;

        while(Window.isOpen())/////////////////////////////////////////////////////
        {
                Event Event;
                while(Window.pollEvent(Event))
                {
                        switch(Event.type)
                        {
                                case Event::Closed:
                                        Window.close();
                                        break;
                        }

                }
///DeklaracjeWP&#281;tli///
        int num=0;
        bool thrust=0, left=0, right=0, strzal=0;
        int x0=shipImage.getPosition().x, y0=shipImage.getPosition().y;
//////KLAWIATURA//////////

        if(Keyboard::isKeyPressed(Keyboard::Right)) {angle+=3;right=1;}
        if(Keyboard::isKeyPressed(Keyboard::Space))
        {
                strzal=1;
                bul.obraz.setPosition(shipImage.getPosition());
                bullets.push_back(bul);
               
                bullets[num].obraz.setPosition(shipImage.getPosition());
                bullets[num].speed.x=cel.getPosition().x-shipImage.getPosition().x;
                bullets[num].speed.y=cel.getPosition().y-shipImage.getPosition().y;
                num++;
               
        }
        if(Keyboard::isKeyPressed(Keyboard::Left))  {angle-=3;left=1;}
        if(Keyboard::isKeyPressed(Keyboard::Up))    {thrust=true;}
        else                                                                    thrust=false;
       

//////ANIMACJE//////
        if(left)shipImage.setTextureRect(IntRect(0,0,37,40));
        else if(right)shipImage.setTextureRect(IntRect(80,0,40,40));
        else if(Event::KeyReleased)shipImage.setTextureRect(IntRect(40,0,40,40));
       


//////PORUSZANIE/////
       
        if(thrust)
        {              
        dx+=cos(angle*DEGTORAD)*0.2;
        dy+=sin(angle*DEGTORAD)*0.2;
        }
        else
        {dx*=0.99;
         dy*=0.99;}
       

        float speed =sqrt(dx*dx+dy*dy);
        if(x>W){x=0;}  if(x<0){x=W;}
        if(y>H){y=0;}  if(y<0){y=H;}
        if(speed>maxSpeed)
        {dx*=maxSpeed/speed;
         dy*=maxSpeed/speed;}

    x+=dx;
        y+=dy;
//////buleta ==o




        if(left)a-=0.0525f;
        if(right)a+=0.0525f;
       
        xcel=cos(a)*r+x0;
        ycel=sin(a)*r+y0;

        /////////DRAW/////////

        shipImage.setPosition(x, y);
        shipImage.setRotation(angle+90);
       
        cel.setPosition(xcel, ycel);
        cel.setRotation(shipImage.getRotation());      

       
        Window.draw(bckImage);Window.draw(shipImage);//Window.draw(cel);

        for(int i=0; i<bullets.size(); i++)
        {
                bullets[i].obraz.move(bullets[i].speed.x/2, bullets[i].speed.y/2);

                if(bullets[i].pozycja().x<=W && bullets[i].pozycja().x>=0 && bullets[i].pozycja().y>0 && bullets[i].pozycja().y<=H )
                Window.draw(bullets[i].obraz);

        }
       
        Window.display();
        Window.clear();
               

        }
       
}
« Last Edit: August 06, 2016, 12:29:50 am by bax533 »

Tigre Pablito

  • Full Member
  • ***
  • Posts: 226
    • View Profile
    • Email
Re: only one bullet moves
« Reply #1 on: August 06, 2016, 02:54:41 am »
Hi

I tried to understand your code, but since it is in C++ (i use C#) and there are some parts that are not in English, i couldn't

I think that perhaps if you define a class for every object that is in the game, it would be less probable that your error occurs

I made a simple sample of what you are needing, Object Oriented, but in C# ... i hope you or someone can translate it to C++ ...

https://www.dropbox.com/s/ao2kcddbsv418tx/SpaceShips.rar?dl=0

Hope this helps

Hapax

  • Hero Member
  • *****
  • Posts: 3370
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: only one bullet moves
« Reply #2 on: August 06, 2016, 12:44:41 pm »
Not fully sure what you're tracking with "num" but you increase it after setting bullet speeds so you may be setting the wrong one.
You could try using .back() to modify the last element:
        bullets.back().obraz.setPosition(shipImage.getPosition());
        bullets.back().speed.x=cel.getPosition().x-shipImage.getPosition().x;
        bullets.back().speed.y=cel.getPosition().y-shipImage.getPosition().y;

You don't need to set its position twice, either ;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*