Javascript Confusion! Functions acting up. via /r/learnprogramming

Javascript Confusion! Functions acting up.

Update: I think it may have something to do with the fact that I'm using p5js. Anything I put inside the setup() and draw() functions doesn't work (those aren't called explicitly, it's supposed to be done automatically.)


So, I have some code (that I've pasted below) which runs a simple game. It uses p5js and p5play (which are linked in the <head>), and works perfectly when it's not in a function.

However, one day I decided that there should be a 'welcome' screen for the game, so I put it all in one big function. Well, apparently Javascript doesn't like that, because now, no matter what I do, it only shows the ALERT and then does nothing!!!

You can visit the website here, too.


function run(){ alert('hi'); var Squares; var left = 5; var circles; var MARGIN = 40; var Closer; var x = 0.1; function setup() { Squares = new Group(); createCanvas(window.innerWidth,window.innerHeight); circles = new Group(); for (var i=0;i<30;i++){ var circle = createSprite(random(0+window.innerWidth/2,window.innerWidth), random(0+window.innerHeight/2,window.innerHeight), 50, 50); var img = loadImage("sun.png"); circle.addImage(img); circle.setCollider("circle", -4,4,90); circle.setSpeed(random(3,2),random(0,360)); circle.scale = random(0.2,0.5); circle.mass = circle.scale; circles.add(circle); circle.maxSpeed = 5; } var img = loadImage("CloserBullet.png"); closerSprite = createSprite(window.innerWidth/2,200,50,50); closerSprite.addImage(img); } var number = 0; var alive = true; function draw() { if (alive==true){ number +=1; x += 0.00049; } if(number%200 == 0){ var imgOther = loadImage("moon.png"); var Square = createSprite(random(0,window.innerWidth),random(0,window.innerHeight), 50, 50); Square.addImage(imgOther); Square.scale = 0.2; if (Squares.length > 5){ for (var i=0;i<Squares.length;i++){ s = Squares[i]; s.remove(); } } Squares.add(Square); } if(closerSprite.overlap(Squares)){ x = x*0.75; for (var i=0;i<Squares.length;i++){ s = Squares[i]; s.remove(); } } closerSprite.setCollider("circle", -1.8,-1.8,88*x); closerSprite.scale = x; //circles.bounce(closerSprite); circles.bounce(circles); background(0,0,0); fill(255); textSize(20); textAlign(CENTER); text("You've been alive for " + number + " diep seconds.", window.innerWidth/2, 100); closerSprite.velocity.x = (mouseX - closerSprite.position.x) * 0.2; closerSprite.velocity.y = (mouseY - closerSprite.position.y) * 0.2; for (var i=0;i<allSprites.length;i++){ s = allSprites[i]; if(s.position.x<0){ s.position.x=1; s.velocity.x=abs(s.velocity.x); } if(s.position.x>window.innerWidth){ s.position.x=window.innerWidth-1; s.velocity.x= -abs(s.velocity.x); } if(s.position.y<0){ s.position.y=1; s.velocity.y=abs(s.velocity.y); } if(s.position.y>window.innerHeight){ s.position.y=window.innerHeight-1; s.velocity.y = -abs(s.velocity.y); } } for (var i=0;i<Squares.length;i++){ Squares[i].rotationSpeed=random(0.1,0.3); } var executed = false; if(closerSprite.overlap(circles)){ fill(255); textAlign(CENTER); textSize(50); text("Game Over! Reload to play again.", window.innerWidth/2, window.innerHeight/2); for (var i=0;i<allSprites.length;i++){ s = allSprites[i]; s.maxSpeed = 0; s.rotationSpeed=0; alive = false; } } textAlign(CENTER); textSize(20); text("Click/tap to use the force field powerup. You have " + left + " remaining!", window.innerWidth/2, window.innerHeight-100); drawSprites(); 


function mousePressed() { if (left > 0){ left -=1; for (var i=0;i<circles.length;i++){ circles[i].attractionPoint(-100,closerSprite.position.x,closerSprite.position.y); } } 




Submitted October 31, 2016 at 06:29PM by –Nylon
via reddit

Database Design question via /r/learnprogramming

Database Design question

Hi guys and gals,

My Professor is being oh so unhelpful with this problem. The book asks us to draw a relational schema with its dependency diagram as part a. As part c, it wants us to draw a Crow's Foot Notation ERD. When asking the professor what is a relational schema he replied with,

"There should be schema. Schema are there to make things clear. So draw the crow's foot diagrams (schema) where applicable."

This makes no sense to me being part C specifically asks for Crow's Foot.

Can anyone give me a better explanation or example please?

Thanks in advance!

Submitted October 31, 2016 at 06:30PM by rapidcheetah
via reddit

I Need Help, Plain and Simple. via /r/learnprogramming

I Need Help, Plain and Simple.

Hey r/learnprogramming! I come to you today to ask for help with a program I want to make(I have no programming experience at all.) I want a program which is a wheel spinner based off a given percent to win. Basically a wheel spin with Player 1(75%) vs Player 2(25%) and will spin randomly to see who wins. If you know of a program or web app like this let me know please.

The program will be based off a percent to win and will also be able to carry up to four "players" if there is not a program like this it is okay, this is just how I want it to be.

Submitted October 31, 2016 at 06:40PM by kappasloth
via reddit

(Python) Stuck with adding a 0 check on second input via /r/learnprogramming

(Python) Stuck with adding a 0 check on second input

So i'm coding a calculator for class and i am on the division operator, this is what I have so far:

Paste bin of code:

Basically I need to check the inputs to make sure they are digits but then I need to check the second input(x2) to make sure its not 0 and print ("Input for x2 is invalid").

Thanks in advance for the help!

Submitted October 31, 2016 at 06:48PM by Pxhtrik
via reddit

[Circuits] I could use some help with the gated SR latch via /r/learnprogramming

[Circuits] I could use some help with the gated SR latch

Hey guys I have this question that I've been working on. I have to give the output of Q for each part of the question. I've guessed at questions a, c, and d. Also it looks to me as if questions a and d are the exact same.

As to where I get stuck this happens when it comes to the diagonal crossing at the end near the Q and NOT Q I have trouble seeing where I get the 2 inputs for the ORs at the end. This is how I've drawn the first question (a) to trace R, E, and S. Any help would be appreciated. Thanks guys.

Submitted October 31, 2016 at 06:58PM by BitOfAnAssembler
via reddit

[HTML][jscript] How to use buttons to input numbers via /r/learnprogramming

[HTML][jscript] How to use buttons to input numbers

Beginner here, how would I use 9 buttons to input a number that can have 1 to 3 digits and display it? For example, a question could be how many books have you read and then the user would have to click on the buttons. If they click on 5, it would show 5 but if they click 6 after, it would show 56.

Submitted October 31, 2016 at 07:01PM by WII_ED
via reddit

Next steps for learning about data structures? via /r/learnprogramming

Next steps for learning about data structures?

So I learned some data structures in my Intro to CS class in college, and now I think CS is super interesting. I've only learned the basics though, like binary trees/linked lists/ so what are some good sources to get a super solid foundation in data structures?

Submitted October 31, 2016 at 07:05PM by CrazedParade
via reddit