Jump to content


Photo

box help


  • Please log in to reply
4 replies to this topic

#1 xxmikexx

xxmikexx

    GMC Member

  • GMC Member
  • 31 posts
  • Version:GM8

Posted 11 July 2012 - 08:25 PM

im doing a platform game and i need help with my box dragging.
i have my char moving left and right and jumping i just need help pushing and pulling a box if it makes it easier my player is obj_player
and my box is named obj_box also what makes it realy hard for me is that i want my box to be-able to fall and if its up high enough it will smash.
i already made a new object called obj_boxsmash but i dont know how to connect all these things.

Edited by xxmikexx, 11 July 2012 - 08:28 PM.

  • 0

#2 Koaske

Koaske

    GMC Member

  • GMC Member
  • 383 posts
  • Version:GM8

Posted 11 July 2012 - 09:43 PM

You need a variable that tells if you're holding onto an object. That would go something like this:

In the player's step event:

crateId = collision_point(x+30, y, objCrate, true, true) //we check if there's a crate 30 pixels ahead of your direction (only works with crates on your right at the moment)
if keyboard_check(vk_space) and crateId
holding = true

if holding
{
   if keyboard_check(vk_left)
   {
      x -= 2
      crateId.x -= 2
   }
   else if keyboard_check(vk_right)
   {
       x+= 2
       crateId.x += 2 // move the crate with the same speed as your character
   }
}

Edited by Koaske, 12 July 2012 - 11:25 AM.

  • 1

#3 HerpDerp

HerpDerp

    GMC Member

  • New Member
  • 215 posts
  • Version:GM8

Posted 11 July 2012 - 09:57 PM

For the box to be able to fall:

obj_box STEP-event:
if place_free(x,y+1) //if there's no ground underneath the box:
{
gravity_direction=270 
gravity=0.5   //set the gravity to make it fall.
}
else //The code in brackets below this will be executed if there is indeed ground below the box:
{
gravity=0 //Disable gravity.
vspeed=0 //Set the vertical speed to 0.
}

Check if box is falling fast enough to be able to smash:

obj_box COLLISION WITH WHATEVER OBJECT YOU WANT IT TO BE ABLE TO SMASH-event:
if vspeed>//Put a number here, fiddle around with it a little bit. It decides whether the current vertical speed of the box is high enough to be smashed or not.
{
//Put smash code here. If you created another object that handles this, put instance_change(x,y,obj_boxsmash) here instead of this green line.
}

If you want to save time, you could create a parent for all the objects that have a chance to smash the box.

To do this you create a new object obj_boxparent. Then go to the objects that have a possibility to smash the
box. Then at the bottom left of the object editor, click Set Parent and select obj_boxparent from the dropdown menu.

Then instead of setting the collision to many objects, set the collision-event for above code to this obj_boxparent.

The box smash thing will happen perfectly for the right objects.

Edited by HerpDerp, 11 July 2012 - 10:04 PM.

  • 1

#4 xxmikexx

xxmikexx

    GMC Member

  • GMC Member
  • 31 posts
  • Version:GM8

Posted 12 July 2012 - 12:34 AM

You need a variable that tells if you're holding onto an object. That would go something like this:

In the player's step event:

crateId = collision_point(x+30, y, objCrate) //we check if there's a crate 30 pixels ahead of your direction (only works with crates on your right at the moment)
if keyboard_check(vk_space) and crateId
holding = true

if holding
{
   if keyboard_check(vk_left)
   {
      x -= 2
      crateId.x -= 2
   }
   else if keyboard_check(vk_right)
   {
       x+= 2
       crateId.x += 2 // move the crate with the same speed as your character
   }
}

theres an error in the first line. can you please fix it?
  • 0

#5 Koaske

Koaske

    GMC Member

  • GMC Member
  • 383 posts
  • Version:GM8

Posted 12 July 2012 - 11:27 AM

Right... I forgot the last two arguments "precise" and "notme". I modified the code in my first post. By the way, you can also look up the function collision_point from GM's documentation (Help -> contents or press F1) to see more info about the arguments.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users