[SOLVED] Determine if mouse pointer clicks on right half or left half of object

Started by arj0n, Thu 22/07/2010 11:50:11

Previous topic - Next topic

arj0n

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.


GarageGothic

Sure, just use the dimension of the object's graphic.

Code: ags
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.

Code: ags
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));
  }

arj0n

Thanx, I'll check it out later.
For now I think the object(s) won't be scaled

arj0n

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?

GarageGothic

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:

Code: ags

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:

Code: ags
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.

arj0n

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);
}

Matti

if (mouse.x + GetViewportX() )

Delete that last bracket - it ends the if-clause before the ">" .

arj0n

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);
}

GarageGothic

Yeah, as I said it wasn't tested. I messed up with the blocking and walkable area parameters in the move command. Try this:

Code: ags
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);
}

arj0n

Like a charm, thanx :)

arj0n

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.
}

GarageGothic

Because the x,y of an object signifies the lower left corner, so you should do:

Code: ags
  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 -
  } 

arj0n


SMF spam blocked by CleanTalk