Coding Problem (getting nil value)

Try a lower case touch.x . Also in the touched function, you’re not making use of BEGAN, MOVING, or ENDED .

Ugh. touch.x doesn’t work either :frowning:

How are you calling the Button:touched(touched) routine. Are you creating an instance of Button by doing something like “button1:init(your parameters)” somewhere and then in the touched(touch) function calling button touch like this “button1:touched(touch)” . It’s hard to say what’s wrong without seeing the code.

@Crazypkr1099 - I suggest you do more tutorials, you don’t understand touch yet.

this is how you use touch

function touched(t)
    if touch.state==BEGAN then
        left:touched(t)
        right:touched(t)
    end
end

When a touch occurs, Codea provides details to the touched function. This is a good place to call the left and right button touched functions, passing them the touch variable t with all the details (I have called the variable t just to avoid confusion because I am saying touch all the time)

So now this will work

function Button:touched(touch)
    if touch.x > (self.x - self.width/2) and
    --etc etc

@Ignatz
My problem with this is that I don’t need a state check for left:touched(t). When I implemented ur code into my game, the player only moves when I began pressing. I need it so that it will continue to go untill I let go. So while button is hold, do left:touched()

Player class

Player = class()
pbullets = {}
function Player:init(img,x,y)
    self.image = img
    self.x = x
    self.y = y
    self.width, self.height = spriteSize(self.image)
     
end

function Player:draw()
    if moving ==  true and
        playerface == 1 then
        sprite(self.image,self.x,self.y,self.width,self.height)
        moving = false
    
    
    elseif moving ==  true and
        playerface == -1 then
        sprite(self.image,self.x,self.y,-self.width,self.height)
        moving = false
            
    
    elseif moving == false and
        playerface == 1 then
            sprite("Documents:PL1",self.x,self.y,self.width,self.height)
            

    elseif moving == false and
        playerface == -1 then
            sprite("Documents:PL1",self.x,self.y,-self.width,self.height)
            end        
    end

    
    
    
    
    

function Player:drawbullet()
    table.insert(pbullets,vec2(player.x + 3,player.y + 50))
    for a,b in pairs (pbullets) do
        sprite("Space Art:Green Bullet",b.x,b.y)
    end
    player:shoot()
end

function Player:shoot()
    for a,b in pairs (pbullets) do
        b.y = b.y +  5
        sprite("Space Art:Green Bullet",b.x,b.y)
    end
end







function Player:left()
    player.x = player.x - 1 
    if moving == true and
    time < 1 then
        if self.image == ("Documents:PL1")then
            self.image = ("Documents:PL2")     
        elseif self.image == ("Documents:PL2") then
            self.image = ("Documents:PL3") 
        elseif self.image == ("Documents:PL3") then
            self.image = ("Documents:PL4")     
        elseif self.image == ("Documents:PL4") then
            self.image = ("Documents:PL1")
            end
        end
end

function Player:right()
    player.x = player.x + 1 
    if moving == true and
    time < 1 then
        if self.image == ("Documents:PL1") then
            self.image = ("Documents:PL2")     
        elseif self.image == ("Documents:PL2") then
            self.image = ("Documents:PL3") 
        elseif self.image == ("Documents:PL3") then
            self.image = ("Documents:PL4")     
        elseif self.image == ("Documents:PL4") then
            self.image = ("Documents:PL1")
            end    
        end
end

Main class

function setup()
    time = 0
    moving = false
    playerface = 1
    left = Button("Cargo Bot:Command Left",600,100,80,100)
    right = Button("Cargo Bot:Command Right",700,100,80,100)
    shoot = Button("Documents:ExampleCircle",650,200,130,130)
    for i=0,1 do
        x = math.random(300,500)
        y = math.random(300,500)
        Enemy(x,y)
        end
    player = Player("Documents:PL1",400,100)
end




function draw()
    time = time + 1 
    if time > 10 then
        time =  0
    end  
-- draw
    background(131, 38, 38, 255)
    left:draw()
    right:draw()
    player:draw()
end
    
    
function touched(t)
    if t.state == BEGAN then
    left:touched(t)
    right:touched(t)




    end
end

I’m sorry guys. I know I’m a complete noob and shouldn’t be asking for this much help :confused: I’ll shut up now

@Crazypkr1099 - I have a couple of posts on touch

http://coolcodea.wordpress.com/2013/10/29/131-physics-adding-touch/

http://coolcodea.wordpress.com/2013/03/26/11-handling-touches-simple-pinch-and-zoom/

I understand that but none tell me how to continue to hold a button and have an object move. My other code was working but I got told if I want to use the ended, I need to use t.state.

@Crazypkr1099 - yes, if you want to know when touch has ended, you need to check the touched function and t.state==ENDED

currenttouch won’t help you because when the touch ends it is left in a permanent state of ENDED

so t.state ==ENDED is what you want

It not though because I need to have the button held down… t.state == ended will check if I let go of the button. I’m literally creating a button that you hold down to move. Once you let go, you stop

Do like this:

Button = class()
function Button:init()
    self.originid = nil
    self.x = WIDTH/2
    self.y = HEIGHT/2
    self.halfwidth = 10
    ellipseMode(CENTER)
end

function Button:touched(t)
    --if the touch started
    if t.state == BEGAN and self.originid == nil then
        --if the touch is on the button
        if t.x < self.x + self.halfwidth and t.x > self.x-self.halfwidth then
            if t.y < self.y+self.halfwidth and  t.y > self.y-self.halfwidth then
                --set the touch identifier to the id of the touch
                self.originid = t.id
            end
        end
    end
    --if the touch is moving and is the same touch that started on the button
    if t.state == MOVING and self.originid == t.id then
        --put the moving code in here, for example, like:
        self.x = t.x
        self.y = t.y
    end
    --if the touch is ended and is the same touch that started on the button
    if t.state == ENDED and self.originid == t.id then
        --set the touch identifier to nil so the button no longer follows finger until another touch begins on it
        self.originid = nil
    end
end

function Button:draw()
    --draw over the last draw
    background(255,255,255,255)
    fill(255,0,0,255)
    strokeWidth(0)
    --draw the button
    ellipse(self.x,self.y,self.halfwidth*2)
end

EDIT: I thought you meant the button moves when you touch it, as it was pretty early when I read your last comment. I think I know what you really meant now, so, if you didn’t mean the button itself moves tell me so I can fix it XD