Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

UI Controls; Change characters by touch

edited June 2012 in Code Sharing Posts: 68

I didn't think about commenting the code but I'm sure it's straightforward.
And if it seems crudley done that's because I'm newish :)
Anyways, all there is is a character who changes everytime you touch it, and arrow buttons.
BUT I'm currently trying to make a tile map, a portal, and trying to figure out how the **** to "destroy" an object after having it collected.

MAIN

-- Use this function to perform your initial setup
function setup()
    displayMode(FULLSCREEN)
    
    char = Character()
    ui = UI()
    char:init()
    ui:init(char)
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    sprite("SpaceCute:Background", WIDTH/2, HEIGHT/2, 1024, 768)
    char:draw()
    ui:draw()
end

function touched(touch)
    char:touched(touch)
    ui:touched(touch)
end

CHARACTER

Character = class()

function Character:init()
    -- you can accept and set parameters here
    self.x = WIDTH/2
    self.y = HEIGHT/2
    self.choice = 3
end

function Character:draw()
    -- Codea does not automatically call this method
    
    if self.choice == 1 then
        sprite("Planet Cute:Character Boy", self.x, self.y)
    elseif self.choice == 2 then
        sprite("Planet Cute:Character Cat Girl", self.x, self.y)
    elseif self.choice == 3 then
        sprite("Planet Cute:Character Horn Girl", self.x, self.y)
    elseif self.choice == 4 then
        sprite("Planet Cute:Character Pink Girl", self.x, self.y)
    elseif self.choice == 5 then
        sprite("Planet Cute:Character Princess Girl", self.x, self.y)
    end
    
    if self.choice > 5 then
        self.choice = 1
    end
end

function Character:point(x,y)
    if x < (self.x + 50) and
       x > (self.x - 50) and
       y < (self.y + 50) and
       y > (self.y - 50) then
        return true
    end
    return false
end

function Character:touched(touch)
    -- Codea does not automatically call this method
    if self:point(touch.x, touch.y) then
        if touch.state == ENDED then
            self.choice = self.choice + 1
        end
    end
end

UI

UI = class()

function UI:init(c)
    -- you can accept and set parameters here
    self.char = c
    self.x = WIDTH/4 - 200
    self.y = HEIGHT/4
    self.x1 = WIDTH/4 - 50
    self.y1 = HEIGHT/4
    self.x2 = WIDTH/4 - 125
    self.y2 = HEIGHT/4 + 75
    self.x3 = WIDTH/4 - 125
    self.y3 = HEIGHT/4 - 75
    self.a = WIDTH - 150
    self.b = HEIGHT/4
    self.toggle = 0
    self.A = false
end

function UI:draw()
    -- Codea does not automatically call this method
    ellipse(self.x, self.y, 100, 100)
    ellipse(self.x1, self.y1, 100, 100)
    ellipse(self.x2, self.y2, 100, 100)
    ellipse(self.x3, self.y3, 100, 100)
    ellipse(self.a, self.b, 100, 100)
    
    if self.toggle == 1 then
        self.char.x = self.char.x - 4
    elseif self.toggle == 2 then
        self.char.x = self.char.x + 4
    elseif self.toggle == 3 then
        self.char.y = self.char.y + 4
    elseif self.toggle == 4 then
        self.char.y = self.char.y - 4
    end
end

function UI:point(x,y)
    if x < (self.x + 50) and
       x > (self.x - 50) and
       y < (self.y + 50) and
       y > (self.y - 50) then
        return 1
    elseif x < (self.x1 + 50) and
       x > (self.x1 - 50) and
       y < (self.y1 + 50) and
       y > (self.y1 - 50) then
        return 2
    elseif x < (self.x2 + 50) and
       x > (self.x2 - 50) and
       y < (self.y2 + 50) and
       y > (self.y2 - 50) then
        return 3
    elseif x < (self.x3 + 50) and
       x > (self.x3 - 50) and
       y < (self.y3 + 50) and
       y > (self.y3 - 50) then
        return 4
    end
    if x < (self.a + 50) and
       x > (self.a - 50) and
       y < (self.b + 50) and
       y > (self.b - 50) then
        return 5
    end
    return 0
end

function UI:touched(touch)
    -- Codea does not automatically call this method
    if self:point(touch.x, touch.y) == 1 then
        if touch.state == BEGAN then
            self.toggle = 1
        elseif touch.state == ENDED then
            self.toggle = 0
        end
    elseif self:point(touch.x, touch.y) == 2 then
        if touch.state == BEGAN then
            self.toggle = 2
        elseif touch.state == ENDED then
            self.toggle = 0
        end
    elseif self:point(touch.x, touch.y) == 3 then
        if touch.state == BEGAN then
            self.toggle = 3
        elseif touch.state == ENDED then
            self.toggle = 0
        end
    elseif self:point(touch.x, touch.y) == 4 then
        if touch.state == BEGAN then
            self.toggle = 4
        elseif touch.state == ENDED then
            self.toggle = 0
        end
    end
    if touch.state == MOVING and self:point(touch.x, touch.y) == 0 then
        self.toggle = 0
        self.A = false
    end
end
Sign In or Register to comment.