We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello,
I'm trying to save the time in a variable to use it for a figure to disappear 10 seconds after it appears or disappear randomly. and for every figure that appears on screen.
At the moment It saves the time many times.
"""
Mouse Press.
Move the mouse to position the shape.
Press the mouse button to invert the color.
"""
#tick=millis()
def update_tick():
#text(12)
#textAlign(CENTER)
pd= millis()/1000
return pd
def setup():
global times
size(640, 360)
noSmooth()
fill(126)
background(102)
times=[]
def draw():
#times=[]
the_time=update_tick()
if mousePressed:
stroke(255)
line(mouseX - 66, mouseY, mouseX + 66, mouseY)
line(mouseX, mouseY - 66, mouseX, mouseY + 66)
times.append(the_time)
print times
else:
stroke(0)
Answers
https://forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text
Thank you, I've re formatted my question.
You should replace variable mousePressed w/ callback function mousePressed() so it triggers once, b/c draw() is called back at about 60 FPS.
Then you should consider to create a
class
to represent 1 Figure:http://py.Processing.org/reference/class.html
Take a look at Objects Tutorial's link below: $-)
http://py.Processing.org/tutorials/objects/
Thank you! Im following your example in, https://forum.processing.org/two/discussion/19200/create-10-instances-of-an-object-python#latest dont understand what is happening:
Its telling me AtrtibuteError: item instance has no attribute "x" in the function display
A class' constructor is called
def __init__(self):
, notdef __ini__(self):
! [-XAlso class' names should follow the UpperCaseCamel's naming convention. :-B
So your class should be renamed to:
class Item:
. B-)Ah yes, I had already noted that, but thanks.:D that's not the main problem, as still doesn't work :(
You've got an organizational problem about Separation of Concerns (SoC):
https://en.Wikipedia.org/wiki/Separation_of_concerns
For example your function update_tick(): It both displays current value of some undeclared global variable called tick & return current millis() divided by
1000
. @-)You should instead split those 2 different concerns as 2 distinct functions. *-:)
A slightly more serious concern violation is in your method leaves().
It accesses its own container and remove() itself from it! @-)
Although it's completely possible given my_items is a global variable, it's still bad programming practice, b/c an object should be independent from its container. =;
Rather than remove() itself, it should flag itself for removal.
Then the top main program can invoke remove() when that condition is
True
. B-)You even assign to property i the current value of global variable i. :-@
If you feel you need some static counter, you can declare it inside the
class
itself. :\">Thanks, I had already solved :-)