Game Development Community

Making stationary object move with the mouse

by Westwood College Ohare (#0001) · in Torque Game Builder · 05/25/2006 (8:14 pm) · 1 replies

Im trying to make a stationary object rotate in the direction the mouse is pointing and was wondering how i should start. I would like to make a turrent like object rotate and aim where ever the mouse it pointing. And is there a way to change the image of the mouse pointer? Can ne1 help me?

#1
05/26/2006 (2:12 pm)
Here is a method of creating a custom cursor and having it follow the mouse. You will need a sprite of some sort that will be your cursor. Also, you will need to give that sprite a name in the SCRIPTING rollout of the edit tab in the level builder.

When you get that far add this function to your code where you feel is appropriate (i put mine in a separate file named mouseSprite.cs which i had to exec in my main.cs file:

function SceneWindow2D::onMouseMove(%this, %mod, %worldPosition)
{
myCursor.setPosition(%worldPosition);
}

function SceneWindow2D::onMouseLeave(%this, %mod, %worldPosition)
{
Canvas.showCursor();
}

function SceneWindow2D::onMouseEnter(%this, %mod, %worldPosition)
{
Canvas.hideCursor();
}

This will make the sprite follow the mouse cursor, plus act correctly when you enter or leave the game window.

To get your cursor to disappear so the custom cursor is the only one that is visible, add this code to the startGame function of your game.cs file:

// This code will hide your cursor
Canvas.hideCursor();

and, finally, you will want your cursor to reappear when you leave the level, so add this to your game.cs file in your endGame function:

//re-initializes your cursor
Canvas.showCursor();

I hope this helps you out, if you run into any problems let me know!!