[CLOSED] My Alert class does not work - Why??

Hi, I am making great work.
My Alert Buttons {OK, Dismiss} are based on a text and a hit function. My Dismiss Button doesn’t work. Why?
The other thing what I want to know is, how to create that if there are more then xxx symbols in the alert information there will be a new line. Is there anyone going to help me?
Main class:

displayMode(OVERLAY)
displayMode(FULLSCREEN)
function setup()
    al = Alert(500, 200, nil)
end
function draw()
    background(0, 0, 0, 255)
    al:draw()
    font("AmericanTypewriter")
    fontSize(25)
    if al.alertdraw == false then
        text("Touch to see the alert", WIDTH/2, HEIGHT/2)
    end
end
function touched(t)
    al:touched(t)
    al.alertdraw = true
end

Alert class:

Alert = class()

function Alert:init(x, y, action, title, txt)
    if action == nil then
        action = function() print("Action!") end
    end
    if title == nil then
        title = "Alert"
    end
    if txt == nil then
        txt = "The Alert has no content"
    end
    
    self.x = x
    self.y = y
    self.alertdraw = false
    self.action = action
    self.headtext = title
    self.informationtext = txt
end

function Alert:draw()
    if self.alertdraw == true then
        -- Alert Box
        fill(255, 255, 255, 255)
        rectMode(CORNER)
        rect(self.x, self.y, 350, 200)
        
        -- Alert Title Text
        font("AmericanTypewriter-Bold")
        fontSize(25)
        fill(42, 64, 74, 255)
        text(self.headtext, self.x + 175, self.y + 175)
        
        -- Alert Information Text
        font("AmericanTypewriter")
        fontSize(15)
        fill(0, 0, 0, 255)
        text(self.informationtext, self.x + 175, self.y + 150)
        
        -- Buttons
        font("AmericanTypewriter")
        fontSize(25)
        fill(255, 0, 0, 255)
        text("Dismiss", self.x + 280, self.y + 25)
        fill(0, 0, 255, 255)
        text("OK", self.x + 35, self.y + 25)
    end
end

function Alert:touched(t)
    if t.state == ENDED and self:OKhit(vec2(t.x,t.y)) then
        if self.action then
            self.action()
        end
    end
    
    if t.state == ENDED and self:Dismiss(vec2(t.x,t.y)) then
        self.alertdraw = false
        print("No Alert Draw")
    end
end

function Alert:OKhit(p)
    local OKT = self.y + 50
    local OKB = self.y
    local OKR = self.x + 50
    local OKL = self.x
    if p.x > OKL and p.x < OKR and p.y > OKB and p.y < OKT then
        return true
    end
    return false
end

function Alert:Dismiss(d)
    local t = self.y - 50
    local b = self.y + 50
    local r = self.x + 350
    local l = self.x + 250
    if d.x > l and d.x < r and d.y > b and d.y < t then
        return true
    end
    return false
end

Try this. You’re also always setting al.alertdraw to true in the touched() function.

function Alert:Dismiss(d)
    local t = self.y + 50
    local b = self.y
    local r = self.x + 350
    local l = self.x + 250
    if d.x > l and d.x < r and d.y > b and d.y < t then
        return true
    end
    return false

Try this

function draw()
    background(0, 0, 0, 255)
    font("AmericanTypewriter")
    fontSize(25)
    if al.alertdraw == false then
        text("Touch to see the alert", WIDTH/2, HEIGHT/2)
    else
        al:draw()
    end
end

This content was deleted. Reason: OutDated

@Ignatz. So?

@Ignatz - offline?

@yojimbo2000, can you help me plz

Thank you very much!

@TokOut - please don’t nag, we are not here just to answer your questions

@TokOut I sent you a correction 6 hours ago, did you try it. Did it work or is there still a problem. You constantly ask for help, but you don’t take the time to respond if what was sent to you worked or not.

Yup, I know I’ve answered him before about the text wrap thing and was just met with rudeness.

@dave1707 - You mean the Main class correction or the hit correction? —> I used them both

@TokOut The correction was for the dismiss button and to close the alert.

Really? Maybe I done something wrong? My Alert didn’t work –> I gave up! So you can close the thread

Thank you for your help Dave, Yojimbo, and Ignatz. I read your posts going back to 2010. Codea is a great tool for prototyping and exploring, and this forum adds a lot of value. I wish there was a way to track patterns instead of questions.

I’ve marked this thread as a save. The idea of passing an action function to the alert extends the state machine pattern.

And I am back [Don’t worry, I am fast explaining]
A cool tool for making buttons. The best button I ever made

Main class as overview:

-- setup function
button = Button("displayName", x, z, function() action() end)
-- draw function
background(0)
button:draw()
-- touch clause with coordinates t
button:touched(t)
-- action function <     action()     >

Button class:

Button = class()

function Button:init(name, x, y, action)
    if x == nil or y == nil then
        x = 0
        y = 0
    end
    
    if name == nil then
        name = "Nil"
    end
    
    if action == nil then
        action = function() print("Using a Non-Function Button") end
    end
    
    self.x = x
    self.y = y
    self.name = name
    self.action = action
    self.color = color(255, 255, 255, 255)
end

function Button:draw()
    fill(self.color)
    rect(self.x, self.y, 75, 75)
    fill(0, 0, 0, 255)
    font("AmericanTypewriter")
    fontSize(25)
    textMode(CENTER)
    text(self.name, self.x + 35, self.y + 35)
end

function Button:touched(t)
    if t.state == BEGAN and self:hit(vec2(t.y,t.x)) then
        self.color = color(187, 187, 187, 255)
        self.action()
    else
        self.color = color(255, 255, 255, 255)
    end
    
    if t.state == ENDED then
        self.color = color(255, 255, 255, 255)
    end
end

function Button:hit(p)
    local t = self.x + 75
    local b = self.x - 1
    local l = self.y - 1
    local r = self.y + 75
    if p.x > l and p.x < r and p.y > b and p.y < t then
        return true
    end
        return false
end

@TokOut You seem to start a lot of things, ask for help, and then don’t finish. You’re just wasting everyone’s time when you do that. Above you said you gave up on your Alert code. Using the change I gave you and a few minor changes to your code, your Alert code works the way it’s supposed to. After the Alert shows, tapping OK executes whatever function you assign to it. Tapping Dismiss closes the Alert box with no action taken. Instead of writing code you don’t understand or asking for help and not using or understanding what’s given to you, start reading some books or tutorials and write small programs to try things. It takes time to learn a language and how to use it to write programs. When I started 3 years ago, I didn’t know anything about Codea, and how it ran was totally different from the way I was used to writing code. There’s still a lot I don’t know about Codea, but that’s what keeps me interested. I write small programs using functions I’m not familiar with and try different things with it so I can see what I can and can’t do with it. What I don’t know, others here do. We learn from each other. Constantly writing code you can’t or won’t finish isn’t teaching you what you need to know.

4th TEST:

pre
lang="lua"

What are you doing?

To post code, put three ~~~ on an empty line at the top of your code, and another three underneath your code.

See how I changed your test above.

@Ignatz, I don’t know how to create colored code.
I don’t know where to place the

pre

tag and use the

lang="lua"

attribute. Where to place it?

Test because nobody answers…

pre lang="lua"
-- test

Read the faq