DualStick Controller:My version

It may not the most advanced one. But I think it’s easy to understand and easy to use for beginners,
The stick is adjustable.
Just copy em all into a new Codea project and click run~


function setup()
    x=512
    y=384
    displayMode(FULLSCREEN)
    GlobalTouches = {}
    LeftStickOP={}
    RightStickOP={}
    DualStickLeft={
    posx=(WIDTH/1024)*150,
    posy=(WIDTH/1024)*150,
    Radius=(WIDTH/1024)*100,
    StickRadius=(WIDTH/1024)*40
    }
    DualStickRight={
    posx=(WIDTH/1024)*874,
    posy=(WIDTH/1024)*150,
    Radius=(WIDTH/1024)*100,
    StickRadius=(WIDTH/1024)*40
    }
end

function touched(touch)
    if touch.state == ENDED then
            GlobalTouches[touch.id] = nil
            LeftStickOP.status=false
            RightStickOP.status=false
    else 
            GlobalTouches[touch.id] = touch
    end
end

function draw()
    DualStickDraw()
    if LeftStickOP.status==true then
        x = x + LeftStickOP.x/9
        y = y + LeftStickOP.y/9
    end
    sprite("Planet Cute:Character Boy",x,y)
    if RightStickOP.status==true then
        sprite("Planet Cute:Star",x+100/math.sqrt(RightStickOP.x*RightStickOP.x+
        RightStickOP.y*RightStickOP.y)*
        RightStickOP.x,y+100/math.sqrt(RightStickOP.y*RightStickOP.y+
        RightStickOP.x*RightStickOP.x)*
        RightStickOP.y)
    end
end

function DualStickDraw()
    background(0, 60, 255, 255)
    fill(0, 0, 0, 255)
    ellipseMode(RADIUS)
    ellipse(DualStickLeft.posx,DualStickLeft.posy,DualStickLeft.Radius)
    ellipse(DualStickRight.posx,DualStickRight.posy,DualStickRight.Radius)
    for _,touch in pairs(GlobalTouches) do
        fill(255, 0, 0, 255)
        if ((touch.x-DualStickLeft.posx)*(touch.x-DualStickLeft.posx))+
        ((touch.y-DualStickLeft.posy)*(touch.y-DualStickLeft.posy))<
        (DualStickLeft.Radius)*(DualStickLeft.Radius) then
            ellipse(touch.x, touch.y, DualStickLeft.StickRadius)
            LeftStickOP.status=true
            LeftStickOP.x=(touch.x-DualStickLeft.posx)
            LeftStickOP.y=(touch.y-DualStickLeft.posy)      
        elseif ((touch.x-DualStickRight.posx)*(touch.x-DualStickRight.posx))+
        ((touch.y-DualStickRight.posy)*(touch.y-DualStickRight.posy))<
        (DualStickRight.Radius)*(DualStickRight.Radius) then
            ellipse(touch.x, touch.y, DualStickRight.StickRadius)
            RightStickOP.status=true
            RightStickOP.x=(touch.x-DualStickRight.posx)
            RightStickOP.y=(touch.y-DualStickRight.posy)      
        end
    end
end

I don’t want to add functionality, but you said the code is supposed to be easy to understand and suitable for beginners. Let me add readability to the DualStickDraw() function to achieve the validity of these statements.

The code below has in my eyes (1) gained general readability and (2) makes obvious that the circle equation is used.


function DualStickDraw()
    background(0, 60, 255, 255)
    fill(0, 0, 0, 255)
    ellipseMode(RADIUS)
    local dsl = DualStickLeft
    local dsr = DualStickRight
    ellipse(dsl.posx,dsl.posy,dsl.Radius)
    ellipse(dsr.posx,dsr.posy,dsr.Radius)
    for _,touch in pairs(GlobalTouches) do
        fill(255, 0, 0, 255)
        local dx, dy, r = (touch.x-dsl.posx), (touch.y-dsl.posy), dsl.Radius
        if ((dx * dx) + (dy * dy)) < (r * r) then
            ellipse(touch.x, touch.y, dsl.StickRadius)
            LeftStickOP.status = true
            LeftStickOP.x = dx
            LeftStickOP.y = dy
        end
        dx, dy, r = (touch.x-dsr.posx), (touch.y-dsr.posy), dsr.Radius
        if ((dx * dx) + (dy * dy)) < (r * r) then
            ellipse(touch.x, touch.y, dsr.StickRadius)
            RightStickOP.status = true
            RightStickOP.x = dx
            RightStickOP.y = dy
        end
    end
end

.@Codeslinger Great job! I’m a beginner myself^_^

great learn something from this code myself !