preload()
preload() runs once before setup() and pauses the sketch until every asset inside it (images, sounds, JSON, fonts) has finished loading. It's the right place for loadImage/loadSound/loadJSON calls, but it should only load things you actually need - here it's fetching an unrelated test JSON that adds a network dependency for no gameplay benefit.
function preload() {
// IMPORTANT: All loadSound calls are commented out for debugging.
// This sketch should display the start menu if p5.js is loading correctly.
// foodEatenSound = loadSound('244983_3260272-lq.ogg'); // Example: Replace with your uploaded filename
// gameOverSound = loadSound('469956_5136979-lq.ogg'); // Example: Replace with your uploaded filename
// Let's load a tiny, known-good JSON to check preload functionality
// This JSON is just {"hello": "world"}
// If this line fails, then your entire p5.js setup is broken.
dummyData = loadJSON('https://jsonplaceholder.typicode.com/todos/1');
// If using external images for characters, load them here
// e.g., characters[0].image = loadImage('assets/classic_snake.png');
}
Line-by-line explanation (2 lines)
dummyData = loadJSON('https://jsonplaceholder.typicode.com/todos/1');- Fetches a small JSON file from a public test API just to confirm that preload() and asynchronous loading are working correctly before the game starts - it's a debugging leftover, not part of gameplay
// foodEatenSound = loadSound('244983_3260272-lq.ogg');- Sound loading is commented out, so foodEatenSound stays undefined - this is why the .play() calls elsewhere never actually make noise