image is not loading

Started by
1 comment, last by MrDarkKnight 12 years, 3 months ago
Hello everyone
I have a Tower class and I have a TowerManager class. The Tower class create only one tower and the TowerManager class manager and store all the towers in a vector.

now when the player click the mouse a new tower will be added to the vector in TowerManager class and it will be drawn on the screen. now everything is working perfectly, the tower get created and start killing the enemys but there is only one problem, the tower image and the bullet image can not be loaded. I think this problem is beacuse when i do this
for(int i=0; i<Towers.size(); i++)
Towers->Load();

the Tower.size() is always at 0 and its not being updated beacuse its outside the game loop. but i don't know how to fix it.


#pragma once
#include <SFML\Graphics.hpp>
#include <iostream>
#include <vector>
#include "Player.h"
#include "MultipleEnemies.h"
#include "Collision.h"

class Tower
{
public:
Tower(const char* towerImage, sf::Vector2<float> Position, int damage, float range, int firingDelayIn_ms, float bulletSpeed, int cost, int refundAmount, Player* InputPlayer);
~Tower();
void Load();
void Init(sf::RenderWindow &Window);
bool IsInRange(int n, MultipleEnemies &Enemies);
void Shoot(sf::RenderWindow &Window, MultipleEnemies &Enemies);
void Draw(sf::RenderWindow &Window);

private:
Player* TowerPlayer;
Collision Collision;
Bullet Bullet;
int Damage, FiringDelayIn_ms, Cost, RefundAmount, CurrentTarget;
float Center, Radius, ElapsedTime, TimeIn_ms;
const char* ImageFile;
sf::Vector2<float> TowerPosition;
sf::Image Image;
sf::Sprite Sprite;
sf::Shape Circle;
sf::Clock Clock;
};




#include "Tower.h"
Tower::Tower(const char* towerImage, sf::Vector2<float> Position, int damage, float range, int firingDelayIn_ms, float bulletSpeed, int cost, int refundAmount, Player* InputPlayer)
{
ImageFile = towerImage;
TowerPosition.x = Position.x/32;
TowerPosition.y = Position.y/32;
Damage = damage;
Radius = range;
FiringDelayIn_ms = firingDelayIn_ms;
Cost = cost;
RefundAmount = refundAmount;
TowerPlayer = InputPlayer;
Bullet.Speed = bulletSpeed;
Bullet.NextBullet = 0;
Bullet.AngleRadian = 0;
CurrentTarget = 0;
}
Tower::~Tower()
{
}
void Tower::Load()
{
Image.LoadFromFile(ImageFile);
Sprite.SetImage(Image);
Image.SetSmooth(false);
Bullet.Image.LoadFromFile("Data/Images/Bullet.png");
for(unsigned int i = 0; i<100-1; i++)
Bullet.Sprite.SetImage(Bullet.Image);
Center = Sprite.GetSubRect().GetWidth()/2.0f;
}
...



#pragma once
#include "Tower.h"
#include "Player.h"
class ManageTowers
{
public:
ManageTowers();
~ManageTowers();
void Init(sf::RenderWindow &Window, MultipleEnemies &Enemies);
void AddTower(Player &Player);
void Events(sf::Event &Event, Player &Player);
void Draw(sf::RenderWindow &Window);
std::vector<Tower*> Towers;
sf::Vector2<float> Position;
};



#include "ManageTowers.h"
ManageTowers::ManageTowers()
{
for(int i=0; i<Towers.size(); i++)
Towers->Load();
}
ManageTowers::~ManageTowers()
{
}
void ManageTowers::AddTower(Player &Player)
{
Towers.push_back(new Tower("Data/Images/Towers/TowerDemo.png", Position, 10, 100, 1000, 1000, 10, 10, &Player));
}
void ManageTowers::Events(sf::Event &Event, Player &Player)
{
if ((Event.Type == sf::Event::MouseButtonPressed) && (Event.MouseButton.Button == sf::Mouse::Left))
AddTower(Player);
}
void ManageTowers::Init(sf::RenderWindow &Window, MultipleEnemies &Enemies)
{
Position.x = Window.GetInput().GetMouseX()*32;
Position.y = Window.GetInput().GetMouseY()*32;
for(int i=0; i<Towers.size(); i++)
{
Towers->Init(Window);
Towers->Shoot(Window, Enemies);
}
}
void ManageTowers::Draw(sf::RenderWindow &Window)
{
for(int i=0; i<Towers.size(); i++)
Towers->Draw(Window);
}


this is what should happen
[media]
[/media]

and insted this is what happening
[media]
[/media]

sorry for the long code
Advertisement
2 things:
Why not load the image in the Tower's constructor? You have the file already, just do it there. Or, you could do this:

void ManageTowers::AddTower(Player &Player)
{
Tower *NewTower = new Tower("Data/Images/Towers/TowerDemo.png", Position, 10, 100, 1000, 1000, 10, 10, &Player)

NewTower.Load();
Towers.push_back(NewTower );
}


Be Aware, you are continually loading new sf::Image's, which isn't good practice. You should instead use an image Manager. the SFML Graphics tutorials goes over this.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)


2 things:
Why not load the image in the Tower's constructor? You have the file already, just do it there. Or, you could do this:

void ManageTowers::AddTower(Player &Player)
{
Tower *NewTower = new Tower("Data/Images/Towers/TowerDemo.png", Position, 10, 100, 1000, 1000, 10, 10, &Player)

NewTower.Load();
Towers.push_back(NewTower );
}


Be Aware, you are continually loading new sf::Image's, which isn't good practice. You should instead use an image Manager. the SFML Graphics tutorials goes over this.


thanks.
I tried to load the image in the constactor but for some reason when I do, nothing works at all. If i click the mouse to add new tower nothing showes up and nothing work.
I also tried the way you did it but also nothing showes up and nothing work.

I tried for hours to fix the problem but nothing works. I even rewrote my classes but it didn't work.

[Added]
nvm I fixed it.
for some reason when I call Sprite.getsize() i get a wrong number and it was setting the tower position out of screen thats why its wasn't showing. but it was loading the image.

This topic is closed to new replies.

Advertisement