Original Post
Hello
I have this code where if you have 10 gold and press on the game screen a tower will be drawn in that position. Now i want to be able to click on each tower to do a specific task.
for example if you click on one of the towers the word "coco" will be printed on the screen. now my problem is that i can only click on the last tower that was create and it will display "coco". If i click on any other tower the word "coco" doesn't print out, only if i click on the last tower that was create the word "coco" get print out.
can you guys please help me out. I'm new to the whole victor thing and i'm still trying to understand how it works.
this is the part of code where if you click on the tower prints out "coco"
tower.cpp
tower.h
I have this code where if you have 10 gold and press on the game screen a tower will be drawn in that position. Now i want to be able to click on each tower to do a specific task.
for example if you click on one of the towers the word "coco" will be printed on the screen. now my problem is that i can only click on the last tower that was create and it will display "coco". If i click on any other tower the word "coco" doesn't print out, only if i click on the last tower that was create the word "coco" get print out.
can you guys please help me out. I'm new to the whole victor thing and i'm still trying to understand how it works.
this is the part of code where if you click on the tower prints out "coco"
void tower::MouseEventReleased(sf::Event &Event)
{
for(int ButtonX=ImageX; ButtonX<=ImageX + ImageSizeX; ButtonX++)
{
for(int ButtonY=ImageY; ButtonY<=ImageY + ImageSizeY; ButtonY++)
{
if(MouseX==ButtonX && MouseY==ButtonY && Event.Type == sf::Event::MouseButtonPressed && Event.MouseButton.Button == sf::Mouse::Left)
{
std::cout<<"coco";
}
}
}
}
tower.cpp
#include "tower.h"
#include "economy.h"
economy eco;
tower::tower(float x, float y)
{
Position = sf::Vector2f(x,y);
TowerImage.LoadFromFile("hero.png");
}
tower::~tower(void)
{
}
void tower::init(sf::RenderWindow &window)
{
TowerSprite.SetImage(TowerImage);
MouseX = window.GetInput().GetMouseX();
MouseY = window.GetInput().GetMouseY();
for(int i = 0; i <TowerVector.size(); i++)
{
ImageX = TowerVector->Position.x;
ImageY = TowerVector->Position.y;
}
ImageSizeX = TowerSprite.GetSize().x;
ImageSizeY = TowerSprite.GetSize().y;
}
void tower::addtower()
{
TowerVector.push_back(new tower(MouseX, MouseY));
}
void tower::eventhandle(sf::Event Event)
{
int getamount=eco.getamount();
if(Event.Type == sf::Event::KeyPressed && Event.Key.Code== sf::Key::F)
eco.deposet(1);
if(getamount>=10)
{
if(Event.Type == sf::Event::MouseButtonReleased && Event.MouseButton.Button == sf::Mouse::Left)
{
tower::addtower();
eco.widthdraw(10);
}
}
}
void tower::drawtower(sf::RenderWindow &window)
{
for(int i = 0; i <TowerVector.size(); i++)
{
// Set position to current tile, and draw it
TowerSprite.SetPosition(TowerVector->Position.x, TowerVector->Position.y);
window.Draw(TowerSprite);
}
eco.DisplayMoney(window);
}
void tower::MouseEventReleased(sf::Event &Event)
{
for(int ButtonX=ImageX; ButtonX<=ImageX + ImageSizeX; ButtonX++)
{
for(int ButtonY=ImageY; ButtonY<=ImageY + ImageSizeY; ButtonY++)
{
if(MouseX==ButtonX && MouseY==ButtonY && Event.Type == sf::Event::MouseButtonPressed && Event.MouseButton.Button == sf::Mouse::Left)
{
std::cout<<"coco";
}
}
}
}
tower.h
#include <SFML\Graphics.hpp>
#include <iostream>
class tower
{
public:
tower(float x, float y);
~tower(void);
void init(sf::RenderWindow &window);
void addtower();
void eventhandle(sf::Event Event);
void MouseEventReleased(sf::Event &Event);
void drawtower(sf::RenderWindow &window);
private:
sf::Vector2f Position;
std::vector<tower*> TowerVector;
float MouseX, MouseY;
int dmg,cost,defence,getamount;
bool DestroyTower;
sf::Image TowerImage;
sf::Sprite TowerSprite;
int ImageX, ImageY,
ImageSizeX, ImageSizeY;
};