Right-Clicking on Hotspots Works...Almost.

Started by J.E.S., Fri 05/09/2008 05:13:59

Previous topic - Next topic

J.E.S.

Instead of displaying messages the old fashioned way, I had the idea to display a comic book style speech balloon that appear next to my characters head when a hotspot is right-clicked. Here is my code:

Code: ags
function repeatedly_execute_always()
{
  //---------------------------------------Player Dialog GUI----------------------------------
  gPlayerDialog.SetPosition(player.x-20, player.y-70);
  
  //-------------------------------Right Click Actions On Hotspots-----------------------------
  
  if ((Hotspot.GetAtScreenXY(mouse.x, mouse.y)==hotspot[1])&&(mouse.IsButtonDown(eMouseRight)))
  {
    gPlayerDialog.BackgroundGraphic=242;
    SetTimer(1, 120);
  }
  if ((Hotspot.GetAtScreenXY(mouse.x, mouse.y)==hotspot[2])&&(mouse.IsButtonDown(eMouseRight)))
  {
    gPlayerDialog.BackgroundGraphic=155;
    SetTimer(1, 120);
  }
  if ((Hotspot.GetAtScreenXY(mouse.x, mouse.y)==hotspot[3])&&(mouse.IsButtonDown(eMouseRight)))
  {
    gPlayerDialog.BackgroundGraphic=244;
    SetTimer(1, 120);
  }
  if ((Hotspot.GetAtScreenXY(mouse.x, mouse.y)==hotspot[4])&&(mouse.IsButtonDown(eMouseRight)))
  {
    gPlayerDialog.BackgroundGraphic=245;
    SetTimer(1, 120);
  }
  if ((Hotspot.GetAtScreenXY(mouse.x, mouse.y)==hotspot[5])&&(mouse.IsButtonDown(eMouseRight)))
  {
    gPlayerDialog.BackgroundGraphic=246;
    SetTimer(1, 120);
  }
  if ((Hotspot.GetAtScreenXY(mouse.x, mouse.y)==hotspot[6])&&(mouse.IsButtonDown(eMouseRight)))
  {
    gPlayerDialog.BackgroundGraphic=247;
    SetTimer(1, 120);
  }
  if ((Hotspot.GetAtScreenXY(mouse.x, mouse.y)==hotspot[7])&&(mouse.IsButtonDown(eMouseRight)))
  {
    gPlayerDialog.BackgroundGraphic=248;
    SetTimer(1, 120);
  }
  if ((Hotspot.GetAtScreenXY(mouse.x, mouse.y)==hotspot[8])&&(mouse.IsButtonDown(eMouseRight)))
  {
    gPlayerDialog.BackgroundGraphic=249;
    SetTimer(1, 120);
  }
  if ((Hotspot.GetAtScreenXY(mouse.x, mouse.y)==hotspot[9])&&(mouse.IsButtonDown(eMouseRight)))
  {
    gPlayerDialog.BackgroundGraphic=243;
    SetTimer(1, 120);
  }
  if ((Hotspot.GetAtScreenXY(mouse.x, mouse.y)==hotspot[10])&&(mouse.IsButtonDown(eMouseRight)))
  {
    gPlayerDialog.BackgroundGraphic=250;
    SetTimer(1, 120);
  }
  if ((Hotspot.GetAtScreenXY(mouse.x, mouse.y)==hotspot[11])&&(mouse.IsButtonDown(eMouseRight)))
  {
    gPlayerDialog.BackgroundGraphic=251;
    SetTimer(1, 120);
  }
  if ((Hotspot.GetAtScreenXY(mouse.x, mouse.y)==hotspot[12])&&(mouse.IsButtonDown(eMouseRight)))
  {
    gPlayerDialog.BackgroundGraphic=252;
    SetTimer(1, 120);
  }
  if (IsTimerExpired(1)==1)
  {
    gPlayerDialog.BackgroundGraphic=0;
}
}


This works like I would expect it to. However, if you just continue to hold the right mouse button down the whole time, you can move the cursor over every hotspot to reveal a speech balloon. I do not want this to happen. What I would rather do, is to somehow disable the rest of the hotspots when another hotspot is active. This way, one speech balloon would always be displayed and the player would have to right click again on a different hotspot to see its particular speech balloon. I mainly want to prevent players from holding down right-click and scrubbing the screen until they find a hotspot.

I'm familiar with:

Code: ags
hotspot[1].enabled=true/false


but I'm not quite sure how I would code the conditions to make a hotspot active again and still be able to only show one speech balloon at a time when right-click is held down. I hope this made sense, please let me know if I need to clarify. Thanks in advance.




TwinMoon

Well, if gPlayerDialog.Graphic = 0 means that the balloon isn't visible, right?

So what you do is, before the line if ((Hotspot.GetAtScreenXY(mouse.x, mouse.y)==hotspot[1])&&(mouse.IsButtonDown(eMouseRight)))

you put:

Code: ags
if (gPlayerDialog.Graphic!=0) {


and you close the bracket at the end.
If the balloon is visible, the code won't run.

Khris

Get your sprite numbers in order, then use:
Code: ags
function on_mouse_click(MouseButton button) {

  ...

  else if (button == eMouseRight) {
    Hotspot*h = Hotspot.GetAtScreenXY(mouse.x, mouse.y);
    if (h != null && !gPlayerDialog.Visible) {
    gPlayerDialog.BackgroundGraphic = h.ID + 240;             // use e.g. slots 241 - X
    SetTimer(1, 120);
    //---------------------------------------Player Dialog GUI----------------------------------
    gPlayerDialog.SetPosition(player.x-20, player.y-70);
    gPlayerDialog.Visible = true;
    bubble
  }
  
  ...
  
}

function repeatedly_execute() {
  if (IsTimerExpired(1)) {
    gPlayerDialog.Visible = false;
  }
}


Are you usig a single room? Because otherwise you'll need to distinguish between rooms using player.Room.

J.E.S.

After a few more hours, I've come to one solution, but I would like to know how someone else would have handled this problem. This code will allow a player to hold down right-click and only see one speech balloon at a time. Scrubbing the screen with the right-click button held down will only let the player see the last hotspot they clicked on. Also, I got rid of the timers so now the player can just hold down right-click on the hotspot to read the speech balloon for as long as they like, then let go when they are finished. I would have liked to been able to use an array such as:
Code: ags
 (gPlayerDialog.BackgroundGraphic!=1||2||3||4||5...etc.)

but I couldn't get that to work, maybe I'm missing something...

Anyway, here's the code:
Code: ags

function repeatedly_execute_always()
{
//---------------------------------------Player Dialog GUI----------------------------------
gPlayerDialog.SetPosition(player.x-20, player.y-70);
  
//-------------------------------Right Click Actions On Hotspots-----------------------------
if((mouse.IsButtonDown(eMouseRight))!=1)
{
gPlayerDialog.BackgroundGraphic=0;
}
if((((((((((((Hotspot.GetAtScreenXY(mouse.x, mouse.y)==hotspot[1])&&(mouse.IsButtonDown(eMouseRight))
&&(gPlayerDialog.BackgroundGraphic!=155)&&(gPlayerDialog.BackgroundGraphic!=244)&&(gPlayerDialog.BackgroundGraphic!=245)
&&(gPlayerDialog.BackgroundGraphic!=246)&&(gPlayerDialog.BackgroundGraphic!=247)&&(gPlayerDialog.BackgroundGraphic!=248)
&&(gPlayerDialog.BackgroundGraphic!=249)&&(gPlayerDialog.BackgroundGraphic!=243)&&(gPlayerDialog.BackgroundGraphic!=251)
&&(gPlayerDialog.BackgroundGraphic!=250)&&(gPlayerDialog.BackgroundGraphic!=252))))))))))))
{
gPlayerDialog.BackgroundGraphic=242;
}
if((((((((((((Hotspot.GetAtScreenXY(mouse.x, mouse.y)==hotspot[2])&&(mouse.IsButtonDown(eMouseRight))
&&(gPlayerDialog.BackgroundGraphic!=242)&&(gPlayerDialog.BackgroundGraphic!=244)&&(gPlayerDialog.BackgroundGraphic!=245)
&&(gPlayerDialog.BackgroundGraphic!=246)&&(gPlayerDialog.BackgroundGraphic!=247)&&(gPlayerDialog.BackgroundGraphic!=248)
&&(gPlayerDialog.BackgroundGraphic!=249)&&(gPlayerDialog.BackgroundGraphic!=243)&&(gPlayerDialog.BackgroundGraphic!=251)
&&(gPlayerDialog.BackgroundGraphic!=250)&&(gPlayerDialog.BackgroundGraphic!=252))))))))))))
{
gPlayerDialog.BackgroundGraphic=155;
}
if((((((((((((Hotspot.GetAtScreenXY(mouse.x, mouse.y)==hotspot[3])&&(mouse.IsButtonDown(eMouseRight))
&&(gPlayerDialog.BackgroundGraphic!=242)&&(gPlayerDialog.BackgroundGraphic!=155)&&(gPlayerDialog.BackgroundGraphic!=245)
&&(gPlayerDialog.BackgroundGraphic!=246)&&(gPlayerDialog.BackgroundGraphic!=247)&&(gPlayerDialog.BackgroundGraphic!=248)
&&(gPlayerDialog.BackgroundGraphic!=249)&&(gPlayerDialog.BackgroundGraphic!=243)&&(gPlayerDialog.BackgroundGraphic!=251)
&&(gPlayerDialog.BackgroundGraphic!=250)&&(gPlayerDialog.BackgroundGraphic!=252))))))))))))
{
gPlayerDialog.BackgroundGraphic=244;
}
if((((((((((((Hotspot.GetAtScreenXY(mouse.x, mouse.y)==hotspot[4])&&(mouse.IsButtonDown(eMouseRight))
&&(gPlayerDialog.BackgroundGraphic!=242)&&(gPlayerDialog.BackgroundGraphic!=244)&&(gPlayerDialog.BackgroundGraphic!=155)
&&(gPlayerDialog.BackgroundGraphic!=246)&&(gPlayerDialog.BackgroundGraphic!=247)&&(gPlayerDialog.BackgroundGraphic!=248)
&&(gPlayerDialog.BackgroundGraphic!=249)&&(gPlayerDialog.BackgroundGraphic!=243)&&(gPlayerDialog.BackgroundGraphic!=251)
&&(gPlayerDialog.BackgroundGraphic!=250)&&(gPlayerDialog.BackgroundGraphic!=252))))))))))))
{
gPlayerDialog.BackgroundGraphic=245;
}
if((((((((((((Hotspot.GetAtScreenXY(mouse.x, mouse.y)==hotspot[5])&&(mouse.IsButtonDown(eMouseRight))
&&(gPlayerDialog.BackgroundGraphic!=242)&&(gPlayerDialog.BackgroundGraphic!=244)&&(gPlayerDialog.BackgroundGraphic!=245)
&&(gPlayerDialog.BackgroundGraphic!=155)&&(gPlayerDialog.BackgroundGraphic!=247)&&(gPlayerDialog.BackgroundGraphic!=248)
&&(gPlayerDialog.BackgroundGraphic!=249)&&(gPlayerDialog.BackgroundGraphic!=243)&&(gPlayerDialog.BackgroundGraphic!=251)
&&(gPlayerDialog.BackgroundGraphic!=250)&&(gPlayerDialog.BackgroundGraphic!=252))))))))))))
{
gPlayerDialog.BackgroundGraphic=246;
}
if((((((((((((Hotspot.GetAtScreenXY(mouse.x, mouse.y)==hotspot[6])&&(mouse.IsButtonDown(eMouseRight))
&&(gPlayerDialog.BackgroundGraphic!=242)&&(gPlayerDialog.BackgroundGraphic!=244)&&(gPlayerDialog.BackgroundGraphic!=245)
&&(gPlayerDialog.BackgroundGraphic!=246)&&(gPlayerDialog.BackgroundGraphic!=155)&&(gPlayerDialog.BackgroundGraphic!=248)
&&(gPlayerDialog.BackgroundGraphic!=249)&&(gPlayerDialog.BackgroundGraphic!=243)&&(gPlayerDialog.BackgroundGraphic!=251)
&&(gPlayerDialog.BackgroundGraphic!=250)&&(gPlayerDialog.BackgroundGraphic!=252))))))))))))
{
gPlayerDialog.BackgroundGraphic=247;
}
if((((((((((((Hotspot.GetAtScreenXY(mouse.x, mouse.y)==hotspot[7])&&(mouse.IsButtonDown(eMouseRight))
&&(gPlayerDialog.BackgroundGraphic!=242)&&(gPlayerDialog.BackgroundGraphic!=244)&&(gPlayerDialog.BackgroundGraphic!=245)
&&(gPlayerDialog.BackgroundGraphic!=246)&&(gPlayerDialog.BackgroundGraphic!=247)&&(gPlayerDialog.BackgroundGraphic!=155)
&&(gPlayerDialog.BackgroundGraphic!=249)&&(gPlayerDialog.BackgroundGraphic!=243)&&(gPlayerDialog.BackgroundGraphic!=251)
&&(gPlayerDialog.BackgroundGraphic!=250)&&(gPlayerDialog.BackgroundGraphic!=252))))))))))))
{
gPlayerDialog.BackgroundGraphic=248;
}
if((((((((((((Hotspot.GetAtScreenXY(mouse.x, mouse.y)==hotspot[8])&&(mouse.IsButtonDown(eMouseRight))
&&(gPlayerDialog.BackgroundGraphic!=242)&&(gPlayerDialog.BackgroundGraphic!=244)&&(gPlayerDialog.BackgroundGraphic!=245)
&&(gPlayerDialog.BackgroundGraphic!=246)&&(gPlayerDialog.BackgroundGraphic!=247)&&(gPlayerDialog.BackgroundGraphic!=248)
&&(gPlayerDialog.BackgroundGraphic!=155)&&(gPlayerDialog.BackgroundGraphic!=243)&&(gPlayerDialog.BackgroundGraphic!=251)
&&(gPlayerDialog.BackgroundGraphic!=250)&&(gPlayerDialog.BackgroundGraphic!=252))))))))))))
{
gPlayerDialog.BackgroundGraphic=249;
}
if(((((((((((((Hotspot.GetAtScreenXY(mouse.x, mouse.y)==hotspot[9])&&(mouse.IsButtonDown(eMouseRight))
&&(gPlayerDialog.BackgroundGraphic!=242)&&(gPlayerDialog.BackgroundGraphic!=244)&&(gPlayerDialog.BackgroundGraphic!=245)
&&(gPlayerDialog.BackgroundGraphic!=246)&&(gPlayerDialog.BackgroundGraphic!=247)&&(gPlayerDialog.BackgroundGraphic!=248)
&&(gPlayerDialog.BackgroundGraphic!=249)&&(gPlayerDialog.BackgroundGraphic!=155)&&(gPlayerDialog.BackgroundGraphic!=251)
&&(gPlayerDialog.BackgroundGraphic!=250)&&(gPlayerDialog.BackgroundGraphic!=252)))))))))))))
{
gPlayerDialog.BackgroundGraphic=243;
}
if((((((((((((Hotspot.GetAtScreenXY(mouse.x, mouse.y)==hotspot[10])&&(mouse.IsButtonDown(eMouseRight))
&&(gPlayerDialog.BackgroundGraphic!=242)&&(gPlayerDialog.BackgroundGraphic!=244)&&(gPlayerDialog.BackgroundGraphic!=245)
&&(gPlayerDialog.BackgroundGraphic!=246)&&(gPlayerDialog.BackgroundGraphic!=247)&&(gPlayerDialog.BackgroundGraphic!=248)
&&(gPlayerDialog.BackgroundGraphic!=249)&&(gPlayerDialog.BackgroundGraphic!=243)&&(gPlayerDialog.BackgroundGraphic!=251)
&&(gPlayerDialog.BackgroundGraphic!=155)&&(gPlayerDialog.BackgroundGraphic!=252))))))))))))
{
gPlayerDialog.BackgroundGraphic=250;
}
if((((((((((((Hotspot.GetAtScreenXY(mouse.x, mouse.y)==hotspot[11])&&(mouse.IsButtonDown(eMouseRight))
&&(gPlayerDialog.BackgroundGraphic!=242)&&(gPlayerDialog.BackgroundGraphic!=244)&&(gPlayerDialog.BackgroundGraphic!=245)
&&(gPlayerDialog.BackgroundGraphic!=246)&&(gPlayerDialog.BackgroundGraphic!=247)&&(gPlayerDialog.BackgroundGraphic!=248)
&&(gPlayerDialog.BackgroundGraphic!=249)&&(gPlayerDialog.BackgroundGraphic!=243)&&(gPlayerDialog.BackgroundGraphic!=155)
&&(gPlayerDialog.BackgroundGraphic!=250)&&(gPlayerDialog.BackgroundGraphic!=252))))))))))))
{
gPlayerDialog.BackgroundGraphic=251;
}
if((((((((((((Hotspot.GetAtScreenXY(mouse.x, mouse.y)==hotspot[12])&&(mouse.IsButtonDown(eMouseRight))
&&(gPlayerDialog.BackgroundGraphic!=242)&&(gPlayerDialog.BackgroundGraphic!=244)&&(gPlayerDialog.BackgroundGraphic!=245)
&&(gPlayerDialog.BackgroundGraphic!=246)&&(gPlayerDialog.BackgroundGraphic!=247)&&(gPlayerDialog.BackgroundGraphic!=248)
&&(gPlayerDialog.BackgroundGraphic!=249)&&(gPlayerDialog.BackgroundGraphic!=243)&&(gPlayerDialog.BackgroundGraphic!=251)
&&(gPlayerDialog.BackgroundGraphic!=250)&&(gPlayerDialog.BackgroundGraphic!=155))))))))))))
{
gPlayerDialog.BackgroundGraphic=252;
}
}


J.E.S.

Thanks for the replies, I didn't see you guys had posted them before I replied to my own solution. I will try your suggestions out also. I pretty sure my solution was taking the long way...

Thanks again!

Pumaman

To save you having hundreds of copy-and-paste "if" statements, either try something like KhrisMUC suggested by arranging the sprite numbers in order, or use a Custom Property on the hotspot to store its sprite number, which you can then query with the hotspot's GetProperty command.

Also, definitely use gPlayerDialog.Visible to keep track of whether it's displayed or not, rather than checking each possible sprite number that it could be set to -- otherwise you'll have a nightmare trying to maintain all that code.

J.E.S.

@KhrisMUC ...I'm trying to work with your code, but I know I'm missing something really simple or don't know how to complete the rest of it. Here is what I have so far:

In the global script:

Code: ags


#sectionstart on_mouse_click  // DO NOT EDIT OR REMOVE THIS LINE
function on_mouse_click(MouseButton button) // called when a mouse button is clicked. button is either LEFT or RIGHT
  {
  if (IsGamePaused() == 1) // Game is paused, so do nothing (ie. don't allow mouse click)
    {
    }
  else if (button == eMouseLeft) 
    {
    ProcessClick(mouse.x,mouse.y, mouse.Mode);
  }
  else if (button == eMouseRight)
  {
    Hotspot*h = Hotspot.GetAtScreenXY(mouse.x, mouse.y);
    if ((h != null) && (gPlayerDialog.Visible!=1)) 
    {
    gPlayerDialog.BackgroundGraphic = h.ID + 240; // use e.g. slots 241 - X
    SetTimer(1, 120);
    gPlayerDialog.SetPosition(player.x-20, player.y-70);
    gPlayerDialog.Visible = 1;
    }
  }
  
  else // right-click, so cycle cursor
    {   
    mouse.SelectNextMode();
  }
  }


I ran this just to see if any errors would pop up, but i got no errors and nothing happens when I right-click on any hotspots.
What exactly was I supposed to do with the line that says, "bubble"? and is there something that needs to be added to my room script? I have a feeling I'm almost there, but need just a little extra hint. I will also be using the same function in all my rooms. Thanks for your time!

SMF spam blocked by CleanTalk