Buttons

What goes in each of the sections of a button class. I mostly need help on init: but touched also. Every time I tap my button I want it to make a sound which i made

@GriffinC Here’s an example, but a class can have a lot of different things depending on what you want it to do.


function setup()
    rectMode(CENTER)
    btn=buttons(300,300,100,50,"Sound")
end

function draw()
    background(40, 40, 50)
    btn:draw()
end

function touched(t)
    btn:touched(t)
end

buttons=class()

function buttons:init(x,y,w,h,txt)
    self.x=x
    self.y=y
    self.w=w
    self.h=h
    self.text=txt
    self.left=x-w/2
    self.right=x+w/2
    self.bottom=y-h/2
    self.top=y+h/2
end

function buttons:draw()
    fill(255)
    rect(self.x,self.y,self.w,self.h)   
    fill(255,0,0) 
    text(self.text,self.x,self.y)
end

function buttons:touched(t)
    if t.state==BEGAN or t.state==MOVING then
        if t.x>self.left and t.x<self.right and 
                    t.y>self.bottom and t.y<self.top then
            sound(SOUND_HIT, 46511)
        end
    end
end

Can you show what itd be like for a ellipse

@GriffinC Here’s an ellipse button.


function setup()
    rectMode(CENTER)
    btn=buttons(300,300,150,100,"Sound")
end

function draw()
    background(40, 40, 50)
    btn:draw()
end

function touched(t)
    btn:touched(t)
end

buttons=class()

function buttons:init(x,y,w,h,txt)
    self.x=x
    self.y=y
    self.w=w
    self.h=h
    self.text=txt
end

function buttons:draw()
    fill(255)
    ellipse(self.x,self.y,self.w,self.h)   
    fill(255,0,0) 
    text(self.text,self.x,self.y)
end

function buttons:touched(t)
    if t.state==BEGAN then
        if (t.x-self.x)^2/(self.w/2)^2+(t.y-self.y)^2/(self.h/2)^2 <= 1 then 
            sound(SOUND_HIT, 46511)
        end
    end
end

whats rectmode

@GriffinC At the top of this forum is a word called “Reference”. Press it, then press “Graphics”. Under Graphics is “Style”. Under “Style” is rectMode. There is a lot more information there that might come in handy.

if (t.x-self.x)^2/(self.w/2)^2+(t.y-self.y)^2/(self.h/2)^2 <= 1 then
sound(DATA, “ZgBAPwBDP0ZAQT1AltWjPNZdfD6BqrI9QABAfT5JQD9ARUBA”)
end

Can you explain this mumbo jumbo. I took out sll the selfs except text how can zi make it so it still makes the sound when i tap it with pout the self stuff

@GriffinC - why would you want to use this? Even if you do, there comes a time when you have to start figuring some of this stuff out for yourself…

The formula part is not very difficult.

For the Sound function, look at the built in reference (as dave1707 suggested).

@GriffinC, why did you take out all the selfs…? It won’t work without them.

That ‘mumbo jumbo’ is a formula to check if the point you touched is within the ellipse, if it is then it calls the sound function

I thought this was a place to ask questions not to be criticized for not knowing something when your new to it

@GriffinC, i wasn’t trying to criticize you, i apologize if that’s how it seemed.

Not you Ignatz. I solved how to keep what I had done and add this

@GriffinC - yes, this is a place where you can get help, but we expect you to make a reasonable effort first, eg looking up the description of a function you haven’t seen before.

How do I switch the button from the left to the right while in landscape
http://imgur.com/QDEeJgZ

Nevermond solved it