[SOLVED]Prevent object to move when another object is next to it .

Started by arj0n, Sat 24/07/2010 09:18:00

Previous topic - Next topic

arj0n

Another question:

Say object1 moves only left or right and object2 can be in it's path.

How can check to prevent 2 objects colliding with each other?
Tried 'IsCollidingWithObject' but that doesn't prevent.

Joe

Try something like this:
Code: ags

object1.Move(...);
if(object1.x>=object2.x && object1.y==object2.y)
  object1.x=object2.x;

//But if you want to make it better you should use with object1.width... aswell
if(object1.x+object1.width>=object2.x)
  if(object1.y<object2.y-object2.height && object1.y-object1.height>object2.y)
    object1.x=object2.x-object1.width;


Obviously... this is not tested
Copinstar © Oficial Site

Dualnames

I think this actually may work.

Code: ags

int detection=5;//How close in pixels should the objects be
Object*back; //if object1 is behind object 2
Object*forth; //if object2 is in front of object 1

function repeatedly_execute() {
back=Object.GetAtScreenXY(object[1].X-detection,object[1].Y);//I've used object[1] instead of object1, in case you want this globally, if you do put a player.Room check.
forth=Object.GetAtScreenXY(object[1].X+detection,object[1].Y);

if ((back!=null) || (forth!=null)) {
//prevent movement of objects here
}

}


EDIT: There's a chance you'll need something different than X property to get the actual width of the objects, like Game.SpriteWidth, so keep that in mind.
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

arj0n


SMF spam blocked by CleanTalk