Difference between revisions of "TntVirtualPad"

From GiderosMobile
Line 6: Line 6:
 
=== Description ===
 
=== Description ===
 
Adds a virtual pad to your game/app screen.
 
Adds a virtual pad to your game/app screen.
 +
 +
You can find the graphics for the pads and the text file that comes with it:
 +
*
 +
*
  
 
=== Examples ===
 
=== Examples ===
'''Add a virtual pad to the stage (or to your scene)'''
+
'''Add a virtual pad to your scene'''
 
<source lang="lua">
 
<source lang="lua">
 
require "tntvirtualpad"
 
require "tntvirtualpad"
  
local texturevpad = TexturePack.new("gfx/tnt_virtual_pad/TNTVirtualPad.txt", "gfx/tnt_virtual_pad/TNTVirtualPad.png", true)
+
LevelX = Core.class(Sprite)
local vPad = CTNTVirtualPad.new(stage, texturevpad, PAD.STICK_SINGLE, PAD.BUTTONS_ONE, 100, 0)
+
 
vPad:setJoyStyle(PAD.COMPO_LEFTPAD, PAD.STYLE_FOLLOW)
+
function LevelX:init()
vPad:setScale(PAD.COMPO_LEFTPAD, 1.5)
+
-- bg
vPad:setScale(PAD.COMPO_BUTTON1, 1)
+
application:setBackgroundColor(0x0099a0)
vPad:setHideDelay(3)
+
-- tnt virtual pad
vPad:start()
+
local texturevpad = TexturePack.new("gfx/UI/TNTVirtualPad.txt", "gfx/UI/TNTVirtualPadX.png", true)
 +
self.vPad = CTNTVirtualPad.new(self, texturevpad, PAD.STICK_SINGLE, PAD.BUTTONS_ONE, 100, 0)
 +
self.vPad:setJoyStyle(PAD.COMPO_LEFTPAD, PAD.STYLE_FOLLOW)
 +
self.vPad:setScale(PAD.COMPO_LEFTPAD, 1)
 +
self.vPad:setScale(PAD.COMPO_BUTTON1, 1)
 +
self.vPad:setHideDelay(5000)
 +
self.vPad:start()
 +
-- tnt virtual pad listeners
 +
self.vPad:addEventListener(PAD.LEFTPAD_EVENT, self.onVKeyDown, self)
 +
self.vPad:addEventListener(PAD.BUTTON1_EVENT, self.vShoot, self)
 +
end
 +
 
 +
-- VIRTUAL PAD HANDLER
 +
function LevelX:onVKeyDown(event)
 +
print(event.data.power)
 +
print(event.data.angle)
 +
print(math.cos(event.data.angle))
 +
print(math.sin(event.data.angle))
 +
-- example usage
 +
--self.player.posx += self.player.vx * math.cos(event.data.angle)
 +
--self.player.posy += self.player.vy * math.sin(event.data.angle)
 +
--if math.cos(event.data.angle) < 0 then self.player:setSkew(5, -5)
 +
--elseif math.cos(event.data.angle) > 0 then self.player:setSkew(-5, 5)
 +
--else self.player:setSkew(0, 0)
 +
--end
 +
end
  
--vPad:addEventListener(PAD.LEFTPAD_EVENT, self.myhero.onVKeyDown, self.myhero)
+
function LevelX:vShoot(event)
--vPad:addEventListener(PAD.BUTTON1_EVENT, self.myhero.vshoot, self.myhero)
+
if event.data.state == PAD.STATE_BEGIN then
 +
-- example usage
 +
--local missile = Missiles.new(self.player.posx, self.player.posy - 12)
 +
--table.insert(self.missiles_list, missile)
 +
end
 +
end
 
</source>
 
</source>
  

Revision as of 05:20, 3 December 2020

Supported platforms: Platform android.pngPlatform ios.pngPlatform mac.pngPlatform pc.pngPlatform html5.pngPlatform winrt.pngPlatform win32.png
Available since: Gideros 2019.4

Description

Adds a virtual pad to your game/app screen.

You can find the graphics for the pads and the text file that comes with it:

Examples

Add a virtual pad to your scene

require "tntvirtualpad"

LevelX = Core.class(Sprite)

function LevelX:init()
	-- bg
	application:setBackgroundColor(0x0099a0)
	-- tnt virtual pad
	local texturevpad = TexturePack.new("gfx/UI/TNTVirtualPad.txt", "gfx/UI/TNTVirtualPadX.png", true)
	self.vPad = CTNTVirtualPad.new(self, texturevpad, PAD.STICK_SINGLE, PAD.BUTTONS_ONE, 100, 0)
	self.vPad:setJoyStyle(PAD.COMPO_LEFTPAD, PAD.STYLE_FOLLOW)
	self.vPad:setScale(PAD.COMPO_LEFTPAD, 1)
	self.vPad:setScale(PAD.COMPO_BUTTON1, 1)
	self.vPad:setHideDelay(5000)
	self.vPad:start()
	-- tnt virtual pad listeners
	self.vPad:addEventListener(PAD.LEFTPAD_EVENT, self.onVKeyDown, self)
	self.vPad:addEventListener(PAD.BUTTON1_EVENT, self.vShoot, self)
end

-- VIRTUAL PAD HANDLER
function LevelX:onVKeyDown(event)
	print(event.data.power)
	print(event.data.angle)
	print(math.cos(event.data.angle))
	print(math.sin(event.data.angle))
	-- example usage
	--self.player.posx += self.player.vx * math.cos(event.data.angle)
	--self.player.posy += self.player.vy * math.sin(event.data.angle)
	--if math.cos(event.data.angle) < 0 then self.player:setSkew(5, -5)
	--elseif math.cos(event.data.angle) > 0 then self.player:setSkew(-5, 5)
	--else self.player:setSkew(0, 0)
	--end
end

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

Methods

Events

Constants