Hi all,
I'm a beginner, so don't judge.
I want to use the touched() function for a character to be moved. However, when the finger is on the screen, but not moving the touched function is not called. Is there an easy way to fix this?
Thanks.
@MattthewLXXIII Here you go! Just create a variable like tch that saves the data from your touched() function. Then you can use that variable in draw()
function setup()
tch = nil
end
function draw()
background(40, 40, 50)
strokeWidth(5)
if tch then
ellipse(tch.x,tch.y,100)
-- Add the rest of your code here
end
end
function touched(touch)
if touch.state == ENDED then
tch = nil
else
tch = touch
end
end
This forum isn't a substitute for reading the reference material on Lua and Codea.
It's fair enough to ask if you get stuck, but asking what ~= means, or about the different states of the touched function, or how unpack works, is wasting our time because they are explained in the Codea reference or in the Lua manual. If you don't understand the explanations there, ask, but please read them first.
I'll concede the ~= and the keyboard, but I'm confused about the unpack() function. It returns 2 values, but when you compare it, it only uses the x value. What is the Lua manual? @Ignatz you don't have to answer.
The "dot notation" (nothing to do with dot product) returns the components of a vector. Eg `pos.x`. So `translate(pos:unpack())` is the same as `translate(pos.x, pos.y, pos.z)` EDIT: or `translate(pos.x, pos.y)` depending on whether pos is vec2 or vec3
How do I tell if the keyboard is not being pressed? The keyboard function appears to return the last key which was pressed as opposed to what is being pressed at that moment.
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.
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.
@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.
@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
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.
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
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).
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
No, but several of us have used this formula or one like it, to get the shortest distance to a line
Here is a post I wrote about the formula our mathematician in residence provided (his version isn't quite the same because it restricts the line so it can't go past the endpoints, ie it won't be perpendicular in that case.
@MattthewLXXIII - if you look at the index page of my blog (from the link above) you'll see my ebooks. They have a lot of math tricks and handy functions in them, especially the 3D grabbag book. It was really hard work finding them, so I thought I'd make it easier for the next person....
function setup()
dostuff=0
end
function draw()
background(255, 255, 255, 255)
if ElapsedTime==1 then
dostuff=dostuff+1
end
strokeWidth(5)
fill(0)
if dostuff == 1 then
ellipse(100,100,100)
end
end
If you are implementing a set of states, then it is more efficient to write it like this
states={DrawMenu,DrawSettings,DrawPlay,DrawEndOfGame}
function setup()
state=1
end
function draw()
states[state]() --run function for current state
end
--draw functions for different states
function DrawMenu()
--code to draw menu screen
--you can transition states easily
state=2 --change to Settings screen
state=3 --start playing the game
end
function DrawSettings()
--code to draw and manage settings screen
--at the end you can revert to the menu
state=1
end
This is much cleaner than having a big draw function with lots of if statements, and runs faster because you don't need to test what state you are in, each time you draw.
Thanks Ignatz, I was using it for something else, but that will be useful too. I think I saw that on one of your links. I was just wondering how ElapsedTime works, why doesn't it just add DeltaTime every frame?
I would expect that it does, but DeltaTime is not exactly 1/60 of a second, it depends on frame rate, so total elapsed time is probably never an integer.
isn't ElapsedTime continually increasing? If so, most of the functions above would draw the ellipse once and once only, which will not likely be visible on the screen at all ...
Comments
@MattthewLXXIII Here you go! Just create a variable like
tch
that saves the data from yourtouched()
function. Then you can use that variable indraw()
Thanks guys!
What is ~=?
Is there a way I can change the title to ask a different question?
The ~= is not equal. Just ask your new question here.
What is != then?
Is there a way to receive input from a bluetooth keyboard?
Thanks.
!= is also not equal, but not in Lua.
Ah, ok.
Do you know the answer to my second question?
Search this forum
Fair enough, thank you.
How do you access the y value on the unpack?
This forum isn't a substitute for reading the reference material on Lua and Codea.
It's fair enough to ask if you get stuck, but asking what ~= means, or about the different states of the touched function, or how unpack works, is wasting our time because they are explained in the Codea reference or in the Lua manual. If you don't understand the explanations there, ask, but please read them first.
I'll concede the ~= and the keyboard, but I'm confused about the unpack() function. It returns 2 values, but when you compare it, it only uses the x value. What is the Lua manual? @Ignatz you don't have to answer.
You can't compare a list of two values with something else. It's basically saying
Which is obviously invalid
This is what unpack is for
For the Lua manual, try google ((Codea is built on the Lua language)
Thanks, but is there a function not involving dot product to get the x and y values for a vector?
EDIT: (individually)
Thanks @yojimbo2000.
please learn the Lua language, then you won't need to ask these basic questions
I have written posts and ebooks that may help
https://coolcodea.wordpress.com/2013/06/19/index-of-posts/
How do I tell if the keyboard is not being pressed? The keyboard function appears to return the last key which was pressed as opposed to what is being pressed at that moment.
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.
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 ...
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(), ifkey
is not nil then it prints the value then setskey
back to nil. I'm not sure what you mean bywhen 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 withkey
.Another way to do it is to use an indicator
Hi guys, thanks for your help. I want a character to move up when w is pressed, but stop moving when w is released.
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.
ellipse(v:unpack(),size)
doesn't work?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).
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?
No, but several of us have used this formula or one like it, to get the shortest distance to a line
Here is a post I wrote about the formula our mathematician in residence provided (his version isn't quite the same because it restricts the line so it can't go past the endpoints, ie it won't be perpendicular in that case.
https://coolcodea.wordpress.com/2015/01/18/193-shortest-distance-from-a-point-to-a-line/
@Ignatz, thanks, his version is nicer.
@MattthewLXXIII - if you look at the index page of my blog (from the link above) you'll see my ebooks. They have a lot of math tricks and handy functions in them, especially the 3D grabbag book. It was really hard work finding them, so I thought I'd make it easier for the next person....
Another question guys:
Why doesn't the ellipse ever draw?
Lack of precision. ElapsedTime probably won't ever equal 1 exactly. Try this
I put the dostuff test first because it is (probably) faster than looking up ElapsedTime
If you are implementing a set of states, then it is more efficient to write it like this
This is much cleaner than having a big draw function with lots of if statements, and runs faster because you don't need to test what state you are in, each time you draw.
Thanks Ignatz, I was using it for something else, but that will be useful too. I think I saw that on one of your links. I was just wondering how ElapsedTime works, why doesn't it just add DeltaTime every frame?
I would expect that it does, but DeltaTime is not exactly 1/60 of a second, it depends on frame rate, so total elapsed time is probably never an integer.
If you want ElapsedTime to be an integer, just do
However, I wouldn't use this approach simply to tell you if one second has passed
isn't ElapsedTime continually increasing? If so, most of the functions above would draw the ellipse once and once only, which will not likely be visible on the screen at all ...