Problem with "rec()" not recognizing self variables

@dave1707 Hi sorry, was already midnight for me by the time i got to creating sprites for the button. I got it working in the end tho. So now I need help with something else, the four buttons will move my main character up, down, left and right, but I need the characters to stay centered in the middle of the screen. I looked into the translate() and push/pop matrix, but for some reason the whole screen moves, and all at different speeds. could you provide an example with the buttons moving the character around a scene with the character centered? thanks

Here’s a scrolling background. Also, you don’t have to lift your finger as you move it from button to button.

viewer.mode=FULLSCREEN

function setup()
    dx,dy=WIDTH/2,HEIGHT/2
    pressed=vec2(0,0)
    up=button(WIDTH/2,150,60,60,0,-1,asset.builtin.Cargo_Bot.Command_Grab)
    down=button(WIDTH/2,50,60,60,0,1,asset.builtin.Cargo_Bot.Command_Grab)
    left=button(WIDTH/2-50,100,60,60,1,0,asset.builtin.Cargo_Bot.Command_Left)
    right=button(WIDTH/2+50,100,60,60,-1,0,asset.builtin.Cargo_Bot.Command_Right)
    img=image(2000,2000)
    stroke(255)
    strokeWidth(2)
    setContext(img)
    background(0)
    for z=1,2000,50 do
        line(z,0,z,2000)
        line(0,z,2000,z)
    end
    for z=1,50 do
        sprite(asset.builtin.Platformer_Art.Coin,math.random(2000),math.random(2000))
    end
    setContext()
end

function draw()
    background(0)
    pushMatrix()
    translate(dx,dy)
    sprite(img,0,0)
    translate()
    popMatrix()
    sprite(asset.builtin.Platformer_Art.Guy_Standing,WIDTH/2,HEIGHT/2)
    up:draw(-50)
    down:draw(50)
    left:draw(50)
    right:draw(50)
    dx=dx+pressed.x
    dy=dy+pressed.y
end

function touched(t)
    up:touched(t)
    down:touched(t)
    left:touched(t)
    right:touched(t)
end

button=class()

function button:init(x,y,w,h,dx,dy,i)
    self.x=x
    self.y=y
    self.w=w
    self.h=h
    self.dir=vec2(dx,dy)
    self.img=i
end

function button:draw(v)
    sprite(self.img,self.x,self.y,v)
end

function button:touched(t)
    if t.state==BEGAN or t.state==CHANGED then
        if t.x>self.x-self.w/2 and t.x<self.x+self.w/2 and
            t.y>self.y-self.h/2 and t.y<self.y+self.h/2 then
            pressed=self.dir
        end
    end
    if t.state==ENDED then
        pressed=vec2(0,0)
    end
end

wow, perfect. you’ve probably made over 60% of my game so far haha. I’ll credit you in it.

@dave1707 Hi, finally finished drawing all my sprites, and got everything working how i wanted it to. I wanted to share how it looks for you, how and where do I do it?

@Exyon You can post it here for everyone to see. If you don’t want to post it, then that’s OK.

@dave1707 It says " “FullSizeRender.mov” is not allowed. "

@Exyon You didn’t say it was a movie. I thought you would post code. In that case you don’t have to post anything.

@dave1707 Ohh, then you’d have to download the sprites i used as well.

@dave1707 https://media.discordapp.net/attachments/727207723036311562/781287481319489586/video0.mov
here’s a link to the video instead

I might also give away my code because I’ll be moving to unity. don’t think anyone would want it though haha

Nice job rotating the character as he moves. That would be a nice beginning for anyone who wants to make a game with it if you give it away.

@dave1707 Thanks! I’ll clean it up a bit, might see if i can implement an item and inventory system, and stop there

@dave1707 Hi, I’m having problems understanding “for”,
I tried looking it up on my own, I understand that it works like

for (initial value), (value you're reaching), (increasing by increment) do
end

but i don’t understand;

for i,v in ipairs(table) do
body
end

could you explain how pairs work and how it’s used in “for”?

@Exyon It takes the table “key” and “value” and puts them in the variables “i” and “v” automatically. If you have a simple table {5,3,8,6,2,7} then “i” will be 1,2,3,4,5,6 and “v” will be 5,3,8,6,2,7 . If you have a keyed table, then “i” will be the key and “v” will be the value. It’s just an easy way of accessing a table instead of using the [ ] .

Here’s an example of both types.

viewer.mode=STANDARD

function setup()  
    tab1={4,7,6,9,3,2}
    for i,v in pairs(tab1) do
        print(i,v)
    end
    
    print()
    
    tab2={five=5,two=2,six=6,seven=7,three=3,nine=9}    
    for i,v in pairs(tab2) do
        print(i,v)
    end      
end

@dave1707 Oh, so it orders them in the order you placed them into the table?

@Exyon If it’s a simple table, then they print in the order that they’re in the table. If it’s a keyed sequence table, then they print in a random order. The “for” loop doesn’t do anything to them in the table, it just reads the table and puts the values in the “i” and “v” variables for you to use however you want.

@dave1707 ohh, I think i got it now. One last question hopefully, how do I add multitouch to my project? i’ve seen the examples but nothing explains how they work, could you provide and example and an explanation? sorry for asking so much