An assortment of questions.

The keyboard function works the way it’s supposed to. If you’re getting the last key instead of what is being pressed, then you’re using it wrong. Without seeing your code, we can’t tell you what you’re doing wrong.

Here’s as simple as the keyboard function gets.

function setup()
    showKeyboard()
    key=""
    
end

function draw()
    background(0)
    fill(255)
    text("Press a key..."..key,WIDTH/2,HEIGHT/2)
    
end

function keyboard(k)
    key=k
end

I’ve noticed, on various language forums, that the more mature members appreciate it when beginners have clearly made an effort to learn before asking questions. I’ve also noticed, on various language forums, that the more mature members sometimes get a little testy when someone seems not to have made the effort.

I do think one should read the Lua manual. At the same time, I wish there was more basic language material in the Codea help, because sometimes, cruising over to the Lua manual takes me way out of my flow. If I were sitting with my team, I might just stick up my head and say “what the heck is not equal in this language”, and someone would say “still ~=, you stupid clod”, and I’d say “thanks, susan” and get back to it.

Some aspects of Codea/Lua are quite weird. I’'ve been programming longer than most of you have been alive, and I have never seen anything quite so non-obvious as the touch logic. It has taken me some learning and practice to get it to work, and I still don’t think I have it quite right. So I’m not surprised when someone doesn’t get that one, or the keyboard one. It makes me think there needs to be a better writeup somewhere, and it needs to be right at one’s fingertips, like in the Codea help or something. Anyway, when someone asks about that, it’s quite all right with me and probably with most of us.

But my point is, and I do have one, on forums I’m more active on, and on the days when I just don’t feel like answering basic questions like when to use dot and when to use colon, well, I just don’t answer. Sometimes I do remind a questioner that the group likes to see that they’ve made the effort. But mostly, if I’m not up for the game, I sit it out.

I agree it’s a delicate balance. I like to see that someone has at least tried to look for an explanation before asking.

We get a lot of young kids who are used to just sticking their hand up when they get stuck, and/or who expect programming to be easy, and it sometimes takes them a while to get the idea that you have to learn to find your own answers as far as possible, and that it takes hard work.

It isn’t just programmers, though. I belong to non programming forums where exactly the same thing happens. People breeze in without looking at any of the help material and expect everything to be explained for them personally. (I’m not saying that’s what happened in this thread, I’m talking more generally).

With regard to your final point, I think it’s better to remind new users that they need to invest some time up front learning the language, rather than just ignoring their question. Especially when they are young kids just starting out.

I don’t mind answering simple questions where I know the answer or it only takes a second to lookup the correct answer. What I don’t like is trying to figure out ambiguous questions, spend time writing example code to help them, then they completely ignore what I gave them because it’s not EXACTLY what they want. They don’t want to take the time to look at the code to see what it does so they can figure out what to change on their own. But then that’s what I’m here for so I guess it doesn’t matter.

i hear ya. kids these days … :smile:

I’m sure we would have been just the same, the education system doesn’t (and never did) teach kids to think for themselves.

@dave1707 I wanted a way to tell when the keyboard was released. I had something similar to your function, but I couldn’t find a way to reset the key to ’ ’ if the key on the keyboard was not pressed. Do you know any good keyboard tutorials? The coolcodea one wasn’t of much help.

There are stacks of keyboard threads on here if you search, it’s a popular topic

@MattthewLXXIII I guess I would have to know what you’re trying to do with the keyboard/key value to give you a better answer. The code below sets key to nil at the start. If a key is pressed, key then contains the value. In draw(), if key is not nil then it prints the value then sets key back to nil. I’m not sure what you mean by when the keyboard was released.. The keyboard function is only called if a key is pressed, so you could say the keyboard is released right after a key is pressed. It’s up to you to do what you want with key.

function setup()
    showKeyboard()
    key=""  --set to nil
end

function draw()
    background(0)
    if key~="" then -- not equal to nil
        print("A key was presses, it was..."..key)
        key=""  -- set to nil
    end
end

function keyboard(k)
    key=k
end

Another way to do it is to use an indicator

function keyboard(k)
    keyboardActive=true
    --add code to process keystroke
end

function draw()
    if keyboardActive then 
        --keyboard is in use
        keyboardActive=nil --reset indicator
    else
        --keyboard is not in use
    end
end

Hi guys, thanks for your help. I want a character to move up when w is pressed, but stop moving when w is released.

-- Keyboard test

function setup()
    x=0
    y=0
    showKeyboard()
    ACTIVE = false
end

function keyboard(key)
    letter = key
    ACTIVE = true
end

function move()
    if ACTIVE then
        if letter == 'd' then
            x = x+1
        end
        if letter == 's' then
            y = y-1
        end
        if letter == 'w' then
            y = y+1
        end
        if letter == 'a' then
            x = x-1
        end
        ACTIVE = false
    end
end

function draw()
    
    background(40, 40, 50)
    move()
    translate(x,y)
    strokeWidth(5)
    ellipse(WIDTH/2,HEIGHT/2,10)
    
end

It doesn’t move very smoothly, how can I fix this?

@MattthewLXXIII The keyboard doesn’t work like that. When you press a key, you get the value once until you press another key. If you want to continue to use the keyboard for that, then you could use another key to stop the movement. You tap w to move up or one of the other keys to move in their direction, but you tap q to quit any movement. I suggest that you look into the use of buttons and skip the keyboard for the movement direction.

Ok, I will. Thanks Dave!

@MattthewLXXIII Here’s a simple example.

displayMode(FULLSCREEN)

function setup()
    rectMode(CENTER)
    x=WIDTH/2
    y=HEIGHT/2
    dx,dy=0,0
    up=button(WIDTH/2,200,"up",vec2(0,1))
    down=button(WIDTH/2,100,"down",vec2(0,-1))
    left=button(WIDTH/2-100,150,"left",vec2(-1,0))
    right=button(WIDTH/2+100,150,"right",vec2(1,0))
end

function draw()
    background(40, 40, 50)
    x=x+dx
    y=y+dy
    sprite("Space Art:Green Explosion",x,y)
    up:draw()
    down:draw()
    left:draw()
    right:draw()
end

function touched(t)
    if t.state==BEGAN then
        up:touched(t)
        down:touched(t)
        left:touched(t)
        right:touched(t)
    end
    if t.state==ENDED then
        dx,dy=0,0
    end
end

button=class()

function button:init(x,y,n,d)
    self.x=x
    self.y=y
    self.n=n
    self.d=d
end

function button:draw()
    fill(255)
    rect(self.x,self.y,80,40)
    fill(255,0,0)
    text(self.n,self.x,self.y)
end

function button:touched(t)
    if t.x>self.x-40 and t.x<self.x+40 and t.y>self.y-20 and t.y<self.y+20 then
        dx=self.d.x
        dy=self.d.y
    end
end
  1. Why is it that ellipse(v:unpack(),size) doesn’t work?
  2. Is there a function to calculate the distance between a point and a line?
    EDIT: I just removed an accidental strikethrough and did back ticks.

Also, thanks Dave, I just was trying to have more space on screen without buttons; I made the playing area smaller to fit a joystick on the side, so either my characters will be smaller or it will be too quick.

I don’t think you can add further parameters after an unpack command

Wrt touch, I would use an indicator as in my example above. Then it’s easy to keep moving as long as the indicator is true, and stop it when the indicator is false (or nil).

I don’t think you can add further parameters after an unpack command

This is correct. I’d avoid using unpack generally, because of the ambiguity over the number of parameters you’re getting back.

For my second question, I wrote a function if anyone wants it, is there a codea function which does the same?

function pointtoline(p,l1,l2) -- vectors, p is point, l1&l2 are points on line
    a = p-l1
    b = l2-l1
    return (a-((a:dot(b))*b)/((b):lenSqr())):len() --length of perpendicular vector resolute
end