/    Sign up×
Community /Pin to ProfileBookmark

Random x and y.

How do i make the small circles have a random x and y, but stay still?

https://editor.p5js.org/AlexArek/sketches/HkxcR322X

Code:
let playerX;
let playerY;
let foods = [];

function setup() {
createCanvas(600, 400);
food = new Food();
for (let i = 0; i < 10; i++)
foods[i]= new Food();

}

function draw() {
background(35);
playerShow();
for (let i = 0; i < foods.length; i++)
food.foodShow();
}

function playerShow() {
noStroke();
fill(50, 80, 200)
ellipseMode(CENTER);
ellipse(mouseX, mouseY, 35)
}

class Food {

foodShow() {
let foodX = random(0, 600);
let foodY = random(0, 400);
fill(255)
noStroke();
ellipse(foodX, foodY, 15)

}
}

to post a comment

7 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumNov 04.2018 — I recommend reading the manual:

https://p5js.org/examples/structure-setup-and-draw.html
The code inside the draw() function runs continuously from top to bottom until the program is stopped.[/quote]Therefore drawing the circles is repeated continuously and they are changing their positions as the positions are random.

You might change the name like this:
let playerX;
let playerY;
let foods = [];


function setup() {
createCanvas(600, 400);
food = new Food();
for (let i = 0; i &lt; 10; i++)
foods[i]= new Food();

playerShow();
<br/>
<br/>
}

function myDraw() {
background(35);
for (let i = 0; i &lt; foods.length; i++)
food.foodShow();
}

function playerShow() {
noStroke();
fill(50, 80, 200)
ellipseMode(CENTER);
ellipse(mouseX, mouseY, 35)
myDraw();
}

class Food {

foodShow() {
let foodX = random(0, 600);
let foodY = random(0, 400);
fill(255)
noStroke();
ellipse(foodX, foodY, 15)
<br/>
}
}
Copy linkTweet thisAlerts:
@SempervivumNov 04.2018 — PS: Just noticed that there is a solution more easy by using noLoop():
function setup() {
createCanvas(600, 400);
food = new Food();
for (let i = 0; i &lt; 10; i++)
foods[i]= new Food();
noLoop(); // Run once and stop <br/>
}
Copy linkTweet thisAlerts:
@alex_arekauthorNov 05.2018 — @Sempervivum#1597407 Thank you so much!
Copy linkTweet thisAlerts:
@GgosyNov 07.2018 — wonderful
Copy linkTweet thisAlerts:
@headgazNov 08.2018 — Look for a guide, I think it will help you.
Copy linkTweet thisAlerts:
@acupofteaNov 10.2018 — @Sempervivum#1597407 Thank you.
Copy linkTweet thisAlerts:
@hohohomeNov 12.2018 — It helped me too.
×

Success!

Help @alex_arek spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 3.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 10 SATS,
)...