Game Development Community

Manage to create a HUD but having some touch problems

by Andrea Farid Marsili · in iTorque 2D · 08/08/2011 (7:41 am) · 20 replies

I used this topic to set my HUD but now I have problem with touch callback because touch is working only for the hud level but not for the game level.

If my problem cannot be resolved I thought about create different hud level and load them when I change level but I don't know how to unload the old level.
Thank you.

#1
08/08/2011 (8:04 am)
Ok, to lead different HUD I only have to load the level but I would prefere to enable touch callback.
#2
08/08/2011 (10:41 am)
@Andrea - Is useMouseEvents enabled for both t2dSceneWindows? Also, can you check to see if one is swallowing the events of the other?
#3
08/08/2011 (11:43 pm)
If I remove HUD sceneWindows the other scene can handle mouseevents so the problem is hud scene...
#4
08/09/2011 (6:28 am)
That's what I thought. You need to use t2dSceneGraph::pickPoint. Here is how you can resolve this:

1. Name each of your sceneGraphs something unique. You can do this in script or the editor. Fastest way is through script:

Main level:
new t2dSceneGraph(mainScreenGraph)

Hud Level
new t2dSceneGraph(hudSceneGraph)

2. When you get a touch down on the top scene graph, the HUD, push that to the next one. You can do this one of two ways (depending on which version of the engine you are using). 1.5 Preview 2 makes this easy:

OnTouchDown
function MyObject::onTouchDown(%this, %touchIDs, %worldPos)
{
   %objects = mainSceneGraph.pickPoint(%worldPos);

   %objectCount = %objects.getWordCount();

   for(%i = 0; %i < %objectCount; %i++)
   {
      %object = getWord(%objects, %i);

     %object.onTouchDown(%worldPos);
   }
}

Touch down bind via ActionMap:
function touchesDown(%touchIDs, %touchesX, %touchesY)
{
   %worldPos = sceneWindow2D.getWorldPoint(%touchX, %touchY);
   
   %objects = mainSceneGraph.pickPoint(%worldPos);

   %objectCount = %objects.getWordCount();

   for(%i = 0; %i < %objectCount; %i++)
   {
      %object = getWord(%objects, %i);

     %object.onTouchDown(%worldPos);
   }
   
}

This manually forces a touch down event to the main scene graph. Make sense?
#5
08/09/2011 (7:19 am)
Ok I have to force touchDown event to the main sceneGraph giving a name to the sceneGraph into my level's code but I cannot understand what's the purpose of the code inside onTouchDown.
#6
08/09/2011 (8:50 am)
Ah, sorry. Let me post it with comments:

// MyObject is just an example name. This can be anything you name
function MyObject::onTouchDown(%this, %touchIDs, %worldPos)
{
   // Picks objects intersecting point with optional group/layer masks
   // %worldPos is The coordinate of the point as 
   // either ("x y") or (x,y)
   // Returns a space separated list of object IDs that intersect
   %objects = mainSceneGraph.pickPoint(%worldPos);

   // Find out how many objects were picked/intersected
   %objectCount = %objects.getWordCount();

   // Loop through all the objects to trigger their onTouchDown
   for(%i = 0; %i < %objectCount; %i++)
   {
      // Get an object at the current index (%i)
      // starts at 0, then increments by 1 each loop
      %object = getWord(%objects, %i);

      // Trigger the onTouchDown for the current object
      %object.onTouchDown(%worldPos);
   }
}

Alright, so let me try to break it down even further. Let's say you have an object called ShootButton in your HUD level. When you touch it, its onTouchDown is called:

function ShootButton::onTouchDown(%this, %touchID, %worldPos)
{
}

At that point, you have a world position that can be used on . What you do is start pushing that to scene graphs that are under your HUD level. You should always have access to those scene graphs if they are loaded. That's where pickPoint comes into play. Objects on your mainSceneGraph are not getting the automatic onTouchDown callbacks, but you can call them yourself by using pickPoint.

This is exactly how onTouchDown is generated in the C++ code, you just cannot see it unless you step through it.
#7
08/10/2011 (12:39 am)
Thank you Mich, I only need a couple of explanations.
"MyObject" is into the level that will be loaded into the hudSceneGraph or into the mainSceneGraph?

This code trigger the onTouchDown for the current object.
%object.onTouchDown(%worldPos);

"MyObject" and "%object" aren't the same, right? I can made a big and transparent object big as the screen to recognize objects inside the other level.

PS: the same code will work also with onMouse callback?

Thank you mich

EDIT:
I renamed the sceneGraph inside my level then I copied your code and create a big transparent object in my hud level but I get this error:

game/scripts/TouchRecognizer.cs (9): Unknown command getWordCount.
  Object (1137) t2dStaticSprite -> t2dSceneObject -> BehaviorComponent -> DynamicConsoleMethodComponent -> SimComponent -> SimObject
#8
08/10/2011 (6:18 am)
Gah, sorry. I messed up that function.

1. MyObject would exist in your HUD level.

2. %object and MyObject are not the same. Poor naming choice on my end. %object would be what exists on the game level, like a player.

3. The code should have been:

%objectCount = getWordCount(%objects);
#9
08/10/2011 (6:57 am)
@Mich:
The problem was getWordCount now althing work like a charm.
Can I say it?
Yes, I can.
I love you Mich.
#10
08/10/2011 (8:09 am)
Just doing my job =)

Glad it works for you.
#11
10/13/2011 (12:26 am)
Can you help me plz in my game controller
when the game start, it load main menu, and when I chose the level it start level
for examble level1.t2d

on level1.t2d the playerObject there
now I need to add
new t2dSceneGraph(mainScreenGraph)

new t2dSceneGraph(hudSceneGraph)
to add controller buttons for my game
where can I add these lines ?
do I need to make 2 levels file one level1.t2d and one hud.t2d ?
can you explain this to me im still on learning stage

#12
10/13/2011 (11:11 pm)
Yen you need to' building Two different levels one for gui one for your game
#13
10/14/2011 (1:42 am)
thank you Andrea now I can see both images from two files

now I will try to give one image as player and one as button to control player

same like Michael explained

thanks again
#14
10/14/2011 (7:14 am)
I renamed the
level1.t2d
and hud.t2d
to

new t2dSceneGraph(mainScreenGraph)
new t2dSceneGraph(hudSceneGraph)

and added this
function upButton::onTouchDown(%this, %touchID, %worldPos)
{ 
	%objects = mainSceneGraph.pickPoint(%worldPos);

%objectCount = getWordCount(%objects);
   for(%i = 0; %i < %objectCount; %i++)
   {
      %object = getWord(%objects, %i);

     %object.onTouchDown(%worldPos);
     
    
     
   }
   
}

added that to games.cs
but i want when upButton touched it do this
echo("Finger " @ %touchID @ " touched the Cloud at " @ %worldPos);

   // If we are already dragging, do nothing
   // Otherwise, assign this finger to the cloud's touch ID
   if(upButton.touchID $= "")
     {
      upButton.touchID = %touchID;
      player.moveUp(1);

     }

player.moveUp(1);
this is my character on level1 which i want to move it up
where can i add this line plz ?
#15
10/14/2011 (9:44 am)
Edit your level (not the GUI one) with a text editor and change:
%levelContent = new t2dSceneGraph() {

into

%levelContent = new t2dSceneGraph(mainSceneGraph) {

The "pickPoint" code is for a transparent actor in your HUD scene which will enable touch events also on mainSceneGraph.
#16
10/14/2011 (11:30 am)
Yes thank u Andrea

If u can help in this part
My character on level1 name player
I need to add this line to
player.moveUp(1);

to

function upButton::onTouchDown(%this, %touchID, %worldPos)
{ 
	%objects = mainSceneGraph.pickPoint(%worldPos);

%objectCount = getWordCount(%objects);
   for(%i = 0; %i < %objectCount; %i++)
   {
      %object = getWord(%objects, %i);

     %object.onTouchDown(%worldPos);
     
    
     
   }
   
}

this line make my player go up
if i touch upButton this player go up

Where can i add this please ?
#17
10/15/2011 (2:26 am)
Michael said
Quote:2. %object and MyObject are not the same. Poor naming choice on my end. %object would be what exists on the game level, like a player.


please if %object is the player how can I apply
this
player.moveUp(1);
to it

i have object named player on mainscene
and upButton on hud scene
#18
10/16/2011 (1:07 am)
You only have to call moveUp inside touchDown callback of upButton
#19
10/16/2011 (1:59 am)
yes please I fix it
callback upButton
:)
now its working good
thanks for help :)
#20
01/16/2012 (9:38 am)
I'm using this code in TGB and it doesnt work. I have my Inventory bar slide up and down on mouseEnter/Leave but the level it self doesnt have mouse events. What gives ?