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

Author Topic: [Solved] map editor problem  (Read 2650 times)

0 Members and 2 Guests are viewing this topic.

newbie123

  • Newbie
  • *
  • Posts: 27
    • View Profile
[Solved] map editor problem
« on: August 25, 2011, 11:56:40 pm »
hello everyone

[Old Post]
i'm working on a map editor and i'm having trouble placing the image on the screen by clicking on the mouse.

what i want exactly is when you left click the mouse on the screen the image will get drawn in that position and stay in that position even if you click again.

My problem is that the image keep following the mouse coordinates instead of taking the last mouse coordinates and set it position there.

can you guys please help me i have been trying to fix this for 2 days and i had no luck.

sorry for being a noob :D

[New Post]
now i have an array of sprites and i want every time you click on the mouse a sprite will get drawn in that position.

i tried the following but the sprites get drawn on top of each other every time i click the mouse button

Code: [Select]

bool DrawMe=false;
int index=0;
while(window.IsOpened())
{
sf::Event Event;
while(window.GetEvent(Event))
{
if(Event.Type == sf::Event::MouseButtonPressed && Event.MouseButton.Button == sf::Mouse::Left)
{
index++;
DrawMe=true;
}
}

float MouseX = Game.GetInput().GetMouseX();
float MouseY = Game.GetInput().GetMouseY();

//Clear
window.Clear();

//Draw

if(DrawMe==true)
      {
 for(int i=0; i<=index; i++)
herosprite[i].SetPosition(MouseX,MouseY);
         DrawMe=false;
      }

for(int i=0; i<=index; i++)
window.Draw(herosprite[i]);

//Display
window.Display();
}
return 0;
}



player931402

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
[Solved] map editor problem
« Reply #1 on: August 26, 2011, 01:32:59 am »
Your program make the boolean always true when u click the first time!


U need to switch false drawme when the mouse bott are relased..



Sorry for my bad eng >.<

newbie123

  • Newbie
  • *
  • Posts: 27
    • View Profile
[Solved] map editor problem
« Reply #2 on: August 26, 2011, 02:28:06 am »
Quote from: "player931402"
Your program make the boolean always true when u click the first time!


U need to switch false drawme when the mouse bott are relased..



Sorry for my bad eng >.<

thankx that works.

but what if i have an array of sprites and i want to place every sprite in a different position when i click the mouse how can i do that ?

newbie123

  • Newbie
  • *
  • Posts: 27
    • View Profile
[Solved] map editor problem
« Reply #3 on: August 26, 2011, 02:54:25 am »
i solveed it here is the code
Code: [Select]

      if(DrawMe==true)
      {
         towersprite.SetPosition(MouseX,MouseY);
        DrawMe=false;
      }
       window.Draw(towersprite);


but now i have an array of sprites and i want to place every sprite in a different position when i click the mouse any idea how to do that ?

the problem with this code is if i have an array of sprites and want to position them in different places that wouldn't work because every time you click on the mouse the sprite keep drawing on top of each other.

i dont know how to separate them

Haikarainen

  • Guest
[Solved] map editor problem
« Reply #4 on: August 26, 2011, 02:59:51 am »
A map editor seems a little bit.. overkill for you, as in lot of cases, i think a resourcemanager would be in place here!

However, you'll need to store mapobjects/tiles somehow, a nice method is using std::vector<customclasshere>

Ill show you on how to do it using your methods but belive me, you should learn some basics first!

https://legacy.sfmluploads.org/code/74

newbie123

  • Newbie
  • *
  • Posts: 27
    • View Profile
[Solved] map editor problem
« Reply #5 on: August 26, 2011, 03:27:47 am »
Quote from: "Haikarainen"
A map editor seems a little bit.. overkill for you, as in lot of cases, i think a resourcemanager would be in place here!

However, you'll need to store mapobjects/tiles somehow, a nice method is using std::vector<customclasshere>

Ill show you on how to do it using your methods but belive me, you should learn some basics first!

https://legacy.sfmluploads.org/code/74

thank you very much it works perfectly.
i know that i'm a newbie but i'm learning as much as i can, i never used vectors but now i'll study the code carefully and try to understand it.
thank you again :D

Haikarainen

  • Guest
[Solved] map editor problem
« Reply #6 on: August 26, 2011, 05:30:19 pm »
Quote from: "newbie123"
thank you very much it works perfectly.
i know that i'm a newbie but i'm learning as much as i can, i never used vectors but now i'll study the code carefully and try to understand it.
thank you again :D


http://www.cplusplus.com/reference/stl/vector/

See it as a dynamic array. A storage container for objects.

Code: [Select]
// Create a vector of floats
std::vector<float> Floats;
// Add a new float with value of 2.3
Floats.push_back(2.3);
// Add another float with value of 4.2
Floats.push_back(4.2);
// outputs 2.3
std::cout << Floats[0];
// outputs 4.2
std::cout << Floats[1]


this ofcourse works with all kind of types, classes, structs, enums andother objects.

EDIT: Btw the first code i posted shouldnt work perfectly, noticed some errors just now :P was very tired when i wrote it and its untested.