Object moves along the x,y from its current location

Started by spydaniel, Tue 20/07/2010 22:52:25

Previous topic - Next topic

spydaniel

I want the player to be able to push an object that will be in one of my rooms, but I can't find a command to do this?

I want the object to move so many pixels along the x,y from its current location, how would I do this?

Khris

The command to move an object is called Object.Move().

To move, say, oBox 40 pixels to the right, you'd use

Code: ags
  oBox.Move(oBox.X + 40, oBox.Y, player.WalkSpeedX);

spydaniel

Thanks Khris, got it to work, but I have another question about this.

Is it possible to get the player to walk up to the object's left side and have them face it? Just so it looks like they are pushing the object.

Khris

Sure, this should work:

Code: ags
  player.Walk(oBox.X - 10, oBox.Y - 2, eBlock);  // experiment with values until it looks good
  player.FaceLocation(player.x + 1, player.y, eBlock);

spydaniel

Thanks again Khris, works perfectly.

In my room, I have a square hole in which the object is meant to slot into, but I'm not sure of a few things.

I want the player to be able to walk over the hole when the object has fell into the hole, but not when it isn't, so I have done this:

Code: ags
function hHotspot1_WalkOn() //Push player up 1 pixel
{
cEgo.StopMoving();
cEgo.Move(cEgo.x, cEgo.y - 1, eBlock, eWalkableAreas);
}

function hHotspot2_WalkOn() //Push player left 1 pixel
{
cEgo.StopMoving();
cEgo.Move(cEgo.x - 1, cEgo.y, eBlock, eWalkableAreas);
}

function hHotspot3_WalkOn() //Push player right 1 pixel
{
cEgo.StopMoving();
cEgo.Move(cEgo.x + 1, cEgo.y, eBlock, eWalkableAreas);
}


Is this the only way to block the player from walking over the hole or is there a better way?

Also, would I use a hotspot to run a script to get the object to move down into the hole? And then make the object un-interactive?

Khris

1. To prevent the player from walking over the hole, draw a second walkable area where the hole is, and make it slightly bigger to account for the player sprite's width and 'depth'.
Turn this area off in the 'first time player enters room' event (call RemoveWalkableArea(2);).
After the object has been pushed into the hole, turn it back on (RestoreWalkableArea(2);).

2. To trigger the object falling down, best use a region. Move the object to the lowest, leftmost position where it's still gonna drop and note the object's coordinates, then switch to regions and draw a pixel there. Same for the topmost, rightmost position. Now fill the area with a rectangular shape (or trapezoid, depending on the perspective of the background).

In the room's repeatedly_execute event, do this:

Code: ags
  if (Region.GetAtScreenXY(oBox.X + GetViewPortX(), oBox.Y + GetViewportY()) == region[1]) {
    region[1].Enabled = false;
    // drop the box, e.g. move if over the hole, then straight down
    RestoreWalkableArea(2);
  }


Note that this will only trigger immediately if there's no blocking movement or walk.
Also, making the box disappear inside the hole can be achieved by using a walkbehind and 'cleverly' adjusting the baselines, possibly during movement.
For convenience's sake, after the box hit the hole's floor, switch to a second background identical to the first but with the box in place, and disable the walkbehind and turn off the box object.

spydaniel

I wanted to share what I managed to do with you're help Khris.

This is my room script:

Code: ags
int pushcounter = 0;

function room_FirstLoad()
{
cEgo.IgnoreWalkbehinds = true;
RemoveWalkableArea(2);
oStoneBlock.Solid = true;
oStoneBlock.BlockingHeight = 100;
oStoneBlock.BlockingWidth = 165;
}

function oStoneBlock_Interact()
{
player.Walk(oStoneBlock.X - 10, oStoneBlock.Y - 2, eBlock);
oStoneBlock.Move(oStoneBlock.X +19, oStoneBlock.Y, cEgo.WalkSpeedX, eBlock, eAnywhere);
pushcounter += 1;

if (pushcounter == 10)
{
  oStoneBlock.Clickable = false;
  oStoneBlock.Move(oStoneBlock.X, oStoneBlock.Y + 90, 50, eBlock, eAnywhere);
  oStoneBlock.Baseline = 10;
  RestoreWalkableArea(2);
}
}


Here is the compiled room to test it out: Room

I changed the magnifying glass icon to one of my own too XD

SMF spam blocked by CleanTalk