Start Page » DragonScript Scripting Language » Abstraction Layers: How you want to build your Game » Behavior Elements » ECBehaviorTouchSensor
Behavior element behavior adding touch sensor support.
Attaches a TouchSensor resource to the behavior element. By default the collision filter category is set to BaseGameApp.CollisionFilterBit.trigger and the collision filter mask is set to BaseGameApp.CollisionFilterBit.actorAI . This causes actors to trigger this touch sensor unless they have the CollisionFilterBit.trigger bit cleared. Modify the collision filter to allow other elements to trigger this touch sensor.
Upon being touched the touch sensor notifies the listeners.
The TouchSensor resource can be queried for the colliders currently touching it. Furthermore colliders entering and leaving trigger a notification allowing elements to track elements of interest.
If the ECBehaviorCollider behavior is present in the behavior element the TouchSensor is attached to the collider.
The behavior retrieves the owner of colliders inside the touch sensor. If these owners are elements they are stored in a list of elements touched by the owner element. This list is updated when elements enter or leave the touch sensor.
The owner behavior element as well as other ECBehavior subclasses can add a listener to be notified if elements enter and exit the touch sensor.
This element behavior can be present multiple times in a BehaviorElement. In this case use a unique identifier to distinguish the individual touch sensors.
Element class properties have the prefix touchSensor.
or touchSensor({id}).
if id is not empty.
Touch sensor shape.
touchSensor.shape
or touchSensor({id}).shape
<string name='touchSensor.shape'>box:position,0,0.5,0:extends,2,1,0.5</string>
Set shape to box matching the element size.
touchSensor.shapeFromSize
or touchSensor({id}).shapeFromSize
false
<boolean name='touchSensor.shapeFromSize'>true</boolean>
Enable touch sensor.
touchSensor.touching
or touchSensor({id}).touching
true
<boolean name='touchSensor.touching'>false</boolean>
Position to attach resource to collider.
touchSensor.position
or touchSensor({id}).position
<vector name='touchSensor.position' x='0' y='0' z='0.1'/>
Orientation to attach resource to collider in degrees.
touchSensor.orientation
or touchSensor({id}).orientation
<vector name='touchSensor.orientation' x='30' y='0' z='0'/>
Bone to attach resource to. If empty string attach to collider.
touchSensor.bone
or touchSensor({id}).bone
<string name='touchSensor.bone'>attach</string>
Element entered touch sensor.
Element left touch sensor.
Element can touch this touch sensor if all listeners return true.
This behavior requires no other behaviors.
This behavior does support element class to be persistable (setPersistable).
Since DragonScript Module Version 1.0
This example defines an element which can detect touching elements.
class MyElement extends BehaviorElementClass public var ECBehaviorComponent component public var ECBehaviorCollider collider public var ECBehaviorTouchSensor touchSensor func new() component = ECBehaviorComponent.new(this, null) collider = ECBehaviorCollider.new(this, component) touchSensor = ECBehaviorTouchSensor.new(this, collider) end end
Using element class supporting adding behaviors the behavior can be added like this:
<?xml version='1.0' encoding='UTF-8'?> <elementClass name='MyClass' class='GenericBehaviorElement'> <behavior type='ECBehaviorComponent'/> <behavior type='ECBehaviorCollider'/> <behavior type='ECBehaviorTouchSensor'> <!-- optional: set collision filter. default value '4:3' which means category BaseGameApp.CollisionFilterBit.trigger filter BaseGameApp.CollisionFilterBit.actorAI. format is '', 'category' or 'category:filter' where category and filter are a list of bits to set. --> <string name='collisionFilter'>3:4 0</string> <!-- set element properties. omit property prefix if used inside behavior tag --> <boolean name='.shapeFromSize'>true</boolean> </behavior> <!-- for adding multiple behaviors use unique identifiers --> <behavior type='ECBehaviorTouchSensor' id='second'/> </elementClass>