(Solved) Move "moving object" with mouse?

K

Karlabos

Guest
So I have an object which is running towards the player (another object called player, that is), with a certain speed.
And I wish to make it so that this object is able to be moved with the mouse. But I don't want it to teleport to wherever the mouse is. (like in just x=mouse_x; y=mouse_y; stuff)
I want it so that whenever the mouse is moved a bit upwards then the object is also moved a little upwards, but with respect to the position it already was on the previous step and so on...

How can I do it?
 
K

KitKatarine

Guest
you could try move_towards_point(mouse_x, mouse_y, 5) in your object's step event, which will move the object toward your mouse's coordinates at a speed of 5 but that will make it move continuously upwards toward the mouse.

So with that you could also go

if distance_to_point (mouse_x, mouse_y) = 32
object.speed = 0

so that will keep it 32 pixels away from the mouse.

(Edit again: I'm a beginner though, this is just what I use to move my player around my game, edited slightly.)
 
J

Jordan Vigay

Guest
try this (replace [object] with the object you want to move towards the mouse)

step event:

if point_distance([object].x,[object].y,mouse_x,mouse_y) > 3 {
direction = point_direction([object].x,[object].y,mouse_x,mouse_y);
speed = 3;
}
 
K

Karlabos

Guest
Huh.. I may not have made myself very clear...

I don't want the object to move towards the mouse. I want it to make the movements the mouse does with respect to its current position. Like it is another mouse object. When the player movesd the mouse both this object and the mouse are moved...
But I can't just use the mouse_x,mouse_y coordinates because the object coordinates may not coincide with them, since it also changes its position when the mouse is not in use...
Like if in a step the player moves the mouse 10 upwards then the enemy has also its y decreased by 10, but still keeps with its pre-set movement towards the player.
 
A

anomalous

Guest
Total guess here.

I think you'll need to figure out how to "start" the mouse linked movement.
You can figure that out I assume is it when mouse pressed and held for example?


For the object:
draw end event:
mouse_x_previous = mouse_x;
mouse_y_previous = mouse_y;

step event:
dx = mouse_x - mouse_x_previous;
dy = mouse_y - mouse_y_previous;

object.x = object.x+dx;
object.y = object.y+dy;

You then do your normal speed stuff on top of that, or before it, however you need.
 
K

Karlabos

Guest
I see, thanks.

So this means the events at step are executed "after" the ones in draw then?
 
A

anomalous

Guest
I see, thanks.So this means the events at step are executed "after" the ones in draw then?
No, the rule with setting a previous value is that it should be set in an event AFTER the event its needed.
You DO need to initialize those variables in a create event, (mouse_previous_x) however, they can be init to mouse_x/y

Draw is after step (unless you create an instance during step!!), so if you only use it in the step event, draw is after step. You could put it in the end step event (which is after step), or any that is after (entirely after) step.

Look up xprevious and yprevious, they are built in variables for the x, y object values, and they may mention when they are set (for comparison). Those are built in so if you need them, that's what they are, the previous steps value for x/y.
 
Top