Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: arj0n on Thu 22/07/2010 11:50:11

Title: [SOLVED] Determine if mouse pointer clicks on right half or left half of object
Post by: arj0n on Thu 22/07/2010 11:50:11
In order to get an object to move right or left x pixels I'd like to know,
if it's possible to determine if the mouse pointer clicks on the right half
or on the left half of an object.

Title: Re: Determine if mouse pointer clicks on right half or left half of object
Post by: GarageGothic on Thu 22/07/2010 12:04:52
Sure, just use the dimension of the object's graphic.

bool ClickedRightHalf(this Object*) { //call when the object has been clicked
 if (mouse.x + GetViewportX()) > (this.X + (Game.SpriteWidth[this.Graphic]/2)) return true;
 }


This assumes the object isn't scaled, otherwise you have to multiply the spritewidth by the scaling factor at object.x, object.y and divide by 100.

Edit: Added this to the code, and streamlined it a bit (I think). Not tested in any way whatsoever.

bool ClickedRightHalf(this Object*) {
  if (!this.IgnoreScaling) {
    return ((mouse.x + GetViewportX()) > (this.X + (GetScalingAt(this.x, this.y)*Game.SpriteWidth[this.Graphic])/200));
    }
  else return ((mouse.x + GetViewportX()) > (this.X + (Game.SpriteWidth[this.Graphic]/2));
  }
Title: Re: Determine if mouse pointer clicks on right half or left half of object
Post by: arj0n on Thu 22/07/2010 12:53:06
Thanx, I'll check it out later.
For now I think the object(s) won't be scaled
Title: Re: Determine if mouse pointer clicks on right half or left half of object
Post by: arj0n on Fri 23/07/2010 18:44:16
Placed an object in a room, gave the object a scriptname, using "any click on opbject", code:

function moveblockhorizontal_AnyClick()
{
bool ClickedRightHalf(this Object*) { //call when the object has been clicked
  if (mouse.x + GetViewportX()) > (this.X + (Game.SpriteWidth[this.Graphic]/2)) return true;
}

This dowsn't seem to work.
What am I doing wrong?
Title: Re: Determine if mouse pointer clicks on right half or left half of object
Post by: GarageGothic on Fri 23/07/2010 18:52:48
You're not supposed to just paste the code into the function. Put it in the room script (or in the global script along with an import in the global header, in case you need to call it from other rooms too), then call it from inside your own function. Like this:


bool ClickedRightHalf(this Object*) { //call when the object has been clicked
 if (mouse.x + GetViewportX()) > (this.X + (Game.SpriteWidth[this.Graphic]/2)) return true;
}

function moveblockhorizontal_AnyClick() {
 //since you're not specifying any particular Object in your function,
  //I'll just get the one that the cursor is over for this example
 Object* myobject = Object.GetAtScreenXY(mouse.x, mouse.y);
  //move ten pixels to the right, or whatever you want to do
 if (myobject.ClickedRightHalf()) myobject.Move(myobject.X+10, myobject.Y, 10, true);
  //move ten pixels left
 else myobject.Move(myobject.X-10, myobject.Y, 10, true); 
 }



Edit: Sorry, not used to interaction editor functions. If the object is always the same (called moveblockhorizontal), you don't need to get myobject, just substitute any "myobject" references with  "moveblockhorizontal" instead. If this is the only object in the game that has this kind of interaction, you can also combine the code like so:

function moveblockhorizontal_AnyClick() {
 if (mouse.x + GetViewportX()) > (moveblockhorizontal.X + (Game.SpriteWidth[moveblockhorizontal.Graphic]/2)) moveblockhorizontal.Move(moveblockhorizontal.X+10, moveblockhorizontal.Y, 10, true);
 else moveblockhorizontal.Move(moveblockhorizontal.X-10, moveblockhorizontal.Y, 10, true);
 }


Of course you'll want to put some horizontal coordinate limits so the player won't push it off screen.
Title: Re: Determine if mouse pointer clicks on right half or left half of object
Post by: arj0n on Fri 23/07/2010 20:13:29
Sorry but here's noob again...
This is not working, getting an error:  
"PE04: parse error at '>:'"
for the line starting with ïf (mouse.x +..."):

// room script file
 bool ClickedRightHalf(this Object*) { //call when the object has been clicked
 if (mouse.x + GetViewportX()) > (this.X + (Game.SpriteWidth[this.Graphic]/2)) return true;
}

function Block_AnyClick()
{
 //since you're not specifying any particular Object in your function,
 //I'll just get the one that the cursor is over for this example
 Object* myobject = Object.GetAtScreenXY(mouse.x, mouse.y);
 //move ten pixels to the right, or whatever you want to do
 if (myobject.ClickedRightHalf()) myobject.Move(myobject.X+10, myobject.Y, 10, true);
 //move ten pixels left
 else myobject.Move(myobject.X-10, myobject.Y, 10, true);
}
Title: Re: Determine if mouse pointer clicks on right half or left half of object
Post by: Matti on Fri 23/07/2010 20:19:08
if (mouse.x + GetViewportX() )

Delete that last bracket - it ends the if-clause before the ">" .
Title: Re: Determine if mouse pointer clicks on right half or left half of object
Post by: arj0n on Fri 23/07/2010 20:31:04
Changed this:
if (mouse.x + GetViewportX()) > (this.X + (Game.SpriteWidth[this.Graphic]/2)) return true;
into this:
if (mouse.x + GetViewportX() > (this.X + (Game.SpriteWidth[this.Graphic]/2))) return true;

That seems to work but when running the 'game' and clicking on the object,
the cursor changes into the watch-cursor and nothing happens anymore...
???


Code that seems to work when saving the room but not when running the game:

// room script file
  bool ClickedRightHalf(this Object*) { //call when the object has been clicked
  if (mouse.x + GetViewportX() > (this.X + (Game.SpriteWidth[this.Graphic]/2))) return true;
}

function Block_AnyClick()
{
  //since you're not specifying any particular Object in your function,
  //I'll just get the one that the cursor is over for this example
  Object* myobject = Object.GetAtScreenXY(mouse.x, mouse.y);
  //move ten pixels to the right, or whatever you want to do
  if (myobject.ClickedRightHalf()) myobject.Move(myobject.X+10, myobject.Y, 10, true);
  //move ten pixels left
  else myobject.Move(myobject.X-10, myobject.Y, 10, true);
}
Title: Re: Determine if mouse pointer clicks on right half or left half of object
Post by: GarageGothic on Fri 23/07/2010 20:53:55
Yeah, as I said it wasn't tested. I messed up with the blocking and walkable area parameters in the move command. Try this:

function Block_AnyClick()
{
  //since you're not specifying any particular Object in your function,
  //I'll just get the one that the cursor is over for this example
  Object* myobject = Object.GetAtScreenXY(mouse.x, mouse.y);
  //move ten pixels to the right, or whatever you want to do
  if (myobject.ClickedRightHalf()) myobject.Move(myobject.X+10, myobject.Y, 10, eBlock, eAnywhere);
  //move ten pixels left
  else myobject.Move(myobject.X-10, myobject.Y, 10, eBlock, eAnywhere);
}
Title: [SOLVED] Determine if mouse pointer clicks on right half or left half of object
Post by: arj0n on Fri 23/07/2010 22:21:34
Like a charm, thanx :)
Title: Re: [SOLVED] Determine if mouse pointer clicks on right half or left half of object
Post by: arj0n on Sat 24/07/2010 10:00:04
function oBlock2_AnyClick() works fine but
function oBlock4_AnyClick() doesn't, it only moves down.
Why is this or where is the code wrong?


// room script file
  bool ClickedRightHalf(this Object*) { //call when the object has been clicked
  if (mouse.x + GetViewportX() > (this.X + (Game.SpriteWidth[this.Graphic]/2))) return true;
  }
  bool ClickedLowerHalf(this Object*) { //call when the object has been clicked
  if (mouse.y + GetViewportY() > (this.Y + (Game.SpriteHeight[this.Graphic]/2))) return true;
  }

function oBlock2_AnyClick()
{
  //since you're not specifying any particular Object in your function,
  //I'll just get the one that the cursor is over for this example
  Object* myobject = Object.GetAtScreenXY(mouse.x, mouse.y);
  if ((myobject.X <=380 && myobject.ClickedRightHalf())) myobject.Move(myobject.X+100, myobject.Y, 10, eBlock, eAnywhere);
   //move x pixels to the right
  else if (myobject.X >=30 && !myobject.ClickedRightHalf())  myobject.Move(myobject.X-100, myobject.Y, 10, eBlock, eAnywhere);
  //move x pixels left

function oBlock4_AnyClick()
{
  //since you're not specifying any particular Object in your function,
  //I'll just get the one that the cursor is over for this example
  Object* myobject = Object.GetAtScreenXY(mouse.x, mouse.y);
  if ((myobject.Y >=30 && myobject.ClickedLowerHalf())) myobject.Move(myobject.X, myobject.Y-60, 10, eBlock, eAnywhere);
   //move x pixels up.
  else if (myobject.Y <=400 && !myobject.ClickedLowerHalf())  myobject.Move(myobject.X, myobject.Y+60, 10, eBlock, eAnywhere);
   //move x pixels Down.
}
Title: Re: [SOLVED] Determine if mouse pointer clicks on right half or left half of object
Post by: GarageGothic on Sat 24/07/2010 10:15:18
Because the x,y of an object signifies the lower left corner, so you should do:

  bool ClickedLowerHalf(this Object*) { //call when the object has been clicked
  if (mouse.y + GetViewportY() > (this.Y - (Game.SpriteHeight[this.Graphic]/2))) return true; //Substituted + for -
  }
Title: Re: [SOLVED] Determine if mouse pointer clicks on right half or left half of object
Post by: arj0n on Sat 24/07/2010 11:02:32
That was it, thanx.