Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: cHugoDance on Fri 26/04/2013 13:54:23

Title: Being able to click objects behind the main character (clickable property)
Post by: cHugoDance on Fri 26/04/2013 13:54:23
Hi guys and gals,

I'm having a relatively simple problem. What I want is, when the cursor passes over the main character, he becomes partially transparent and it's possible to click objects and hotspots behind him.

I'm using the Tween module.

Code (AGS) Select

bool hugoFaded = false;

function repeatedly_execute()
{
  if (IsGamePaused() == 1) return;  //anything above is always done, below is what doesnt get done when paused
 
 
  if (Character.GetAtScreenXY(mouse.x, mouse.y) == null && cHugo.ActiveInventory == null && hugoFaded){ //if mouse is not over hugo and hugo is faded out
 
    fadeHugoIn(); //run fade hugo in
    hugoFaded = false;
    }

    if(Character.GetAtScreenXY(mouse.x, mouse.y) == cHugo && cHugo.ActiveInventory == null&& !hugoFaded) {  //if mouse is over hugo and hugo isnt faded
     
    fadeHugoOut();
    hugoFaded = true;
    }


The functions mentioned just simply tween the main character (hugo)'s transparency.
Code (AGS) Select

function fadeHugoIn (){
    cHugo.StopAllTweens();
    cHugo.TweenTransparency(0.3, 0, eEaseOutTween, eNoBlockTween);
}

function fadeHugoOut(){
    cHugo.StopAllTweens();
    cHugo.TweenTransparency(0.3, 50, eEaseInTween, eNoBlockTween);
  }


The issue I'm having is that Character.GetAtScreenXY relies on the character having the clickable property enabled, which needs to be disabled when the mouse is hovering over him so that the player can click behind him.
If anybody has a solution or any ideas as to how I can achieve this, your help would me most appreciated.
Title: Re: Being able to click objects behind the main character (clickable property)
Post by: san.daniele on Fri 26/04/2013 15:56:54
maybe I shouldn't try to reply, having almost no knowledge myself.

I would try to play around comparing player.x and player.y with mouse.x/mouse.y. somehow define a range of coordinates that are valid (I guess the big downside is that transparent areas of the character aren't recognized).


… or is it possible to add something like a
Code (AGS) Select
if mouse.IsButtonDown() cEgo.Clickable = false;
Meaning that as soon as the player clicks on the character it renders it unclickable so the actual click goes through.
then reactivate the Clickable once the button is released.
Title: Re: Being able to click objects behind the main character (clickable property)
Post by: geork on Fri 26/04/2013 16:06:15
One way I can see is to set object precedence over other available interactions in on_mouse_click. I don't know what mouse setup you have, but if you had something like:
Code (AGS) Select
function on_mouse_click(MouseButton button)
{
  //Stuff
  if(button == eMouseLeft){
    ProcessClick(mouse.x, mouse.y, Mouse.Mode);
  }
  //Stuff
}
 
You could change it to:
Code (AGS) Select
function on_mouse_click(MouseButton button)
{
  //Stuff
  if(button == eMouseLeft){
    Object *o = Object.GetAtScreenXY(mouse.x, mouse.y);
    if(o != null) o.RunInteraction(Mouse.Mode); //IF there is an object under the mouse pointer, run object interaction
    else ProcessClick(mouse.x, mouse.y, Mouse.Mode); //else proceed as normal
  }
  //Stuff
}

Not tested, but will hopefully work

Hope that helps! :)

EDIT: Beaten by san.daniele :)
Title: Re: Being able to click objects behind the main character (clickable property)
Post by: Khris on Fri 26/04/2013 19:22:54
You can work around the original issue by using a bounding box to figure out whether the mouse is close to or above the player sprite.

The other thing is, if the player becomes not clickable as soon as you're near them, you can just go ahead and set them to be not clickable regardless of where the mouse is.
Then make them transparent if the mouse is close, using a few simple lines of math. (Basically, mouse distance from (player.x, player.y - character sprite height / 2) < 40 or something like that.)