Invoking a function within a loop

In order to complete this assignment with a pop art theme, I have chosen to incorporate pokeballs into the design. The task at hand involves using two for loops, one for the top row and one for the bottom row, while also creating a function. The main challenge I am facing is figuring out how to randomly generate 2 additional pokeballs in the top row (for a total of 6 pokeballs: 3 on top and 3 on the bottom) using a function call.

var RANDCOL = random(0, 400);
var XBALL = 65;
var YBALL = 108;

//dividing tiles
fill(250, 0, 0);
rect(0, 0, 130, 200);
fill(0, 187, 255);
rect(131, 0, 137, 200);
fill(0, 0, 0);
rect(269, 0, 130, 200);
fill(167, 158, 235);
rect(0, 200, 131, 199);
fill(46, 184, 101);
rect(131, 200, 137, 199);
fill(255, 0, 0);
rect(269, 200, 130, 199);

//pokeball specifications
var pokeBall = function(x) {
    stroke(158, 214, 235);
    ellipse(XBALL, YBALL, 99, 97);
    fill(0, 0, 0);
    stroke(0, 0, 0);
    line(15, 109, 56, 109);
    line(116, 109, 72, 109);
    ellipse(64, 108, 19, 16);
};

//loop for top row
for(var i = 0; i < 3; i++) {
    var x = i * 134;
    fill(RANDCOL);
    pokeBall();
}

Answer №1

Initially, the given code will not execute as Processing functions are being used before the setup() function is invoked. It is recommended to provide a concise MCVE that can be easily copied and ran.

To achieve your objective, the pokeBall() function should accept arguments specifying the ball's position, and the ball should be drawn based on those coordinates. You are on the right track by utilizing the XBALL and YBALL variables, but they should be transformed into arguments and passed from the for loops as described.

For example, consider the following function:

function drawBall(){
   ellipse(100, 150, 20, 20);
}

This function consistently draws a circle at coordinates 100,150. To utilize arguments instead:

function drawBall(ballX, ballY){
   ellipse(ballX, ballY, 20, 20);
}

Subsequently, the function can be invoked with arguments to draw circles at different locations:

drawBall(100, 200);
drawBall(200, 300);
drawBall(60, 20);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Executing React Fetch API Twice upon loading the page

Double-fetching Issue with React Fetch API on Initial Page Load import React, { useState, useEffect } from 'react' import axios from 'axios'; import { Grid, Paper, TextField } from '@mui/material' import PersonOut ...

Is there a way to change the border property of an element when clicked, without causing the displacement of other elements?

I'm in the process of creating a webpage where users can choose the color and storage capacity of an item. Only one color/capacity can be selected at a time, and once chosen, it should be highlighted with a border. The issue I encountered is that whe ...

What is the most effective approach for managing exceptions at a global level in Node.js using Express 4?

Is there an Exception filter available in node.js with express 4, similar to the one in asp.net MVC? I have searched through various articles but haven't found a solution that meets my requirements. I also attempted the following in app.js: process ...

Error: Controller not found when angular.module is called from different files

As I am restructuring my code to make it more modular, I decided to move the controller into a separate file that belongs to the same module. However, I am facing an issue where the controller does not load, even though I have verified that the load order ...

How to remove a variable definition in Typescript

Is there a way to reset a variable to undefined after assigning it a value? To check, I am using the Underscore function _.isUndefined(). I have attempted both myVariable = undefined and delete myVariable without success. ...

Maximizing the potential of AFRAME animations: Tips for recycling your animations

Looking to create a unique and irregular animation, similar to the pattern of a waterdrop falling: Drip nothing Drip Drip Drip nothing nothing Is there a method to achieve this effect or loop an extended animation sequence of dripping? ...

Any ideas on how to resolve this ajaxToolkit issue?

Just for your reference, here's what I'm trying to achieve: https://i.stack.imgur.com/GYaNz.jpg Error 1: Unknown server tag 'ajaxToolkit:CalendarExtender'. <ajaxToolkit:CalendarExtender FirstDayOfWeek="Monday" PopupPosition="Botto ...

Align the image so that it is perfectly positioned along the lower edge of the window

I have been experimenting with parallax scrolling and resizing images using JavaScript. However, I am struggling to align my homepage perfectly with the edge of the window. The code I have doesn't line up with the bottom edge of the webpage. If I us ...

Enhance user experience by implementing an autocomplete feature for a text input field

Can you name the process in which a form is automatically filled using database information without needing to refresh the page? One common example of this technique is seen on platforms like Google or Facebook, where they predict friends you may be searc ...

Having trouble making class changes with ng-class

I am attempting to change the direction of the arrow in my checkbox by utilizing two classes with the assistance of ng-class. Unfortunately, it is not producing the desired outcome. Below is my code: Note: The angularJS CDN has already been incorporated. ...

Changing an AngularJS Protractor promise from a string to a decimal number - how to do it?

I am currently working with Angular.js Protractor to retrieve the values of cells in a grid. Although I can successfully retrieve these values, they are strings and I need to perform calculations with them. Upon attempting this: ptor.findElements(protrac ...

ASP updatepanel textbox focusing - Working only with breakpoints, not without them

I've encountered a bizarre problem where my code functions perfectly with a breakpoint set, but when I remove the breakpoint, certain parts of the code fail to work. My goal is to have a textbox automatically select all text upon focus; it should foc ...

What causes the return of undefined when accessing an item from an object?

In order to extract the item "Errors" from the data object provided below: {Id: 15, Date: "22-02-2019", Time: "22:45", Sport: "Football", Country: "United Kingdom", …} Bet: "Win" Bookie: "Bet365" Competition: "Premier League" Country: "U ...

What is the best way to modify the height of a div before fetching an image using Ajax, and then revert it back to its original height once the image has been

Using a form and Ajax, I'm dynamically pulling an image to replace another image in the .results div. Initially, when the image was removed and loaded, the page jumped up and down, so I tried adding a style attribute to set the div height and then rem ...

Converting objects into JSON format

I'm trying to transform an object that looks like this: { user[id]: 1, user[name]: 'Lorem', money: '15.00' } Into the following structure: { user: { id: 1, name: 'Lorem', }, money: ...

Guide on combining vendor CSS files in a React application using Webpack

Incorporating third-party libraries is an essential part of my project. For example, I have Mapbox GL installed via npm, which comes with CSS files needed for its functionality. The Mapbox GL CSS file can be found at mapbox-gl/dist/mapbox-gl.css in the no ...

AngularJS Accordion Component: A Handy Tool for Organ

I have been trying to implement an Accordion in my angular project by sending HTML structure from a controller. However, despite following the documentation closely, the Accordion is not displaying as expected. Here is a snippet of the code I am using: v ...

Load Angular scripts only when needed

I need to develop an application that can dynamically register new Angular Controllers obtained from a script. This application should load the minimum necessary scripts at startup and then fetch additional scripts as needed from other modules. Here' ...

Guide to flattening a nested array within a parent array using lodash

Within my array named sports, there is another array called leagues, but I need to flatten these arrays using _.flatten due to issues with Angular filters. If you look at the data, inside the first array which contains a "name":"Live Betting", there is an ...

Execute AngularJS $q.all, regardless of any errors that may occur

Is there a way to ensure $q.all is triggered even if the promises return errors? I'm working on a project where I need to make multiple $http.post requests, sending user-inputted values from text fields. The backend (Django REST framework) has a valu ...