Jump to content


Photo

Move Object With Cursor


  • Please log in to reply
16 replies to this topic

#1 MattClark1000

MattClark1000

    GMC Member

  • New Member
  • 60 posts
  • Version:GM8

Posted 18 May 2010 - 04:04 PM

I have a cursor object, and a block object. The cursor is moved by the arrow keys. (*EDIT*- cursor is not controlled by the mouse) How can I make it so if I move the cursor on top of the block, press and hold down, say the letter X on the keyboard, and press an arrow key, it will move the cursor and the block together, but only as long as i'm holding down X, and if I let go of X and press an arrow key, it will just move the cursor, and leave the block where it was when I let go of the X key? Similar to the way you play the game "Puzznic".

Using GM7 Pro

Edited by MattClark1000, 18 May 2010 - 04:20 PM.

  • 0

#2 moomoo112

moomoo112

    GMC Member

  • New Member
  • 663 posts
  • Version:GM7

Posted 18 May 2010 - 04:13 PM

create a variable in the block's create event, let's say drag
then you want to check if a key is pressed, x, and if it is, the block will drag along so,
//create event
drag = 0

//step event
if keyboard_check(ord('X')){
  if mouse_x = x and mouse_y = y //if X is pressed and the mouse is over the block..
   drag = 1} //drag it
if drag = 1{
  x = mouse_x
  y = mouse_y}
if keyboard_check_released(ord('X'))
  drag = 0 //if you stop pressing X the dragging stops
I don't have GM available currently, but I believe that code should work how you want

Edited by moomoo112, 18 May 2010 - 04:14 PM.

  • 0

#3 Shorooq

Shorooq

    GMC Member

  • GMC Member
  • 526 posts

Posted 18 May 2010 - 04:16 PM

in obj_block step event
if place_meeting(x,y,obj_cursor)
{
if keyboard_check(ord("X"))
{
x = obj_cursor.x
y = obj_cursor.y
}
}

moomoo12
you way is right but he use obj_cursor
not the real cursor

Edited by Shorooq, 18 May 2010 - 04:24 PM.

  • 0

#4 moomoo112

moomoo112

    GMC Member

  • New Member
  • 663 posts
  • Version:GM7

Posted 18 May 2010 - 04:28 PM

erk, yea, my bad....
use Shorooq's code
  • 0

#5 Gonar

Gonar

    GMC Member

  • New Member
  • 161 posts

Posted 18 May 2010 - 05:29 PM

hi.
if you like you can see at http://gmc.yoyogames...howtopic=249574
my platform lib, because have push/drag code.

sorry haven't gm here to make an example.

you can use the lib or you can see the code in the lib to get an idea :)

cu and gl

maybe at home download start again gm :)
  • 0

#6 MattClark1000

MattClark1000

    GMC Member

  • New Member
  • 60 posts
  • Version:GM8

Posted 18 May 2010 - 09:45 PM

hi.
if you like you can see at http://gmc.yoyogames...howtopic=249574
my platform lib, because have push/drag code.

sorry haven't gm here to make an example.

you can use the lib or you can see the code in the lib to get an idea :)

cu and gl

maybe at home download start again gm :)



how do you get the .gm6 to use the library file? b/c i've looked at the library file itself, and it's a mess, and I can't put it together properly.
also, I use GM7 Pro, and when I loaded up your .gm6 file in it, there were several "unknown actions" that I could not look at.
  • 0

#7 Myname

Myname

    GMC Member

  • New Member
  • 413 posts

Posted 18 May 2010 - 09:55 PM

This goes in the block's step event. Change obj_cursor to the cursor object.
if(place_meeting(x,y,obj_cursor)){
	if(keyboard_check(ord('X'))){
		x = obj_cursor.x;
		y = obj_cursor.y;
	}
}

  • 0

#8 MattClark1000

MattClark1000

    GMC Member

  • New Member
  • 60 posts
  • Version:GM8

Posted 19 May 2010 - 12:32 AM

This goes in the block's step event. Change obj_cursor to the cursor object.

if(place_meeting(x,y,obj_cursor)){
	if(keyboard_check(ord('X'))){
		x = obj_cursor.x;
		y = obj_cursor.y;
	}
}




several ppl have given me this code, however it isn't working as intended, even though it seems as if it should - it's error free, but it does not function right. I placed it in the block's step event, as instructed, and tested it, but it is not working. If i use the arrows to move the cursor over the block, hold down "X", and then move the cursor again using the arrows - still holding X, the cursor does not move the block along with it, like it should.
  • 0

#9 Myname

Myname

    GMC Member

  • New Member
  • 413 posts

Posted 19 May 2010 - 12:52 AM

This goes in the block's step event. Change obj_cursor to the cursor object.

if(place_meeting(x,y,obj_cursor)){
	if(keyboard_check(ord('X'))){
		x = obj_cursor.x;
		y = obj_cursor.y;
	}
}




several ppl have given me this code, however it isn't working as intended, even though it seems as if it should - it's error free, but it does not function right. I placed it in the block's step event, as instructed, and tested it, but it is not working. If i use the arrows to move the cursor over the block, hold down "X", and then move the cursor again using the arrows - still holding X, the cursor does not move the block along with it, like it should.

Well then you're putting it in the wrong place or your sprite offsets are weird.
  • 0

#10 Gonar

Gonar

    GMC Member

  • New Member
  • 161 posts

Posted 19 May 2010 - 06:32 AM

hi
you must put the PF_Gonar.lib file in the
gm's library folder, like C:\Program Files\Game_Maker7\lib
or something like this.
  • 0

#11 Gonar

Gonar

    GMC Member

  • New Member
  • 161 posts

Posted 19 May 2010 - 07:51 AM

Hi
I made with GM8 lite a working example of what i think you want.

http://host-a.net/Gonar/Grab.zip

Hope to help you :D
  • 0

#12 Gonar

Gonar

    GMC Member

  • New Member
  • 161 posts

Posted 19 May 2010 - 08:24 AM

and a grid base example, but without snapping by using place_snap.

Link: http://host-a.net/Gonar/GrabGrid.zip

cu and gl
  • 0

#13 saim

saim

    GMC Member

  • New Member
  • 1085 posts

Posted 19 May 2010 - 01:49 PM

Game Maker seem to have a problem about using too many keyboard keys at the same time. People say it is a hardware problem, but I don't buy it.
Anyway, try to make your block respond to user input rather than making it follow the cursor (although I really can't find the problem in following the cursor).

<step event for block object>
if place_meeting(x,y,obj_cursor) && keyboard_check(ord('X'))
 drag=1
 else drag=0

if drag
{
x+=(keyboard_check(vk_right)-keyboard_check(vk_left))*SPEED //just a smart code to move left/right. SPEED is not the built-in speed, you'll need to define it
y+=(keyboard_check(vk_down)-keyboard_check(vk_up))*SPEED //the same for up'n'down
}

  • 0

#14 martianblue

martianblue

    GMC Member

  • New Member
  • 25 posts

Posted 19 May 2010 - 01:58 PM

Hi
I made with GM8 lite a working example of what i think you want.

http://host-a.net/Gonar/Grab.zip

Hope to help you :D



Sorry to chime in on someone else's thread, but I was wondering if you could rework this example to work with the mouse cursor, when the left button is pressed(just up and down)?

Edited by martianblue, 19 May 2010 - 01:59 PM.

  • 0

#15 Gonar

Gonar

    GMC Member

  • New Member
  • 161 posts

Posted 19 May 2010 - 03:43 PM

martianblue

try this
http://host-a.net/Gonar/GrabMouse.zip
http://host-a.net/Go...useNoCursor.zip
or
a general mouse grab example http://host-a.net/Go...ragAnObject.gm6
the collision checkings might be tricky, because with mouse, the movement isn't absolute in 2-axis

cu and gl

Edited by Gonar, 19 May 2010 - 03:54 PM.

  • 0

#16 martianblue

martianblue

    GMC Member

  • New Member
  • 25 posts

Posted 19 May 2010 - 05:31 PM

martianblue

try this
http://host-a.net/Gonar/GrabMouse.zip
http://host-a.net/Go...useNoCursor.zip
or
a general mouse grab example http://host-a.net/Go...ragAnObject.gm6
the collision checkings might be tricky, because with mouse, the movement isn't absolute in 2-axis

cu and gl



That's exactly what I was needing. Thank you very much. I really appreciate it.

Again, sorry for jumping in on your thread.
  • 0

#17 Gonar

Gonar

    GMC Member

  • New Member
  • 161 posts

Posted 19 May 2010 - 07:32 PM

no problem and the topic is of MattClark1000 :D

good luck with your project
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users