Help with mouse
by GCBeany · in Torque Game Builder · 04/15/2006 (1:06 pm) · 1 replies
I'm trying to get an object to move around on the screen when I click and drag it.
I've been working with this tutorial:
http://tdn.garagegames.com/wiki/Torque_2D/Getting_Started/ObjectSelection1Tutorial
... and trying to work it into my project. For some reason it doesn't work. Any help would be greatly appreciated. Here is what my script looks like:
function startGame(%level)
{
// Set The GUI.
Canvas.setContent(mainScreenGui);
Canvas.setCursor(DefaultCursor);
moveMap.push();
sceneWindow2D.loadLevel(%level);
setupT2DScene();
}
function setupT2DScene()
{
// Create our scenegraph
new t2dSceneGraph(t2dScene);
// Associate Scenegraph with Window.
sceneWindow2D.setSceneGraph( t2dScene );
// Setup all the images
SetupImages();
simpleMouse();
// Create Background
%background = new t2dStaticSprite() { scenegraph = t2dScene; };
%background.setPosition("0 0");
%background.setSize("100 75");
%background.setImageMap(purpbgImageMap);
%background.setLayer(31);
}
// Setup the images
function SetupImages ()
{
datablock t2dImageMapDatablock(HighlightImageMap)
{
imageMode = full;
imageName = "~/data/images/magicbox/Highlight";
};
}
function simpleMouse()
{
$test = new t2dStaticSprite() { sceneGraph = t2dScene; };
$test.setImageMap( highlightImageMap );
$test.setSize( "5 5" );
$test.setPosition( "0 0" );
$test.isSelectable = true;
$test.objectName = "test box";
}
function sceneWindow2D::onMouseMove( %this, %mod, %worldPos, %mouseClicks )
{
%this.mousePos = %worldPos;
%this.objectFollowCheck();
}
function sceneWindow2D::onMouseDragged( %this, %mod, %worldPos, %mouseClicks )
{
%this.mousePos = %worldPos;
%this.objectFollowCheck();
}
function sceneWindow2D::onMouseDown( %this, %mod, %worldPos, %mouseClicks )
{
//lets get a list of all the objects at the clicked point in the t2dScene
%objList = t2dScene.pickPoint(%worldPos);
//lets get a count of how many objects in the list
%objCount = getWordCount(%objList);
//we will start looping through the list
for(%i=0;%i<%objCount;%i++)
{
//grabing the entry corresponding to the loop
%obj = getWord(%objList, %i);
//if we find an object in the list that "isSelectable = true"
if(%obj.isSelectable)
{
//we toggle a value so we know we found an object that "isSelectable"
%selected = true;
//we kick out of the loop
%i = %objCount;
}
}
//if we found an "isSelectable" object
if(%selected)
{
//we then store that object as the selectedObj
%this.selectedObj = %obj;
%this.objectSelected = true;
}
}
function t2dSceneWindow::objectFollowCheck()
{
if(%this.objectSelected)
{
%obj = %this.selectedObj;
%mousePos = %this.mousePos;
%obj.setPosition(%mousePos);
}
}
//---------------------------------------------------------------------------------------------
// endGame
// Game cleanup should be done here.
//---------------------------------------------------------------------------------------------
function endGame()
{
sceneWindow2D.endLevel();
moveMap.pop();
}
I've been working with this tutorial:
http://tdn.garagegames.com/wiki/Torque_2D/Getting_Started/ObjectSelection1Tutorial
... and trying to work it into my project. For some reason it doesn't work. Any help would be greatly appreciated. Here is what my script looks like:
function startGame(%level)
{
// Set The GUI.
Canvas.setContent(mainScreenGui);
Canvas.setCursor(DefaultCursor);
moveMap.push();
sceneWindow2D.loadLevel(%level);
setupT2DScene();
}
function setupT2DScene()
{
// Create our scenegraph
new t2dSceneGraph(t2dScene);
// Associate Scenegraph with Window.
sceneWindow2D.setSceneGraph( t2dScene );
// Setup all the images
SetupImages();
simpleMouse();
// Create Background
%background = new t2dStaticSprite() { scenegraph = t2dScene; };
%background.setPosition("0 0");
%background.setSize("100 75");
%background.setImageMap(purpbgImageMap);
%background.setLayer(31);
}
// Setup the images
function SetupImages ()
{
datablock t2dImageMapDatablock(HighlightImageMap)
{
imageMode = full;
imageName = "~/data/images/magicbox/Highlight";
};
}
function simpleMouse()
{
$test = new t2dStaticSprite() { sceneGraph = t2dScene; };
$test.setImageMap( highlightImageMap );
$test.setSize( "5 5" );
$test.setPosition( "0 0" );
$test.isSelectable = true;
$test.objectName = "test box";
}
function sceneWindow2D::onMouseMove( %this, %mod, %worldPos, %mouseClicks )
{
%this.mousePos = %worldPos;
%this.objectFollowCheck();
}
function sceneWindow2D::onMouseDragged( %this, %mod, %worldPos, %mouseClicks )
{
%this.mousePos = %worldPos;
%this.objectFollowCheck();
}
function sceneWindow2D::onMouseDown( %this, %mod, %worldPos, %mouseClicks )
{
//lets get a list of all the objects at the clicked point in the t2dScene
%objList = t2dScene.pickPoint(%worldPos);
//lets get a count of how many objects in the list
%objCount = getWordCount(%objList);
//we will start looping through the list
for(%i=0;%i<%objCount;%i++)
{
//grabing the entry corresponding to the loop
%obj = getWord(%objList, %i);
//if we find an object in the list that "isSelectable = true"
if(%obj.isSelectable)
{
//we toggle a value so we know we found an object that "isSelectable"
%selected = true;
//we kick out of the loop
%i = %objCount;
}
}
//if we found an "isSelectable" object
if(%selected)
{
//we then store that object as the selectedObj
%this.selectedObj = %obj;
%this.objectSelected = true;
}
}
function t2dSceneWindow::objectFollowCheck()
{
if(%this.objectSelected)
{
%obj = %this.selectedObj;
%mousePos = %this.mousePos;
%obj.setPosition(%mousePos);
}
}
//---------------------------------------------------------------------------------------------
// endGame
// Game cleanup should be done here.
//---------------------------------------------------------------------------------------------
function endGame()
{
sceneWindow2D.endLevel();
moveMap.pop();
}
Torque Owner GCBeany