Game Development Community

Mouse events per class

by Pedro Vicente · in Torque 2D Beginner · 10/28/2013 (1:13 pm) · 1 replies

I am porting a iTorque 1.5 project to Torque 2D

I have this code that creates a "button", which is a sprite that responds to mouse events

iTorque 1.5 code



function CreateButton( %scenegraph_obj, %class, %image_map, %xpos, %ypos, %xsize, %ysize )
{
    %obj = new t2dStaticSprite() 
    { 
        scenegraph = %scenegraph_obj; 
        class = %class;
        useMouseEvents = "1";
    };
    %obj.setVisible( true );
    %obj.setImageMap( %image_map );
    %obj.setPosition( %xpos, %ypos );	
    %obj.setSize( %xsize, %ysize );       
    %obj.setLayer( 1 );
    return %obj;
}


If I call this function with

CreateButton( %this, "MainMenuButtonRun", "ButtonStartImageMap", 0,  0, 128, 128);

It creates a sprite "connected" to the class named "MainMenuButtonRun"

So, I can define

function MainMenuButtonRun::onTouchUp( %this, %modifier, %worldPosition, %clicks )
{ 
   
   
}


and when the button is clicked with the mouse this function is called.

Torque 2D code



When trying to do the same in Torque 2D

function CreateButton(%class, %image_map, %xpos, %ypos, %xsize, %ysize )
{
  %obj = new Sprite() 
  { 
    class = %class;
    useMouseEvents = "1";
  };
  %obj.setVisible( true );
  %obj.setImage( %image_map );
  %obj.setPosition( %xpos, %ypos );	
  %obj.setSize( %xsize, %ysize );     
  %obj.setSceneLayer( 1 );
  
  CaScene.add( %obj );
  return %obj;
}

the mouse event is not triggered

#1
10/28/2013 (1:32 pm)
At the SceneWindow level, you need to set the field UseObjectInputEvents to true.

And at the SceneObject level, the field is no longer called UseMouseEvents but rather UseInputEvents.