Game Development Community

onMouseDown Help! [Resolved]

by Neal Guzman · in Torque Game Builder · 06/21/2011 (10:38 am) · 25 replies

I have tried and doesn't work.
I feel I'm missing something important somewhere.

What I'm trying to do is to get the mouse button to fire a bullet.

In this case the bullet is a clone of the actual bullet.

shoot works fine with keybinds.

keys.bindCMD("keyboard","space","shoot();","stopshoot();");



In Game.cs
in the startgame function

// Add these two lines
   Canvas.hideCursor();
   sceneWindow2D.setUseWindowMouseEvents(true);



function SceneWindow2D::onMouseDown(%this)
{
shoot();
}

Which should go to the shoot function

function shoot()
{

%currentclone = pbullet.clone(1);

%currentclone.setposition(player.getposition());

%currentclone.setlinearvelocityX(100);

}

About the author

I am an GUI Artist, HUD Artist, Texture Artist, GUI Editor, and Programmer and Game Designer/Developer. I know and still am learning c# and torque script. I earned my Bachelors' of Science in Game Design and Development on 12/16/11

Page «Previous 1 2
#1
06/21/2011 (12:09 pm)
Try:
sceneWindow2D.setUseMouseEvents( true );
#2
06/21/2011 (12:50 pm)
That line I already tried if you look again at the above JS would it matter if there is a space in there?

the line

sceneWindow2d.setUseMouseEvents(true); is right under the hide cursor or canvas.hidecursor();

Does there have to be a keybind set up for it or should it just work?
#3
06/21/2011 (12:51 pm)
The line you have and the one I posted are different.

That should activate the mouse events on the scene window, then the callback should work. No need to do a bind.
#4
06/21/2011 (12:56 pm)
Noticed you didn't have setUseWindowMouseEvents(true);
the line after I posted

The log shows this as an Error.

sceneWindow2d.setUseMouseEvents(true);

game/gameScripts/game.cs (23): Unknown command setUseMouseEvents.
Object sceneWindow2D(1316) sceneWindow2D -> t2dSceneWindow -> GuiControl -> SimGroup -> SimSet -> SimObject
#5
06/21/2011 (1:03 pm)
hmm... interesting. I've had trouble with the method not working sometimes and had to use the field acces like so:
sceneWindow2D.useWindowMouseEvents = true;

or
sceneWindow2D.useObjectMouseEvents = true;
#6
06/21/2011 (1:11 pm)
None of those work either.

But no script errors either.
#7
06/21/2011 (1:18 pm)
Are you sure that sceneWindow2D is valid where you're setting the mouse events?
#8
06/21/2011 (1:23 pm)
function startGame(%level)
{
   Canvas.setContent(mainScreenGui);
   Canvas.setCursor(DefaultCursor);
   new ActionMap(moveMap);
  Canvas.hideCursor();  
  sceneWindow2D.setUseWindowMouseEvents(true);  
    moveMap.push();
   $enableDirectInput = true;
   activateDirectInput();
   enableJoystick();
   sceneWindow2D.loadLevel(%level);   
   Canvas.pushdialog(splash);  
    
}

This is where I have it calling from I have no idea where to find out if it's valid.
#9
06/21/2011 (1:25 pm)
I don't think it will be since you're loading the level after you're telling it to use mouse events.
#10
06/21/2011 (1:26 pm)
even if i put it after loading the level it still doesn't do anything.
#11
06/21/2011 (1:29 pm)
I suggest you place that in the onlevelloaded callback.
#12
06/21/2011 (1:36 pm)
I don't have an onlevelloaded callback

the startgame is a default.

#13
06/21/2011 (1:37 pm)
http://tdn.garagegames.com/wiki/TGB/Behaviors/Callbacks#onLevelLoaded
#14
06/21/2011 (1:52 pm)
I read it but really no idea how to use it.

Tried the following

function scene2dWindow::onLevelLoaded()
{
sceneWindow2D.useWindowMouseEvents = true;  
}

The screen went entirely blank and all my game components where gone.

Btw Thanks for trying to help anyways. I can always just stick to the keybinds being they actually work.
#15
07/17/2011 (12:09 am)
Hi Neal,

try this:

function t2dSceneWindow::onMouseDown(%this, %modifier, %worldPos, %mouseClicks)
{
shoot();
}
#16
07/17/2011 (12:09 am)
Hi Neal,

try this:

function t2dSceneWindow::onMouseDown(%this, %modifier, %worldPos, %mouseClicks)
{
shoot();
}
#17
07/29/2014 (6:16 pm)
Amjad Yahya I tried that as well it doesn't work.

function startGame(%level)
{
   Canvas.setContent(mainScreenGui);
   Canvas.setCursor(DefaultCursor);
   new ActionMap(moveMap);
   moveMap.push();
   $enableDirectInput = true;
   activateDirectInput();
   enableJoystick();
   sceneWindow2D.loadLevel(%level);  
   Canvas.pushdialog(splash); 
   //Canvas.hideCursor();
   sceneWindow2D.setUseWindowMouseEvents(true);
   sceneWindow2D.setUseMouseEvents( true ); 
    
}


//---------------------------------------------------------------------------------------------
// endGame
// Game cleanup should be done here.
//---------------------------------------------------------------------------------------------

function endGame()
{
   sceneWindow2D.endLevel();
   moveMap.pop();
   moveMap.delete();
}

/////////////////////My Space Mayhem Code///////////////////////////////////////

function startmygame()
{

$Enemyset= new simset();
$refilldropset= new SimSet();
//show HUD
//canvas.schedule(4000,pushdialog,hud2);
Canvas.popdialog(titlepage);
canvas.pushdialog(HUD2);
//Canvas.hideCursor(); 
 sceneWindow2D.setUseObjectMouseEvents(true) ; 
 sceneWindow2D.setUseWindowMouseEvents(true);  
 //t2dSceneWindow.setUseObjectMouseEvents(true);
 //t2dSceneWindow.setUseWindowMouseEvents(true);
//$Themeaudio = alxPlay(LevelMusicAudio);

//bind some keys to control the player
new actionmap(keys);
 
keys.bindCMD("keyboard","w","MoveUp();","stopmoveup();");
keys.bindCMD("keyboard","a","MoveLeft();","stopmoveleft();");
keys.bindCMD("keyboard","d","MoveRight();","stopmoveright();");
keys.bindCMD("keyboard","s","MoveDown();","stopmovedown();");
keys.bindCMD("keyboard","space","shoot();","stopshoot();");
keys.bindCMD("keyboard","m","shoot();","stopfire();");
//to activate something push it to deactivate, pop it
keys.push();


//set up player variables
player.score = 0;
player.shield = 100;
player.lives = 5;
scoretext.text = "Score = "@ player.score;
fuelbaricon.extent = 268;
bosshealthicon.extent= 300;
$fuelbar = 100;

//get x and y position
%y = getrandom( -8.3 , -31.6 ) ;
%x = getrandom( -41.9 , -23.9) ;


//codeing for setting lives visible
liveicon5.setvisible(1);
liveicon4.setvisible(1);
liveicon3.setvisible(1);
liveicon2.setvisible(1);
liveicon1.setvisible(1);

schedule(4000,0,enemyspawn);

//set the clone to the x and y position

player.setvisible(1);
player.setposition(%x, %y);


//call up spawns
bossspawn();
fuelloss();
fuelstation();
refilldrop();


$timmer = 0;
timetext.settext("Time = " SPC $timmer);
timmer();

}


function MoveUp()
{
player.setlinearvelocityY(-50);
}

 function stopmoveup()
{
 player.setlinearvelocityY(0);
}

function MoveDown()
{
player.setlinearvelocityY(50);
}

function stopmovedown()
{
player.setlinearvelocityY(0);
}

 function MoveLeft()
{
player.setlinearvelocityX(-50);
}

function stopmoveleft()
{
player.setlinearvelocityX(0);
}

function MoveRight()
{
player.setlinearvelocityX(50);
}

function stopmoveRight()
{
player.setlinearvelocityX(0);
}


function scene2dWindow:::onMouseDown(%this, %modifier, %worldPos, %mouseClicks)
{
shoot();
}


function win()
{
$boss1=false;

%count = $enemyset.getcount();

 for(%i=0;%i<%count;%i++)
     {
      %currentenemy= $enemyset.getobject(%i);
      %currentenemy.safedelete();
     }

//deactivate keybinds
keys.pop();

cancel ($enemySpawnSchedule);
cancel ($fuelloss);
cancel ($bossspawn);
cancel ($bossmove);

Canvas.Pushdialog(YouWin);
canvas.schedule(2000,popdialog,YouWin);
canvas.schedule(2000,pushdialog,Credits);
canvas.schedule(3000,popdialog,Credits);
canvas.schedule(4000,pushdialog,endsplash);

Canvas.popdialog(HUD2);
}

 function lose()
{
 %count = $enemyset.getcount();

 for(%i=0;%i<%count;%i++)
     {
      %currentenemy= $enemyset.getobject(%i);
      %currentenemy.safedelete();
     }

//deactivate keybinds
keys.pop();

// cancel schedules
cancel ($enemySpawnSchedule);
cancel ($fuelloss);

canvas.pushdialog(EndSplash);

//bring up replay screen
//canvas.schedule(2000,pushdialog,endsplash);
}
#18
07/30/2014 (7:09 am)
Add this somewhere and see what happens. It just echos to log, so you shouldn't see anything on screen.
function sceneWindow2D::onMouseDown(%this, %a, %b, %c)
{
    echo(" -- onMouseDown " @ %a @ " " @ %b @ " " @ %c);
}
#19
07/30/2014 (7:58 am)
I added that function and nothing showed up in the log.
#20
07/30/2014 (8:01 am)
Cleaned up the code abit and it shouldn't be this hard to do a mouse bind.

function startGame(%level)
{
   Canvas.setContent(mainScreenGui);
   Canvas.setCursor(DefaultCursor);
   
   new ActionMap(moveMap);   
   moveMap.push();
   
   $enableDirectInput = true;
   activateDirectInput();
   enableJoystick();
   Canvas.pushdialog(splash); 
   sceneWindow2D.loadLevel(%level);  
   //
   sceneWindow2D.setUseWindowMouseEvents(true);
   sceneWindow2D.setUseMouseEvents( true ); 
   //Canvas.hideCursor();
   //
}

function activategame ()
{   
   $Enemyset= new simset();
   $refilldropset= new SimSet();
   Canvas.popdialog(titlepage);
   canvas.pushdialog(HUD2);
   //  Canvas.hideCursor(); 
   //$Themeaudio = alxPlay(LevelMusicAudio);

   //bind some keys to control the player
   new actionmap(keys);
 
keys.bindCMD("keyboard","w","MoveUp();","stopmoveup();");
keys.bindCMD("keyboard","a","MoveLeft();","stopmoveleft();");
keys.bindCMD("keyboard","d","MoveRight();","stopmoveright();");
keys.bindCMD("keyboard","s","MoveDown();","stopmovedown();");
keys.bindCMD("keyboard","space","shoot();","stopshoot();");
keys.bindCMD("keyboard","m","shoot();","stopshoot();");
//to activate something push it to deactivate, pop it
keys.push();
//moveMap.bind( "mouse", "button1", "shoot();" );

//set up player variables
player.score = 0;
player.shield = 100;
player.lives = 5;
scoretext.text = "Score = "@ player.score;
fuelbaricon.extent = 268;
bosshealthicon.extent= 300;
$fuelbar = 100;

//get x and y position
%y = getrandom( -8.3 , -31.6 ) ;
%x = getrandom( -41.9 , -23.9) ;

//codeing for setting lives visible
liveicon5.setvisible(1);
liveicon4.setvisible(1);
liveicon3.setvisible(1);
liveicon2.setvisible(1);
liveicon1.setvisible(1);

schedule(4000,0,enemyspawn);

//set the clone to the x and y position

player.setvisible(1);
player.setposition(%x, %y);


//call up spawns
bossspawn();
fuelloss();
fuelstation();
refilldrop();


$timmer = 0;
timetext.settext("Time = " SPC $timmer);
timmer();

}

function sceneWindow2D::onMouseDown(%this, %a, %b, %c)  
{  
    echo(" -- onMouseDown " @ %a @ " " @ %b @ " " @ %c);  
}  

function MoveUp()
{
player.setlinearvelocityY(-50);
}

 function stopmoveup()
{
 player.setlinearvelocityY(0);
}

function MoveDown()
{
player.setlinearvelocityY(50);
}

function stopmovedown()
{
player.setlinearvelocityY(0);
}

 function MoveLeft()
{
player.setlinearvelocityX(-50);
}

function stopmoveleft()
{
player.setlinearvelocityX(0);
}

function MoveRight()
{
player.setlinearvelocityX(50);
}

function stopmoveRight()
{
player.setlinearvelocityX(0);
}
Page «Previous 1 2