Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Joystick Project Main Class
CharacterSprite = class()
function CharacterSprite:init(x)
self:log("init character")
-- you can accept and set parameters here
self.x = 200
self.y = 200
--parameters.watch("self.x")
--parameters.watch("self.y")
end
function CharacterSprite:draw()
-- Codea does not automatically call this method
if joystickLeft.visible then
--self:log("character::move");
self.x = self.x + jsDeltaPos[1]/10
self.y = self.y + jsDeltaPos[2]/10
end
sprite("Platformer Art:Guy Standing", self.x, self.y)
end
function CharacterSprite:touched(touch)
-- Codea does not automatically call this method
self:log("touched name: "+self.name)
end
function CharacterSprite:hitTest(x,y)
self:log("hitTest")
--return vec2(self.x, self.y):dist(vec2(x,y)) < self.r
end
function CharacterSprite:moveLeft()
self:log("moveLeft")
self.x = self.x - 1
end
function CharacterSprite:moveRight()
self:log("moveRight")
self.x = self.x + 1
end
function CharacterSprite:log(msg)
print("Character: "..msg)
end
JoystickLeft = class()
function JoystickLeft:init(x)
self:log("init joystick")
-- you can accept and set parameters here
self.xpos = 100
self.ypos = 100
self.x = xpos
self.y = ypos
self.visible = false
self.graphic = ellipse(100,100)
end
function JoystickLeft:draw()
--log("js.draw")
-- Codea does not automatically call this method
if self.visible == true then
ellipse(self.xpos, self.ypos, 200, 200)
ellipse(CurrentTouch.x,CurrentTouch.y,50,50)
end
end
function JoystickLeft:touched(touch)
-- Codea does not automatically call this method
--self:log("js.touched")
jsDeltaPos = vec2(touch.x - self.xpos, touch.y - self.ypos)
if touch.state == BEGAN then
self:log("touch began: "..touch.x..", "..touch.y)
jsDeltaPos = vec2(0,0)
self.xpos = touch.x
self.ypos = touch.y
self.visible = true
elseif touch.state == ENDED then
self:log("touch ended: "..touch.x..", "..touch.y)
self.visible = false
jsDeltaPos = vec2(0,0)
end
end
function JoystickLeft:log(msg)
print("JoystickLeft: "..msg)
end
-- Joystick
-- Use this function to perform your initial setup
function setup()
log("setup main")
jsDeltaPos = vec2(0,0)
joystickLeft = JoystickLeft()
character = CharacterSprite()
parameter.watch("jsDeltaPos")
parameter.watch("character.x")
parameter.watch("character.y")
end
-- This function gets called once every frame
function draw()
-- Set Style Here
background(40, 40, 50)
strokeWidth(5)
-- Do your drawing here
joystickLeft:draw()
character:draw()
end
function touched(touch)
if touch.state == BEGAN then
--log("touch began: "..touch.x..", "..touch.y)
joystickLeftvisible = true
elseif touch.state == MOVING then
--log("touch moving: "..touch.x..", "..touch.y)
elseif touch.state == ENDED then
--log("touch ended: "..touch.x..", "..touch.y)
joystickLeft.visible = false
end
joystickLeft:touched(touch)
--if character:hitTest(touch.x, touch.y) then
-- character:touched(touch)
--end
end
function log(msg)
print("Main: "..msg)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment