• SFML Bullets Programming.
    37 replies, posted
Another dumb question probably, go ahead and rate me boxes. How would I go to create bullets? No need for a clip or anything yet, right now I just want to shoot bullets indefinitely so I can make it work. Also, please use some code examples, but don't post the solution or anything so I have to figure it out on my own.
What do you want? A specific example or a example that will slot into an "engine"
You would use classes.
Well to start, you need a cool-down variable so they don't spam out. Then you will need some kind of direction velocity. And of course, you would need to create the bullet and when it goes off screen destroy it, or use a lifetime variable (Bullet destroys after x amount of seconds).
[QUOTE=DarkSpirit05er;17213260]Another dumb question probably, go ahead and rate me boxes. How would I go to create bullets? No need for a clip or anything yet, right now I just want to shoot bullets indefinitely so I can make it work. Also, please use some code examples, but don't post the solution or anything so I have to figure it out on my own.[/QUOTE] I'm not going to post any code as an example, but think about what you need to do.. You need your player object or something that will fire the bullets or at least be the origin and direction. You need a bullet class or a projectile class in order to store the information on each bullet. You also need to create multiple instances of a bullet. Now think for a minute on how one goes about making a new object. I suck at this kind of thing though, so I probably did that badly.
You don't need a cool-down variable if you want to fill 'em with lead. I'm making a top down and I'm having them just spill out and destroy upon leaving the screen.
1. Make bullet image (small) 2. Load the bullet into an sf::image [url]http://www.sfml-dev.org/tutorials/1.5/graphics-sprite.php[/url] 3. Using the same link as above for sf::sprite, use a container of some sort (like a std::vector or std::list) to store multiple instances of an sf::sprite (add and remove them as you need to) with the bullet sf::image as their image. 4. Move the bullet sprite in the direction the player is facing. 5. PROFIT!!!//??1!?1!! also, I cap the framerate to 30 when I use sfml so I don't have to deal with timing and things.
[QUOTE=Jallen;17215102]1. Make bullet image (small) 2. Load the bullet into an sf::image [url]http://www.sfml-dev.org/tutorials/1.5/graphics-sprite.php[/url] 3. Using the same link as above for sf::sprite, use a container of some sort (like a std::vector or std::list) to store multiple instances of an sf::sprite (add and remove them as you need to) with the bullet sf::image as their image. 4. Move the bullet sprite in the direction the player is facing. 5. PROFIT!!!//??1!?1!! also, I cap the framerate to 30 when I use sfml so I don't have to deal with timing and things.[/QUOTE] This but you might want to make a bullet class depending on what your doing. And then store instances of the bullet class in the Vector or List.
Thanks people.
How would one do a "tracer" like in source?
[QUOTE=conman420;17217711]How would one do a "tracer" like in source?[/QUOTE] Personal I would have the bullet class draw a line (for a simple tracer) from it's coordinates to the point of position minus velocity. That way faster bullets will have a longer tracer than slower bullets. Also with SFML you could use a polygon with only 2 points, and add the color arguments to have an orangy/yellowy color at the front fading out to white behind it(and maybe add a bit of alpha to make the very end fade into the background).
[cpp] sf::Image BulletI; // Line 6 BulletI.LoadFromFile("bullet.png"); // Line 7 sf::Sprite Bullet(BulletI); // Line 8 [/cpp] I get 1>c:\users\darkspirit\documents\visual studio 2008\projects\zombie\zombie\main.cpp(7) : error C2143: syntax error : missing ';' before '.' 1>c:\users\darkspirit\documents\visual studio 2008\projects\zombie\zombie\main.cpp(7) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1>c:\users\darkspirit\documents\visual studio 2008\projects\zombie\zombie\main.cpp(7) : error C2371: 'BulletI' : redefinition; different basic types 1> c:\users\darkspirit\documents\visual studio 2008\projects\zombie\zombie\main.cpp(6) : see declaration of 'BulletI' What the hell is wrong with that?
Seems like you're missing an include.
#include <SFML/Graphics.hpp>
[cpp] #include <SFML/Graphics.hpp> #include <SFML/Audio.hpp> [/cpp] My includes.
Strange, you got it setup properly? Don't know what IDE/compiler you use but just look at the getting started pages [url]http://www.sfml-dev.org/tutorials/1.5/index.php[/url]
Maybe you have a syntax error before that snippet.
Now the program freezes when I press Mouse1 to fire.
[QUOTE=DarkSpirit05er;17226713]Now the program freezes when I press Mouse1 to fire.[/QUOTE] That's nice to know.
[QUOTE=DarkSpirit05er;17226713]Now the program freezes when I press Mouse1 to fire.[/QUOTE] You have to show us the code for when you press Mouse1...
Sorry about that. :P [cpp] if (Window.GetInput().IsMouseButtonDown(sf::Mouse::Left)) BulletDef(Player,Bullet,1); [/cpp] BulletDef: [cpp] void BulletDef(sf::Sprite Reference,sf::Sprite Target, float pu) { float TargetRotation = Reference.GetRotation(); Target.SetRotation(static_cast<float>(fmod(TargetRotation, 360))); float BulletRotation = Target.GetRotation(); if (BulletRotation < 0) Target.SetRotation(BulletRotation + 360.f); float BulletX = Target.GetPosition().x; float BulletY = Target.GetPosition().y; for (int n = 1; n == 1;) { Target.SetX(BulletX + pu); Target.SetY(BulletY + pu); } } [/cpp]
[cpp] for (int n = 1; n == 1;) { Target.SetX(BulletX + pu); Target.SetY(BulletY + pu); }[/cpp] uhh...
So, what should I replace it with? I tried making it so the bullet would be continuously moving, therefor an infinite loop. But now I realize how dumb that was.
[QUOTE=Jallen;17215102] also, I cap the framerate to 30 when I use sfml so I don't have to deal with timing and things.[/QUOTE] What if it drops below 30?
[QUOTE=Sporbie;17229419]What if it drops below 30?[/QUOTE] Then they need a new computer.
[QUOTE=DarkSpirit05er;17229411]So, what should I replace it with? I tried making it so the bullet would be continuously moving, therefor an infinite loop. But now I realize how dumb that was.[/QUOTE] Create a bullet-class and have a think-function in it. This function will be called in the game-loop.
Do I define the bullet's Image and Sprite in the class?
[QUOTE=DarkSpirit05er;17229547]Do I define the bullet's Image and Sprite in the class?[/QUOTE] Yeah, but make sure the image is a pointer, so you don't make unnecessary copies of it.
Seriously: 1>c:\users\darkspirit\documents\visual studio 2008\projects\zombie\zombie\projectile.h(7) : error C2143: syntax error : missing ';' before '.' 1>c:\users\darkspirit\documents\visual studio 2008\projects\zombie\zombie\projectile.h(7) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1>c:\users\darkspirit\documents\visual studio 2008\projects\zombie\zombie\projectile.h(7) : error C2238: unexpected token(s) preceding ';' 1>c:\users\darkspirit\documents\visual studio 2008\projects\zombie\zombie\projectile.h(8) : error C2061: syntax error : identifier 'BulletImage' projectile.h [cpp] #include <SFML/Graphics.hpp> class Projectile { public: sf::Image BulletImage; BulletImage.LoadFromFile("bullet.png"); sf::Sprite Bullet(BulletImage); public: ~Projectile(void); void SetDirection(float); void SetSpeed(float); void Think(float); }; [/cpp] [editline]07:34PM[/editline] missing ; before .? That doesn't make any sense.
[QUOTE=DarkSpirit05er;17230246] [cpp] BulletImage.LoadFromFile("bullet.png"); [/cpp][/QUOTE] This line of code needs to be inside a method, not a class definition.
Sorry, you need to Log In to post a reply to this thread.