Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: arj0n on Sun 15/08/2010 12:14:56

Title: Drag'n'Drop: drag'n'drop object goes through other objects and bounderies
Post by: arj0n on Sun 15/08/2010 12:14:56
How can I change this code so that the object is only drag'n'Dropped left or right?


function room_RepExec()
{
 if (mouse.IsButtonDown(eMouseLeft)) {
   if (button_pressed==0) {
     button_pressed = 1;
     ob = Object.GetAtScreenXY(mouse.x, mouse.y);
   }
 }
 else {
   button_pressed=0;
   if (ob!=null) {
     ob=null;
   }
 }
 if (ob != null) {
   int width  = Game.SpriteWidth[ob.Graphic];
   int height = Game.SpriteHeight[ob.Graphic];
   ob.SetPosition(mouse.x-width/2, mouse.y+height/2);  
}
}
Title: Re: Drag'n'Drop: drag X and leave Y at position
Post by: tzachs on Sun 15/08/2010 12:41:33
If I understand you correctly, replace the last line with:


ob.SetPosition(mouse.x-width/2, ob.Y); 


And you can then also remove the line before it (int height = ...)
Title: Re: Drag'n'Drop: drag X and leave Y at position
Post by: arj0n on Sun 15/08/2010 13:45:18
Thanx
Title: Re: Drag'n'Drop: drag X and leave Y at position
Post by: arj0n on Sun 15/08/2010 14:32:36
To get a clear view of what I'm trying to explain, check the game-demo "Blockz".

This part of code checks, when dragging 'myobject' to the right, if an object called 'oBlockb' is at a certain coordinate [and therefor being in it's way].
If so, 'myobject' [the dragged object] shouldn't move.
It also should let the dragged object only move right when it's X <=360.

Problems:
* If I drag the object really quick it drags through oBlockb instead of being blocked by it.
* If I drag the object really quick it drags through the  <=360 boundary.
* If I drag the object slowly it stops because of "myobject.X+120==oBlockb.X", but then I also can't drag it to the left...

So, how to solve these problems...

And: is there maybe a better way to check if objects are blocking the to-be-dragged "myobject'?


Object* myobject = Object.GetAtScreenXY(mouse.x, mouse.y);
if (mouse.IsButtonDown(eMouseLeft)) {
    if (button_pressed==0) {
      button_pressed = 1;
      ob = Object.GetAtScreenXY(mouse.x, mouse.y);
    }
  }
  else {
    button_pressed=0;
    if (ob!=null) {
      //ob.Baseline = 0;   
      ob=null;
    }
  }
  if ((ob != null) && (Game.SpriteWidth[myobject.Graphic]==120) &&
  (myobject.X+120==oBlockb.X &&  myobject.Y-60==oBlockb.Y-60 && myobject.X <=360)){
    int width  = Game.SpriteWidth[ob.Graphic];
    int height = Game.SpriteHeight[ob.Graphic];
  ob.SetPosition(ob.X, ob.Y);} 
 
  else if  ((ob != null) && (Game.SpriteWidth[myobject.Graphic]==120) &&
  (myobject.X+120==oBlockb.X && myobject.Y-60==oBlockb.Y-120 && myobject.X <=360)) {
    int width  = Game.SpriteWidth[ob.Graphic];
    int height = Game.SpriteHeight[ob.Graphic];
  ob.SetPosition(ob.X, ob.Y);} 
   
  else if  (ob != null){
    int width  = Game.SpriteWidth[ob.Graphic];
    int height = Game.SpriteHeight[ob.Graphic];
    ob.SetPosition(mouse.x-width/2, ob.Y);


Regards,
Arj0n.