Skip to content

Instantly share code, notes, and snippets.

@interactivenyc
Created April 14, 2016 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save interactivenyc/136e6f41e7d2ca4776a1e72a53be5450 to your computer and use it in GitHub Desktop.
Save interactivenyc/136e6f41e7d2ca4776a1e72a53be5450 to your computer and use it in GitHub Desktop.
CodeaTuts Joystick Help
--# Joystick
Joystick=class()
function Joystick:init(x,y,id,type)
self.id=id
self.type=type
self.ox=x
self.oy=y
self.cx=x
self.cy=y
self.dx=0
self.dy=0
self.c=20
self.bSpeed = 2
end
function Joystick:draw()
stroke(255)
strokeWidth(2)
fill(255,255,255,100)
ellipse(self.ox,self.oy,150)
if self.type=="move" then
heroX=heroX+(self.cx-self.ox)/10
heroY=heroY+(self.cy-self.oy)/10
end
if self.type=="shoot" then
bDirection=vec2(self.cx-self.ox,self.cy-self.oy)
bDirection=bDirection:normalize()
if math.abs(bDirection.x+bDirection.y)>0 then
self.dx=bDirection.x*self.bSpeed
self.dy=bDirection.y*self.bSpeed
self.c=self.c+1
if self.c>12 then
table.insert(bTable,vec4(heroX,heroY,self.dx,self.dy))
self.c=0
end
end
end
end
function Joystick:touched(touch)
for jsKey,jsInstance in pairs(jsTable) do
if touch.id==jsInstance.id then
if touch.state==MOVING then
jsInstance.cx=touch.x
jsInstance.cy=touch.y
end
if touch.state==ENDED then
table.remove(jsTable,jsKey)
end
end
end
end
--# Main
--displayMode(FULLSCREEN)
function setup()
heroX,heroY=WIDTH/2,HEIGHT/2
jsTable,bTable={},{}
parameter.watch("screenX")
parameter.watch("screenY")
end
function draw()
background(40, 40, 50)
for jsIndex,jsInstance in pairs(jsTable) do
jsInstance:draw()
end
fill(255)
for bIndex,bInstance in pairs(bTable) do
ellipse(bInstance.x,bInstance.y,10)
bInstance.x=bInstance.x+bInstance.z
bInstance.y=bInstance.y+bInstance.w
if bInstance.x<0 or bInstance.x>WIDTH or bInstance.y<0 or bInstance.y>HEIGHT then
table.remove(bTable,bIndex)
end
end
sprite("Platformer Art:Guy Standing",heroX,heroY,25)
end
function touched(touch)
if touch.state==BEGAN then
if touch.x<WIDTH/2 then
for a,b in pairs(jsTable) do
--if b.type=="move" then
-- return -- limit 1 move joystick
--end
end
table.insert(jsTable,Joystick(touch.x,touch.y,touch.id,"move"))
else
table.insert(jsTable,Joystick(touch.x,touch.y,touch.id,"shoot")) -- allow multiple shoot
end
end
for jsIndex,jsInstance in pairs(jsTable) do
jsInstance:touched(touch)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment