Game Development Community

EDIT: Closed

by Tyler Slabinski · in Torque Game Builder · 04/09/2009 (7:26 pm) · 16 replies

I am closing this thread and making a new one. Mostly because the topic has changed 5 times, and some people may be confused.

#1
04/10/2009 (11:51 am)
Bump
#2
04/10/2009 (1:38 pm)
Ok, here is my entire script:

function playerClass::onLevelLoaded(%this, %scenegraph)
{
     $player = %this;
      
      moveMap.bindCmd(mouse0, button0, "playerShoot();", "playerShootStop();");
}

function playerLeft()
{
    echo("Shooting");
}

function playerLeftStop()
{
    echo("Not shooting");
}

I am now getting this error (It does not say error, it just says it in grey):

game/gameScripts/game.cs (23): Unable to find function enableJoystick
#3
04/10/2009 (2:13 pm)
That looks pretty close, it looks like your function names are playerLeft and playerLeftStop but you are trying to call playerShoot and playerShootStop, if you change those it may work. Something like:


function playerClass::onLevelLoaded(%this, %scenegraph)
{
$player = %this;

moveMap.bindCmd(mouse0, button0, "playerShoot();", "playerShootStop();");
}

function playerShoot()
{
echo("Shooting");
}

function playerShootStop()
{
echo("Not shooting");
}
#4
04/10/2009 (2:20 pm)
Ohh, sorry. My actual code is much longer, and is this:

function bowClass::onLevelLoaded(%this, %scenegraph)
{
     $bow = %this;
      
     moveMap.bindCmd(mouse0, button0, "drawBow();", "releaseBow();");
}

function drawBow()
{
    $bow.enabled = true;     
    $bow.setAnimationFrame(0);
    $bow.setVisible(true);
    $bow.playAnimation(drawBow, false, 0, false);
}

function releaseBow()
{
    $bow.enabled = true;     
    $bow.setAnimationFrame(0);
    $bow.setVisible(true);
    $bow.playAnimation(releaseBow, false, 0, false);
}

I just did the code in my last post to show a smaller example script.

I have tried your code and got the same exact results. Nothing.
#5
04/10/2009 (2:23 pm)
I don't think TGB lets you map an action directly to a generic mouse button press. As far as I know you can only map function calls to presses on specific objects.

To pick up on mouse clicks you need to define the onMouseDown method for the object's class , and enable mouse events for the object.

You could handle this a couple of ways, first if you just have specific targets that you want the user to shoot, then implement a function like this:
function shootableObject::onMouseDown(%this, %modifier, %worldPosition, %clicks) {
  //do something to the shootable object
}

Alternatively, if you want any click anywhere to register, then you'll want an object that covers the entire clickable space. I'm guessing you have some sort of t2dSceneObject acting as a backdrop for your action, like a TileMap, or a big t2dStaticSprite. If you enable Mouse Events, and implement the onMouseDown function for your background object, then it shouldn't matter where you click, as long as the mouse is over the background object the function will always get called.

The t2dSceneWindow object also has a callback function for onMouseDown, as well. It seems that this should also let you pick up on any mouse click, but I haven't played around with it any.
#6
04/10/2009 (2:27 pm)
Oh, you posted that new post while I was typing, so you could do something like this:

function backgroundObject::onMouseDown(%this, %modifier, %worldPosition, %clicks) { 
 drawBow();
}

function backgroundObject::onMouseUp(%this, %modifier, %worldPosition, %clicks) {
 releaseBow();
}

where 'backgroundObject' is the class of whatever object you are using as a backdrop. You could possibly do something very similar using the t2dSceneWindow, but I haven't messed with that, so I don't know exactly how you would do that.
#7
04/10/2009 (2:35 pm)
I am trying to do something similiar to these:

armorgames.com/play/133/bowman
armorgames.com/play/588/bowmaster-prelude

A drag and shoot effect for the bow.

Since my plan wouldn't work. Is there another way to do this?
#8
04/10/2009 (3:40 pm)
This should do what you want:

function t2dSceneWindow::onMouseDown(%this, %modifier, %worldPosition, %clicks) {
	//click
}

function t2dSceneWindow::onMouseDragged(%this, %modifier, %worldPosition, %clicks) {
	//drag
}

function t2dSceneWindow::onMouseUp(%this, %modifier, %worldPosition, %clicks) {
	//let go
}
#9
04/10/2009 (6:14 pm)
How would I get the position of the mouse? I could get the position if I attached an object to it, but I would prefer to know how to do it directly.
#10
04/10/2009 (6:17 pm)
If you read the documentation, it tells you what the arguments passed in are.

%modifier = any modifier keys that were pressed
%worldPosition = the mouse position
%clicks = how many times the mouse was clicked
#11
04/10/2009 (6:42 pm)
Nevermind, I seem to have figured it out. Thanks.
#12
04/10/2009 (7:02 pm)
Ok, now I got a new problem... How would I get the X position of the mouse only?
#13
04/10/2009 (8:49 pm)
That would be the getWord method.

This will give you the X and Y mouse positions:
%xPosition = getWord(%worldPosition,0);
%yPosition = getWord(%worldPosition,1);
#14
04/11/2009 (7:21 am)
Ahh, thanks very much!

I know many other people would want this too, I will put the math into a resource.
#15
04/11/2009 (8:29 am)
Did you try the code I gave you? I'm surprised that didn't work. See, the variable %worldPosition is a vector. It's a string containing a list of space separated values.

The function getWord() breaks the string into words based on where the spaces are, then returns the word you specify. So if %worldPosition contains the string "12 15" then it contains two words, 12 is word 0 and 15 is word 1. So getWord("12 15",0) returns 12 and getWord("12 15",1) returns 15.

%worldPosition, as passed in to the mouse callback methods, contains the x coordinate in word position 0, and the y coordinate in word position 1. So getWord(%worldPosition,0) returns the x coordinate, and getWord(%worldPosition,1) returns the y coordinate. I hope I didn't confuse you with my code section above by including how to get both variables, I figured you might eventually want to get the y coordinate as well, but you can get either one in isolation.
#16
04/11/2009 (9:20 am)
Well... Don't I feel stupid. I could have just used the function:

t2dAngleBetween()

Can anyone tell me how to use this function? I tried this:

%angle = t2dAngleBetween($startPosition, $endPosition);

Although, it seems to give me a distance. Not an angle...