I think I understand what you're saying there, but I still don't know how to do it pratically. I (think) I fixed all the other errors now, it's just the bullet's sprites not being member variables now.
Your example didn't help that much either... I see you set the variable m_name and comment that that's how it's supposed to be done, then after that you set it again and say it'll have no effect. At least on my head it's like that.
[editline]22nd November 2010[/editline]
wtf actually yes I do, I tried modifying my code before posting that and nothing happened, just re-compiled (again of course) and it worked...
Visual Studio I hate you :saddowns:
[editline]22nd November 2010[/editline]
Wait... testing it again now, I can't assign x, y, speed, direction and rgba colors to my bullets via create_bullet....
what am I doing wrong?
[QUOTE=Samuka97;26211324]Your example didn't help that much either... I see you set the variable m_name and comment that that's how it's supposed to be done, then after that you set it again and say it'll have no effect. At least on my head it's like that.[/QUOTE]
That second assignment isn't setting the same variable again, it's setting a [i]different[/i] variable with the same name. That's the key point I was trying to illustrate: when you declare a local variable within a function, it "hides" any pre-existing variable with the same name that comes from outside the function (e.g. a global or class member variable).
The first assignment to m_name sets the string stored in the Foo instance.
The second assignment sets a temporary local variable that has nothing to do with the Foo instance.
[QUOTE=Samuka97;26211324]Wait... testing it again now, I can't assign x, y, speed, direction and rgba colors to my bullets via create_bullet....[/QUOTE]
What does your Bullet constructor look like now?
cpp:
[cpp]#include "Bullet.hpp"
#include <cmath>
#include <SFML/Graphics.hpp>
const double pi = 3.1415;
Bullet::Bullet(sf::Image &BulletImage, sf::Image &BulletBackImage, double x, double y, double speed, double direction, int color_r, int color_g, int color_b, int color_a) {
// Create a sprite for the bullet's image.
BulletSpr.SetImage(BulletImage);
BulletBackSpr.SetImage(BulletBackImage);
// Set up some properties for it.
BulletSpr.SetCenter(14.f, 14.f);
BulletBackSpr.SetCenter(8.f, 8.f);
// Our variables
x = x;
y = y;
speed = speed;
direction = direction;
color_r = color_r;
color_g = color_g;
color_b = color_b;
color_a = color_a;
}
Bullet::~Bullet () {
}
void Bullet::update(sf::RenderWindow &target) {
x = x;
y = y;
speed = speed;
direction = direction;
color_r = color_r;
color_g = color_g;
color_b = color_b;
color_a = color_a;
// Color the bullet
BulletSpr.SetColor(sf::Color(color_r, color_g, color_b, color_a));
// Calculate it's trajectory
//x = x + speed * cos(direction*pi/180);
//y = y - speed * sin(direction*pi/180);
x = 100;
y = x;
// Move it's sprite
BulletSpr.SetPosition(float(x), float(y));
BulletBackSpr.SetPosition(float(x), float(y));
// And finally, draw it.
//target.Draw(BulletBackSpr);
target.Draw(BulletSpr);
}[/cpp]
hpp:
[cpp]#ifndef BULLET_H //usually something like FILE_NAME_H
#define BULLET_H
#pragma once
#include <SFML/Graphics.hpp>
//---------------------------------------------------------------------------
class Bullet {
public:
double x, y, speed, direction;
int color_r, color_g, color_b, color_a;
void update(sf::RenderWindow &target);
Bullet (sf::Image &BulletImage, sf::Image &BulletBackImage, double x, double y, double speed, double direction, int color_r, int color_g, int color_b, int color_a);
~Bullet();
private:
sf::Sprite BulletSpr;
sf::Sprite BulletBackSpr;
};
//---------------------------------------------------------------------------
#endif[/cpp]
Unless I set the bullet's position to like, 100 in it's update function, it doesn't appear. Also, no matter what, creating the bullet using RGBA colors 255, 0, 0 and 255 doesn't let me color it. Everytime I run the game now, it's a slightly different color, that ranges from dark blue to violet.
errr...
[b]Edit:[/b] post below fixed it
OK, look, one thing he's been consistently trying to tell you, is that:
[cpp]
// Our variables
x = x;
y = y;
speed = speed;
direction = direction;
color_r = color_r;
color_g = color_g;
color_b = color_b;
color_a = color_a;
[/cpp]
Is complete nonsense. To avoid setting a local variable to itself like this, you need to disambiguate the variable name:
[cpp]
// Our variables
this->x = x;
this->y = y;
this->speed = speed;
this->direction = direction;
this->color_r = color_r;
this->color_g = color_g;
this->color_b = color_b;
this->color_a = color_a;
[/cpp]
It's also been said that this pattern is best replaced with an [I]initializer list[/I]:
[cpp]
Bullet (sf::Image &BulletImage, sf::Image &BulletBackImage, double x, double y, double speed, double direction, int color_r, int color_g, int color_b, int color_a) : x(x), y(y), speed(speed), direction(direction), color_r(color_r), color_g(color_g), color_b(color_b)
{
/* snip */
}
[/cpp]
(Notice that there is no ambiguity here because only member fields can be initialized in an initializer list)
This is not all he's been trying to tell you, but I think it's a good idea to put this here as clear as possible.
Huh. I had tried that before (before even the crash started happening), but since it didn't do anything, at least in my eyes, I took it out and left it as-is. That fixed it, thanks!
[IMG]http://i225.photobucket.com/albums/dd159/samuka97/BulletFusionC_MultipleBullets.png[/IMG]
I'll now try and start implementing enemy ships, and after that, level loading from a file. But wow, 3 pages just for this. But first, I have my final tests this week. Thanks Facepunch :3
[QUOTE=Samuka97;26221587]But wow, 3 pages just for this.[/QUOTE]
Looking at your code snippets from the first page compared to the more recent ones, I can't say this has been a waste of time :smile:
[QUOTE=Samuka97;26221587]That fixed it, thanks![/QUOTE]
I'm glad you got it working, but more importantly, do you understand [i]why[/i] that last change made it work?
If not, I'd recommend you think about it (and possibly ask more questions) until you do understand, because the experience and knowledge that you gain from solving problems like this are what make you a better programmer, which is more important (IMO) than this particular project you're working on at the moment.
Actually, yes. I can see the mistakes I was making, but I only got to see them after I got them fixed, because while my code wasn't working It's not like I knew what I could possibly be doing wrong if I'm new to C++.
Sorry, you need to Log In to post a reply to this thread.