Check out the special deal below:

More Deals!


Click here to learn more and make more!


Check out the AllAdvantage FAQ!

Get paid to surf. Make money off the Internet instead of the other way around.

Created on: July 8, 1997
How do I make an Object move to where the user clicks?


How do I make an object move to where the user clicks? I'd also like the user to be able to click on an Object and them move to the Object. So how do you do it? There seems to be many of these type games in the libraries

Moshe Herskine
herskine@dircon.co.uk



(As usual there's a zip file with some source code you should download and look at first before you read this. The link's at the bottom of the page.)

I've seen a tutorial of how to do this somewhere but I haven't been able to locate it. I also seem to remember one done by Peter Hoggan, one of the old masters from when K&P was in its prime. Not only did the character move to where the user clicked but as the character moved closer he would become larger and as he moved farther away he would become smaller. It was a very smooth effect like the stuff Sierra does in their games.

But first we need to learn how to make an Object move to where the user clicks. We might as well use Active Objects for this since that's what just about everyone will want to move around anyway. The key to getting this effect to work is being able to store both the X and Y values of the mouse cursor when the user clicks the mouse. I do this by having a line of code that Always Sets the X Position and Y Position of an Active Object to the XMouse and YMouse values. The XMouse value stores the current horizontal position of the mouse. And the YMouse stores the current vertical position of the mouse.

So now we know where the user has clicked and where we need to move the object. All that's left is to move the object. The most easy way to do this is with four conditions.

I used the Blue Knight as the Object I wanted to move. And I used another Active Object I created, called Marker, to mark where I wanted to move the knight to.

First test to see if night is to the left of the marker if so add to knight's X Position.
Second, test to see if knight is to the right of the marker if so subtract from X Position of the knight.
Third, test to see if knight is above the marker and if so add to knight's Y Position.
Fourth, test to see if knight is below the marker and if so subtract from the Y Position of the knight.
You test for these condition continuously. And the knight will always move to where the user last clicked.

This also make the knight move to whatever Object a user clicks so there's no additional coding needed for that effect. The knight will move where the user clicks period if there's an object there or not. You'll need to add extra code to stop the knight from moving through your objects if that is not a desired effect. I've demonstrated this for Background Object Obstacles so you can see what you need to do.



As you can see making an Object move to where the user clicks is not that hard to implement. But it would be better to make the Object move towards where the user clicked and then stop if it encountered an obstacle. This would be a more effective effect. It's really not much harder to do so I'd recommend it.

You might want to consider integrating this movement into one of the built-in Movement Methods in Click & Create. This would make it much more versatile. Basically you just have the Speed set to zero whenever the object is where you want it. If it's not there then you set the Speed to a non-zero value and Set it to Always Look in the Direction of the Active Object that is Always Set to where the user clicked. Oh and if you try to do this you'll find out I've kind of tricked you. There's a little more to it than I have said. This is how I would do it under most circumstances. I didn't do it this way because I want to encourage more people to experiment with making their own Movement Methods. It might be more work but it does give you more power. And it doesn't use anywhere near the same amount of system resources. If I wanted to make a scrolling star field background with 200 stars I would use the Set X Position and Set Y Position to do it. Otherwise it may slow the application down too much.

I've included the source code for you to download which should help with your understanding.
clickmve.zip (22 KB): Download Click & Create source code for Recursive Programming.


This page and all of its contents are Copyright 1997-98 by Christopher Jay Craft.

1