Difference between revisions of "CTNTVirtualPad.Event"

From GiderosMobile
(Created page with "__NOTOC__ '''Available since:''' Gideros 2019.4<br/> '''Class:''' VirtualPad<br/> === Description === Those are all the Events dispatched by the TNT Virtual Pad component...")
 
Line 31: Line 31:
  
 
=== Example ===
 
=== Example ===
'''TO DO'''
+
'''Same as front Virtual Pad page but shortened'''
 
<source lang="lua">
 
<source lang="lua">
 +
require "tntvirtualpad"
 +
 +
LevelX = Core.class(Sprite)
 +
 +
function LevelX:init()
 +
-- tnt virtual pad listeners
 +
self.vPad:addEventListener(PAD.LEFTPAD_EVENT, self.onVKeyDown, self)
 +
self.vPad:addEventListener(PAD.BUTTON1_EVENT, self.vShoot, self)
 +
end
 +
 +
function LevelX:onVKeyDown(event)
 +
print(event.data.power)
 +
print(event.data.angle)
 +
end
 +
 +
function LevelX:vShoot(event)
 +
if event.data.state == PAD.STATE_BEGIN then
 +
--local missile = Missiles.new(self.player.posx, self.player.posy - 12)
 +
end
 +
end
 
</source>
 
</source>
  
 
{{VirtualPad}}
 
{{VirtualPad}}

Revision as of 16:46, 3 December 2020

Available since: Gideros 2019.4
Class: VirtualPad

Description

Those are all the Events dispatched by the TNT Virtual Pad components.

The events hold a data table containing useful information.

Joystick Events

  • PAD.LEFTPAD_EVENT
  • PAD.RIGHTPAD_EVENT

Joysticks Event data table:

  • data.selected can be true or false (true when element is interacting with user)
  • data.power value (float) from 0 to 1 (1 when stick is on the base border)
  • data.angle angle of joystick in radians. If joystick is set as digital, values are from 0 to 7 (0 is right and so on in clockwise order, steps are 45 degrees)
  • data.state is the state of the joystick. It returns PAD.STATE_NONE→joystick not pressed, PAD.STATE_BEGIN→joystick begin touch, PAD.STATE_DOWN→joystick touched and pressed, PAD.STATE_END→joystick begin release

Buttons Events

  • PAD.BUTTON1_EVENT
  • PAD.BUTTON2_EVENT
  • PAD.BUTTON3_EVENT
  • PAD.BUTTON4_EVENT
  • PAD.BUTTON5_EVENT
  • PAD.BUTTON6_EVENT

Buttons Event data table:

  • data.selected can be true or false (true when element is interacting with user)
  • data.state is the state of the button. It returns PAD.STATE_NONE→button not pressed, PAD.STATE_BEGIN→button begin touch, PAD.STATE_DOWN→button touched and pressed, PAD.STATE_END→button begin release

Example

Same as front Virtual Pad page but shortened

require "tntvirtualpad"

LevelX = Core.class(Sprite)

function LevelX:init()
	-- tnt virtual pad listeners
	self.vPad:addEventListener(PAD.LEFTPAD_EVENT, self.onVKeyDown, self)
	self.vPad:addEventListener(PAD.BUTTON1_EVENT, self.vShoot, self)
end

function LevelX:onVKeyDown(event)
	print(event.data.power)
	print(event.data.angle)
end

function LevelX:vShoot(event)
	if event.data.state == PAD.STATE_BEGIN then
		--local missile = Missiles.new(self.player.posx, self.player.posy - 12)
	end
end