Game Development Community

Need help with programming movement for a mouse

by Noah Nelsen · in Torque Game Builder · 08/21/2008 (4:58 am) · 5 replies

Ok so I want the character to move to an object when the object is clicked on. If I'm starting with this script

function sceneWindow2D::onMouseDown(%this, %modifier, %worldPosition, %mouseClicks)

then would I use an 'if' so see if it clicked on a object and which one? Also then I want the character to walk to the object making sure not to walk into anything and changing it's grapihcs direction as it goes. How would I program that? Thanks for your help.

#1
08/21/2008 (5:43 am)
In all honesty, this is what you should do:

*Go through multiple beginner tutorials.
*Learn the basics of TGB and TorqueScript.
*Realize that people on the TGB forums will only give you a chunk of code like you are asking for if they are really bored and don't have anything better to do. Especially since it appears like you didn't try anything before posting.

I'll point you in the right direction though. Use an onMouseDown callback on the class of object that you want to be clicked on. You'll probably need to create a special class for that. And then for the character object use a "moveTo" method.

I don't mean to come across as harsh, but you really learn so much more when you figure things out for yourself.
#2
08/21/2008 (7:31 am)
Ok so this is what I have so far, but I can't figure out how to identify the object. I've tried it many ways but none have worked.


function sceneWindow2D::onMouseDown(%this, %modifier, %worldPosition, %mouseClicks)
{
%theblocks.moveTo("38 23", 6, true, true, true, 0.01))

}

If anyone can just tell me how to identify the object in the TGB whether it's with a class or name. And if I'm calling it right.
#3
08/21/2008 (8:19 am)
@Noah - Check out the behaviors in the "Behavior Playground" project. I'm pretty sure there is an implementation of what you want in there. Also, have a look at the official docs, as I think there might be an example in there as well.

I'll try to find a specific location in a bit.
#4
08/21/2008 (12:56 pm)
A specific location would be great thanks.
#5
08/21/2008 (1:58 pm)
@Noah - OK. For inspiration, have a look at the mouseDraggable behavior in the "Behavior Playground" project. That will provide you with a way to grab mouse input behavior properly, including world position and modifier.

For your action, walk through the A* demo project to see how they perform pathfinding.

Finally, for appearance you have several options depending on your game type. If you are doing a simple overhead view, you can just rotate your sprite. If you want to change animation, that will be a bit more tricky. There's no specific example to point you to without knowing your project, but you get the idea on how to start this.

Hope that helps! =)