Hi @Invad3rZIM, thanks for your availability.
The code I’m posting here does not strictly relate to the game I originally asked help for, but it pretty sums up the same issue in a bunch only of classes.
I’d need to make the VirtualButton sprite to show up when the virtual stick is in movement.
If there’s better approach ( and I’m sure there is ) please give me some guide lines
Any help will be appreciated
thanks
A.
function setup()
print("This example tracks multiple touches and colors them based on their ID")
-- keep track of our touches in this table
touches = {}
pos = vec2(WIDTH/2, 300)
steer = vec2(0,0)
speed = 400
controller = VirtualStick {
moved = function(v) steer = v end,
released = function(v) steer = vec2(0,0) end
}
end
-- This function gets called whenever a touch
-- begins or changes state
function touched(touch)
if touch.state == ENDED then
-- When any touch ends, remove it from
-- our table
touches[touch.id] = nil
else
-- If the touch is in any other state
-- (such as BEGAN) we add it to our
-- table
touches[touch.id] = touch
controller:activate()
end
end
function draw()
background(0, 0, 0, 255)
for k,touch in pairs(touches) do
controller:draw()
end
end
-- Base class for controllers
--
-- Controllers translate touch events into callbacks to functions
-- that do something in the app. (Model/View/Controller style).
--
-- Controllers can draw a representation of their current state on
-- the screen, but you can choose not to.
--
-- A controller can be installed as the global handler for touch
-- events by calling its activate() method
Controller = class()
function Controller:activate()
touched = function(t)
self:touched(t)
end
end
function Controller:draw()
-- nothing
end
-- Utility functions
function touchPos(t)
return vec2(t.x, t.y)
end
function clamp(x, min, max)
return math.max(min, math.min(max, x))
end
function clampAbs(x, maxAbs)
return clamp(x, -maxAbs, maxAbs)
end
function clampLen(vec, maxLen)
if vec == vec2(0,0) then
return vec
else
return vec:normalize() * math.min(vec:len(), maxLen)
end
end
-- projects v onto the direction represented by the given unit vector
function project(v, unit)
return v:dot(unit)
end
function sign(x)
if x == 0 then
return 0
elseif x < 0 then
return -1
elseif x > 0 then
return 1
else
return x -- x is NaN
end
end
function doNothing()
end
-- A virtual analogue joystick with a dead-zone at the center,
-- which activates wherever the user touches their finger
--
-- Arguments:
-- radius - radius of the stick (default = 100)
-- deadZoneRadius - radius of the stick's dead zone (default = 25)
-- moved(v) - Called when the stick is moved
-- v : vec2 - in the range vec2(-1,-1) and vec2(1,1)
-- pressed() - Called when the user starts using the stick (optional)
-- released() - Called when the user releases the stick (optional)
VirtualStick = class(Controller)
function VirtualStick:init(args)
self.radius = args.radius or 100
self.deadZoneRadius = args.deadZoneRadius or 25
self.releasedCallback = args.released or doNothing
self.steerCallback = args.moved or doNothing
self.pressedCallback = args.pressed or doNothing
end
function VirtualStick:touched(t)
local pos = touchPos(t)
if t.state == BEGAN and self.touchId == nil then
self.touchId = t.id
self.touchStart = pos
self.stickOffset = vec2(0, 0)
self.pressedCallback()
elseif t.id == self.touchId then
if t.state == MOVING then
self.stickOffset = clampLen(pos - self.touchStart, self.radius)
self.steerCallback(self:vector())
elseif t.state == ENDED or t.state == CANCELLED then
self:reset()
self.releasedCallback()
end
end
end
function VirtualStick:vector()
local stickRange = self.radius - self.deadZoneRadius
local stickAmount = math.max(self.stickOffset:len() - self.deadZoneRadius, 0)
local stickDirection = self.stickOffset:normalize()
return stickDirection * (stickAmount/stickRange)
end
function VirtualStick:reset()
self.touchId = nil
self.touchStart = nil
self.stickOffset = nil
end
function VirtualStick:draw()
if self.touchId ~= nil then
pushStyle()
ellipseMode(RADIUS)
strokeWidth(1)
stroke(255, 255, 255, 255)
noFill()
pushMatrix()
translate(self.touchStart.x, self.touchStart.y)
ellipse(0, 0, self.radius, self.radius)
ellipse(0, 0, self.deadZoneRadius, self.deadZoneRadius)
translate(self.stickOffset.x, self.stickOffset.y)
--ellipse(0, 0, 25, 25)
tint(255,255,255,180)
sprite("Cargo Bot:Star Filled",0,0,130)
popMatrix()
popStyle()
end
end
VirtualButton = class()
function VirtualButton:init(x,y)
self.x = x
self.y = y
end
function VirtualButton:draw()
pushStyle()
pushMatrix()
sprite("Cargo Bot:Star", self.x,self.y)
popStyle()
popMatrix()
end