3.4: Boolean Variables - p5.js Tutorial

3.4: Boolean Variables - p5.js Tutorial

The Coding Train

55 лет назад

147,744 Просмотров

This video covers the boolean variables in p5.js. I first look at mousePressed, and also discuss creating your own boolean variable to track the state of a button. Code: https://thecodingtrain.com/tracks/code-programming-with-p5-js/code/3-conditionals/4-boolean

🕹️ p5.js Web Editor Sketch: https://editor.p5js.org/codingtrain/sketches/yzBpV0CI

🎥 Previous video: https://youtu.be/r2S7j54I68c?list=PLRqwX-V7Uu6Zy51Q-x9tMWIv9cueOFTFA
🎥 Next video: https://youtu.be/cnRD9o6odjk?list=PLRqwX-V7Uu6Zy51Q-x9tMWIv9cueOFTFA
🎥 All videos: https://www.youtube.com/playlist?list=PLRqwX-V7Uu6Zy51Q-x9tMWIv9cueOFTFA

Timestamps:
0:00 Introduction
0:48 Boolean Variables -- TRUE or FALSE
3:00 Distinction Between Mouse is Clicked and Mouse is Pressed
8:00 Assigning States to the Program
11:50 mousePressed()
13:35 Toggling the State
14:36 The NOT operator

Editing by Mathieu Blanchette
Animations by Jason Heglund
Music from Epidemic Sound

🚂 Website: http://thecodingtrain.com/
👾 Share Your Creation! https://thecodingtrain.com/guides/passenger-showcase-guide
🚩 Suggest Topics: https://github.com/CodingTrain/Suggestion-Box
💡 GitHub: https://github.com/CodingTrain
💬 Discord: https://discord.gg/hPuGy2g
💖 Membership: http://youtube.com/thecodingtrain/join
🛒 Store: https://standard.tv/codingtrain
🖋️ Twitter: https://twitter.com/thecodingtrain
📸 Instagram: https://www.instagram.com/the.coding.train/

🎥 Coding Challenges: https://www.youtube.com/playlist?list=PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH
🎥 Intro to Programming: https://www.youtube.com/playlist?list=PLRqwX-V7Uu6Zy51Q-x9tMWIv9cueOFTFA

🔗 p5.js: https://p5js.org
🔗 p5.js Web Editor: https://editor.p5js.org/
🔗 Processing: https://processing.org

📄 Code of Conduct: https://github.com/CodingTrain/Code-of-Conduct

This description was auto-generated. If you see a problem, please open an issue: https://github.com/CodingTrain/thecodingtrain.com/issues/new

#boolean #variables #p5js #javascript

Тэги:

#mouseIsPressed #p5.js #javascript #button #rollover #tutorial #conditional #boolean #Boolean_Data_Type #Variable #boolean_variables #boolean_variable #p5js #daniel_shiffman_boolean #javascript_example_for_boolean_varaible #p5.js_button #rollover_daniel_shiffman #p5js_tutorial #p5.js_tutorial #variables #javascript_(programming_language) #conditionals
Ссылки и html тэги не поддерживаются


Комментарии:

@hanalevakova7602
@hanalevakova7602 - 09.11.2020 15:39

👍
Yea,its true!
Your videos are great! Keep up a good work ;)

Ответить
@nathen1778
@nathen1778 - 20.11.2020 05:40

He should be my coding teacher, he is insane!!!!

Ответить
@yotiis2782
@yotiis2782 - 02.12.2020 22:20

It it’s true I watch the video in sequence and I plan on finishing the,

Ответить
@ertugrulsrt5761
@ertugrulsrt5761 - 27.01.2021 15:04

Everytime i press something, it detects it like 3 or 4 times. So, i can not switch on and off consistently. How can i solve this

Ответить
@terracoon9882
@terracoon9882 - 19.02.2021 16:23

good

Ответить
@sjeffries9248
@sjeffries9248 - 22.02.2021 02:27

Then I live in your fantasy world.

Ответить
@spaceviolet4535
@spaceviolet4535 - 06.03.2021 04:10

var watchingVideoIn Sequence = true

Ответить
@willmacrae2310
@willmacrae2310 - 09.03.2021 23:42

Tons of people are watching, you do an insanely good job making the content easy and fun! Thanks for making all of your videos :)

Ответить
@tarkovwkx
@tarkovwkx - 18.03.2021 01:38

AHHHHHHHHHH this is so great

Ответить
@alimansourey2076
@alimansourey2076 - 23.03.2021 13:29

The best teacher I have ever seen . The way he explains the importance of boolean data is brilliant 🥵

Ответить
@1morelight
@1morelight - 28.05.2021 19:36

I think at this point we need a video about the general concept of data types and what they're implemented in JS and p5.js. Maybe you already have one in the following videos :)

Ответить
@wirito
@wirito - 07.08.2021 17:31

His if( mouseX > 250 && mouseX < 350.... ) is bothering me. This means that if approaching the square from the left, it will be filled before you even touch it. Similarly, if you approach it from the right it won't get filled at the 400 pixel mark (which is when the square starts from the right.

The correct numbers should be mouseX > 300 && mouseX < 400.

Could you explain why you did 250 and 350 for X please?

Ответить
@arcturusk649
@arcturusk649 - 25.09.2021 02:26

God bless you Dan!

Ответить
@MerlinZuni
@MerlinZuni - 15.11.2021 23:17

It 2021 and we are still watching. Thanks for your great work. Your deserve a nap.

Ответить
@omtrivedi5555
@omtrivedi5555 - 16.12.2021 08:03

plss tell me 😢🙏whats the error in this code here i am not getting background colour even when mouse is pressed
var on = false;
function setup() {
createCanvas(600, 400);
}

function draw() {

if (on) {
if (mouseIsPressed) {
background(0, 255, 0);
} else {
background(0);
}
}

background(0);
stroke(255);
strokeWeight(4);
noFill();
if (mouseX > 250 && mouseX < 350 && mouseY > 150 && mouseY < 250) {
fill(255, 0, 200);
}
rectMode(CENTER);
rect(300, 200, 100, 100);

function mousePressed() {
if (mouseX > 250 && mouseX < 350 && mouseY > 150 && mouseY < 250) {
on = !on;
}
}

}

Ответить
@svfactory
@svfactory - 20.01.2022 17:00

haha, yes watching all videos after videos, thanks for your work help a lot.

Ответить
@andrewperkinson8941
@andrewperkinson8941 - 21.01.2022 16:10

Hello everyone! when i put "function mousePressed() " in DRAW it does not work for me, in this example to turn the rect pink.
putting it in setup i do not get the issue, however i need it in draw

Ответить
@rorothegoatboat
@rorothegoatboat - 31.01.2022 08:41

create 500 black shapes with a 40% chance that they'll be red and if they land on the top half of the canvas make them smaller.

Ответить
@Nekromant05
@Nekromant05 - 22.02.2022 02:23

I once was supposed to make a game where you have a button that will jump away after a few seconds and you have to press it in time and get a point each time you press it. But I was a bit mean and instead made it that the button will jump away if you get near it with the mouse. :)

Ответить
@NickColemanLive
@NickColemanLive - 28.03.2022 22:47

Mate, im watching these video one after another. Thank you for these thoroughly enjoyable series of lessons and introduction to P5. Greetings from Dusseldorf

Ответить
@reneperol
@reneperol - 29.03.2022 16:29

Watching in sequence with little peaks in the future for motivation. I am starting from scratch with zero coding background, and you sir are an awesome teacher, I can't thank you enough for these videos.

Ответить
@samaquiaslorta2693
@samaquiaslorta2693 - 15.04.2022 21:10

Dont work too hard! your a dope teacher 🙌🙌

Ответить
@brianballs5289
@brianballs5289 - 07.06.2022 22:43

I rarely write comments but I just have to thank you for these videos. I stumbled upon Creative Coding and p5 while looking for some fun side projects to do while working on The Odin Project. Your videos have really helped me grasp and understand the concepts that I've been learning so far and have been immensely helpful for me to visualize what all these functions and commands are actually doing in JavaScript. Its been so refreshing to be utilizing everything I've learned so far in a different way by making something creative. Thank You!

Ответить
@jasonoboti
@jasonoboti - 11.08.2022 22:35

I learnt alot... but only attentive students will hear the sirens at the background at some point in this video

Ответить
@stevemcfish
@stevemcfish - 26.08.2022 16:47

on=!on;

Ответить
@juvenalalfonso310
@juvenalalfonso310 - 26.09.2022 02:13

its true I'm watching them.

Ответить
@morrownow
@morrownow - 21.10.2022 19:06

HELP! It doesn't work for me, I don't know what I'm doing wrong,...

var x = false;

function setup() {
createCanvas(600, 400);
}

function draw() {
if (x) {
background(50, 200, 50);
} else {
background(50);
}

function mousePressed(){
x = true;
}
}

Ответить
@jackym3204
@jackym3204 - 29.11.2022 14:44

Actually, most of my class is watching this series! Thank you for making this, I am wildly lost for 99% of my assignments, and these videos have gotten me through every one of them.

Ответить
@goldthumb
@goldthumb - 01.01.2023 02:47

Don't be "fooled" by the title. We learn many p5js specific concepts here. Important p5js stuff like "mouseIsPressed" and "mousePressed()" will be explained carefully by Daniel. That's why I don't want to skip any one in this 100-video series. There are still 87 to go.

Ответить
@lindaszeto979
@lindaszeto979 - 01.01.2023 06:07

I am watching these in sequence. :)

Ответить
@dekattenheld1442
@dekattenheld1442 - 10.01.2023 23:48

how do you put all that stuf in comments at once?

Ответить
@lanenaamorcita
@lanenaamorcita - 28.01.2023 01:35

I actually watch them in order, thank u! It helped tons.

Ответить
@frax520l
@frax520l - 03.03.2023 23:11

You ask, if any<body watches the videos in a row --> that is me

Ответить
@theBigNine
@theBigNine - 14.03.2023 00:26

Thank you for making this videos, I watch your videos and also coding along with it. Your videos explain things throughly and it helps me learn how to code in general, not just in JavaScript p5js. Thank you, Sir. 😎👍

Ответить
@affcffa
@affcffa - 03.05.2023 21:47

Bro, you are awesome! , thanks for teach, and thanks for teach how to teach, im looking your videos 1 by 1 just because i'm having fun.

Ответить
@ricccc4028
@ricccc4028 - 04.05.2023 04:22

Thanks!

Ответить
@heozhai8068
@heozhai8068 - 07.05.2023 09:35

Thank you so much for the video. but I would suggest understanding the coding "0n=!On" as PUTTING a new value to the variable in a logical expression way since the syntax used the " = " as the working launguage.
It would be easy to understand for me, and not be much confused by the logical understanding of what "=" means.
many thanks again, very helpful for me, a 45-year-old start learner of programming.

Ответить
@Mel-mu8ox
@Mel-mu8ox - 12.05.2023 16:58

if mousePressed = true
laptop begins to float

XD

Ответить
@lidakmify
@lidakmify - 12.05.2023 17:46

Still after 7 years, we are watching your videos and of course in order! Thank you, you're the best teacher!

Ответить
@angeseba2009
@angeseba2009 - 25.05.2023 19:26

yes, im watching... also, in order :)

Ответить
@Muffin--Man
@Muffin--Man - 07.08.2023 22:03

kinda repetitive i know🤔

var x=200,y=200,speedx=2,speedy=3,d=100,c;
function setup() {
createCanvas(400, 400);
c=createGraphics(400,400);
c.clear();
}

function draw() {
if(d>200||d<100||speedx>16||speedy>=48){
d=100;
speedy=3;
speedx=2;
}
background(220);
c.rectMode(CENTER);
c.rect(20,380,20);
c.rect(50,380,20);
c.rect(80,380,20);
c.rect(110,380,20);
c.rect(140,380,20);
c.rect(170,380,20);
c.rect(200,380,20);
if(x<=d/2 ||x>=width-d/2){
speedx*=-1;

}
if(y<=d/2 ||y>=height-d/2){
speedy*=-1;
}
x+=speedx;
y+=speedy;

circle(x,y,d);
text('X',x-30,y-10);
text('X',x+20,y-10);
arc(x,y,50,20,0,PI,CHORD);
c.text('Y',46,385);
c.text('X',16,385);
c.text('S+',74,385);
c.text('S-',104,385);
c.text('Sx',134,385);
c.text('Sy',164,385);
c.text('R',194,385);
image(c,0,0);
}
function mousePressed(){
if(mouseX>10&&mouseX<30&&mouseY>370&&mouseY>360){
speedx*=-1;
}
if(mouseX>40&&mouseX<60&&mouseY>370&&mouseY>360){
speedy*=-1;
}
if(mouseX>70&&mouseX<90&&mouseY>370&&mouseY>360){
d+=20;
}
if(mouseX>100&&mouseX<120&&mouseY>370&&mouseY>360){
d-=20;
}
if(mouseX>130&&mouseX<150&&mouseY>370&&mouseY>360){
speedx*=2;
}
if(mouseX>160&&mouseX<180&&mouseY>370&&mouseY>360){
speedy*=2;
}
if(mouseX>190&&mouseX<210&&mouseY>370&&mouseY>360){
x=200;
y=200
}

}

Ответить
@brunorocha9898
@brunorocha9898 - 13.09.2023 23:45

we are watching :D

Ответить
@toshevislombek
@toshevislombek - 21.01.2024 11:42

still we are watching your videos, and Also I'm yousing your video to teaching

Ответить
@cassianunes9486
@cassianunes9486 - 26.03.2024 17:43

I made to make the turn on/off work but not in dependence of the mouseX mouseY position. Even if I click outside of the rectangle the switch is working. Does anyone know how do fix it?

Ответить
@PanchayaSieng
@PanchayaSieng - 15.10.2024 18:56

When I was using a mouseIsPriessed, the execution didn't work. So, I tried functionmouseClicked instead, and it works. (Oct, 2024)

Ответить
@Mindurown8
@Mindurown8 - 28.11.2024 02:26

I’ve been watching your videos on and off for over 4 years now. I appreciate what you do !!

Ответить