We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have a story that is read by mouseWheel scrolling. At the some words there could be events like as fading screen out, playing an audio while we are scrolling the text. How can i achieve this events based on the text position? This is the code is below;
String[] headlines = {
"Aniden yerimden fırladım ve üzerine atladım. Ayağım takıldı ve düşmemle birlikte kafamı yere çarptım. Uyandığımda sırılsıklamdım",
};
String c="Please scroll the mouse wheel to read the story";
PFont f;
PFont f2;
float x;
int index = 0;
void setup() {
size(800, 450);
textAlign(CENTER);
f = createFont("calibri", 16, true);
f2 = createFont("calibri", 10);
x = width;
}
void draw() {
background(245);
stroke(255, 200, 0);
fill(255, 200, 0);
rect(293, 164, 220, 20);
textFont(f, 16);
fill(100, 100, 100);
text(headlines[index], x, 180);
textFont(f2, 10);
fill(70);
text(c, 400, 400);
stroke(255, 255, 255);
fill(255, 255, 255);
rect(500, 164, 400, 20);
rect(0, 164, 293, 20);
if (x < -textWidth(headlines[index])) {
x = width;
index = (index + 1) % headlines.length;
}
}
void mouseWheel(MouseEvent event) {
x-=event.getCount();
}
Answers
You have two variables that decide which text is drawn and where it is drawn.
The first variable is index. This decides which of the strings in the headlines array are drawn. Since there is only one string in that array, index is always 0, so the first and only string is the one that gets drawn.
Where it is drawn is a different matter. The variable x remembers how much of the text has been scrolled, and adjusts the text's position by that much.
If you want different things to happen, you will have to make them happen when the x variable reaches a certain value. You can use if statements to make things happen when x is in a certain range of values by checking the value of x against some other value in an if statement.
For example:
Notice that this causes this event (of printing a string to the console) to happen continuously! If you want to cause one time events, you might consider adding boolean variables to track when they happen.
How do you make the background darker? Have another variable that remembers how dark it is, use it when you draw the background, and make it a "darker" value when the fadeing code block runs.
I see. We need boolean to cause one time events. Could you edit this code for me? I translated the story to english. After the sentence "i hit my head to floor" could you cause fade out screen one time, then turn the light on to continue story ?
You have to at least try to make an effort to understand and add the code yourself first.
Of course I tried it, your boolean code didn't work, and I couldnt find how to make events in boolean for example fading out. When I type your code, it says "fade" is not suitable. Then I asked your help to edit the code for me. Do you know I have making effort for 3 months for this project. If nobody tell, I can't solve this technical problems, and my English is not much good, it is not my native language. Progressing uses English only?
What is the meaning of the statement that you wrote "x < -10 && !fade )& !fade"
Boolean statements uses < , >, =, ! .... so how should i get this? this statement doesn't interact the value of my text.
Now In my project we need text orientation "x". Right? So how should i connect the value text x to the boolean fade? Should i start to learn the sublect in that link; https://processing.org/reference/logicalAND.html ?
This is a conditional expression that causes code to run only when the expression evaluates to TRUE.
There are two parts. On the left, we are checking to see if the value stored in the variable x is less than negative ten. For example, if x is -11, this is TRUE since -11 < -10. If x is instead, for example, 4, then it is FALSE since 4 < -10 is not a true statement.
On the right side we are looking at the boolean variable fade. This variable can either be TRUE or FALSE. But we are using the NOT operator (!) to change this to the other value. So when fade is TRUE, !fade is FALSE. When fade is FALSE, !fade is TRUE.
Then we combine them using a LOGICAL AND (&&). This returns true if both sides are TRUE. If either side is FALSE, it returns FALSE.
In total, the expression is making sure that the following code only happens when x is less than negative ten and variable fade is set to false.
Chrisir, thank you so much. The part of case and break structure are diffucult to understand for me for now. I hope to learn it soon. By the way, if i continue to write the story in string, the text position is changing. How can i change the starting position of my text? x value seems not editable because of the mousewheel sliding control x. I couldn't achieve it.
Second; I made the text mask with patch rectangles around it. But my patch method seems weird while the bg color fading out. How can i mask the text in middle box? Is it possible to use PImage masking on text? or how?
Switch() is a lot like if(), but instead of saying if(stateBkg == normal) you say case normal. It's a way of checking for different states for a variable. If the case is normal (stateBkg == normal) the code follong will be executes. If then the case is gettingDarker (stateBkg == getting darker) the following code will be executed as well as the first code. Unless you use break;
break; tells the programming to break out from the current code. If you use it in a for loop the code will stop the iteration and go on to the code after the for loop. When you are using it in switch() you are saying "stop executing this code"
You can check the reference for a different explanation if this didn't help.
How can you change the starting position of your text? By changing x! In setup you are defining x with the line: x=width; if you don't want x to be the size of the width you can for example say x = 7; instead.
Are you sure? When the Story gets longer, start is still the same.
Second: yeah, I noticed that too. Instead of using
background
, you could use arect
with growing opacity (seefill()
)Yes Chrisir, I am sure. The text start position is changing when i type more words to write the story in string. I tried to define start value for x, but i couldn't achieve it. Instead of using bg, If i want to use rect to fade out, Where should i type rect fill() ? under the part "switch"?
Really?
What Happens?
What do you mean? If you want to see it, type more words in string text and you can see the change text position in preview.
you are right, I am sorry. I apologize.
in this new version I changed a few things.
First, you said, we have a problem with the text when it gets longer (and I didn't believe you, sorry). It was caused by
textAlign(CENTER);
insetup()
. It centers the text. Thus when we make the text longer, it shifts left (as you said). To avoid this, I deletedtextAlign(CENTER);
insetup()
.(The small help text still uses
textAlign(CENTER);
indraw()
but after the text I reset the alignment of the text back to LEFT with the linetextAlign(LEFT);
(which is the default).)Second, I improved the part where the screen is getting dark. Instead of using
manageBackground();
(usingbackground()
) at the beginning of draw(), I now usemanageRectCoveringTheScreen()
(using a rect covering the entire screen) at the end ofdraw()
. Here I just cover the entire screen with a rect. The opacity of the rect increases from 0 until it is full opaque (covers completely all underneath).(Additionally, I remove opacity from the text and the yellow rect and the help text using
fullOpacity
)Also, I hide the mouse.
a word about
switch()
Let me say a word about
switch()
. It has been explained already. But the idea behind it is that we have 4 situations:normal display (before in the story he falls to the floor),
then the rect is getting darker (more opaque),
it stays totally dark a second (more or less) and
then it gets brighter again.
To have full control over the 4 situations, I use a small trick: I use a var
stateBkg
(state of background, should be named state of rect now or so) which can have different values: 0,1,2,3, to which I gave names:normal
,gettingDarker
,waiting
,gettingLighter
. So from the value ofstateBkg
(normal, gettingDarker, waiting, gettingLighter) I always know which situation we have. So I have full control of the process, getting darker, stay dark, getting bright again. I now evaluatestateBkg
withswitch()
inmanageRectCoveringTheScreen()
and act different for each situation (case...
andbreak;
are the brackets around the handling of each situation if I may say so).Third, I implemented another event that occurs at a certain point in the story, a red ball falls down in the function
manageBallFalling()
.Chrisir. You are great! Thank you so much.
I am improving the code according the story with teachings by you. Can you check my last editing code please? I did add some different events as much as learned from you. But i couldn't achieve to put different rect color to covering screen in different times. I would color the first one to black when the story "hit his head to floor" Because of eyes off. After getting lighter story will continue from the sentence"When I woke up". I wrote the code for it but didn't work with two rect. It worked with one rect.
Then in the continuing of story "the water getting dark" i want to color it dark blue and make cursor visible, also manageable but text didn't slide when i continue to mouseWheel.
By the way, how can we add sound effect based on text position?
when you download the library minim, you see it comes with a lot of examples, e.g. TriggerASample what you want.
here is another example for sound with the library minim :
I corrected some mistakes here
i come back to you
with more help
Okay Chrisir. I appreciate so much. I am trying to improve and create some different events but i got stuck in show different rect or different image in different times (x position)...
Why?
Just check
if(x< -288)
then start event and set a booleansomethingHasBeenDone
to trueI duplicate the manageRect, int and other parameters required by rect fading in and out. Then i set different colors to rects which are in different times. But each rect are same color unluckily.... why? I think I need a sample.
small improvement, first rect black, second blue now
reason: you used
stateBkg
falsely for both rectsI kept it that way but
manageRectCoveringTheScreen2()
does now some checks whether it should run alreadyThank you Chrisir. I see that function must be stateBkg==normal. I couldn't think of adding boolean too. I should study more... I downloaded minim library. Tomarrow I will try to add some sound effects. See u. Thanks
you are welcome! ;-)
I used a trick to check whether
manageRectCoveringTheScreen2()
is allowed to run.Alternatively, you could use
stateBkg
andstateBkg2
to avoid this problem!!!!see you! ;-)
Hi Chrisir. How are you? I have been studying using audio in processing for 5 days. I could add bg sound and also managing a sound efect for a specific text position. But I have a problem with memory... Java preview is getting slow and stuck :( Do you know what cause to this?
post your entire code
it's important you load sounds only in setup() and not in or from draw()
Okay. I add below. There is 2 sound files if u want to get. Maybe i can send via mail.
it might be wise to load mp3 instead of wav-file
there are mp3. you mean wav is wise?
i read your code
please load all mp3 in setup not somewhere else
no, mp3 is good
I see. But i managed one sound in another void at the end (for text x position to trigger it) How can i load all in setup with this trigger?
you need different vars like fallingSnippet etc.
and load their content in setup with loadSnippet
when the effect occurs just say fallingSnippet.play();
I tried that, but i could't achieve. Should i put the var as below?
int fallingSnippet=man_falling_down.mp3;
and;
void setup() { minim = new Minim(this); player = minim.loadSnippet("fallingSnippet"); }
then;
here:
you forgot to say
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
so he does it again and again!!!!!!
I sent you an e-mail concerning the sound
line 369 above is also wrong since you have defined minim in setup() already
I got it. Thank you so much. How we use "null"? I thought it means; "not here". I coulnd't connect the parts in my mind.
I mean what is the difference if we write Minim minim; AudioPlayer underwaterSound; AudioSnippet manFallingDown;
without null. Does we define the sound not here?
I am not sure.
null
means that it is not defined, so there is nothing in it, no sound...without
= null
could also be null automaticallyI am using
null
to check if the loading of an mp3 was succefuldid you get my e-mail?
Yes i got you e-mail. Thank you. I see the definition null... Now I am working on interative rect that faded out, the mouseButton for fading as it click more. I hope to achieve it my own way :) If I can't, i may ask. Thank you again and again.
Have a good evening Chrisir!
a good evening to you, too, Sir.
I would like to ask you the problem of the code that i write, I am trying to do "rect that is getting darker will be interactive with mouse press. When pressing in rect, it will be getting lighter, when we don't press it will be darker. But i need to make it lighter at last. Getting darker will be disable with maybe press time or passing 5 second. (which one is easy is ok)"
This is the code i mess up;
We are working with stateBkg
So work with this
void mousePressed(){
if (stateBkg==gettingDarker)
stateBkg=gettingLighter;
}
Hello. How can I make this code interactivity real? I press but there is nothing chance. The background must be clicable to change light to dark...