I'd like to know why there is a delay in function mousePressed.
I know there is a problem in my code especially in for loops,
and if there are two balls on each others the bottom one disappeared sometimes in first if I press on them.
And sometimes the top one disappeared first
int number=80;
int i;
ball [] balls =new ball[number];
void setup(){
size(1000,1000);
frameRate(60);
for(i=0 ;i<number;i++)
{
balls[i]=new ball(color(random(0,255),random(0,255),random(0,255)),random(30,970) , random(30,970),random(1.9,2));
}
}
void draw()
{
background(255,0,0);
for( i=number-1 ;i>=0;i--)
{
if (mousePressed == true) {
balls[i].disapeear();
}
balls[i].display();
balls[i].bouncing();
}
}
class ball
{
float speed,x,y;
color c;
float A=1;
float B =1;
ball(color colour ,float horiz,float vert,float s)
{
speed = s;
x = horiz;
y = vert;
c = colour;
}
void disapeear()
{
float L = sqrt((x-mouseX )*(x-mouseX))+((y-mouseY)*(y-mouseY));
if(L<15)
{
x=-100;
y=-100;
}
}
void bouncing()
{
x=x+(speed * A);
y=y+(speed * B);
if((x>width-30)||(x<0))
{
A =A * -1 ;
}
if((y>height-30)||(y<0))
{
B =B * -1 ;
}
}
void display()
{
fill(c);
stroke(0,0,255);
ellipse(x,y,30,30);
}
}
When I was looking at your code, the one thing you don't have is a mousePressed() function.
If you add that function to the main class, that might stop the delay.
Related
I have tried many methods, and can't seem to grasp the idea of extracting an index from my array of strings to help me generate my desired number of building with a desired height, please help, here is my example
edit: Hi, i saw your feedback and posted my code below, hopefully it helps with the idea overall, as much as it is just creating rects, its more complicated as i need to involve arrays and string splitting along with loops. i more or less got that covered but i as i said above, i cannot extract the values from my array of string and create my facades at my own desired number and height
String buffer = " ";
String bh = "9,4,6,8,12,2";
int[] b = int(split(bh, ","));
int buildingheight = b.length;
void setup () {
size(1200, 800);
background(0);
}
void draw () {
}
void Textbox() {
textSize(30);
text(buffer, 5, height-10);
}
void keyTyped() {
if (key == BACKSPACE) {
if (buffer.length() > 0) {
buffer = buffer.substring(0, buffer.length() - 1);
}
} else if (key == ENTER) {
background(0);
stroke(0);
GenerateFacade();
println(buffer);
}
else {
buffer = buffer + key;
Textbox();
}
}
void GenerateFacade() {
fill(128);
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b.length; j++) {
if (int(b[j]) > buildingheight) {
buildingheight = int(b[j]);
}
}
rect(i*width/b.length, height - (int(b[i])*height/buildingheight), width/b.length, int(b[i])*height/buildingheight);
}
}
For the next time it would be great if you provide us with some code so we know what you tried and maybe can point you to the problem you have.
You need just the keyPressed function and some variables
int x = 0;
int buildingWidth = 50;
int buildingHeight = height / 6;
void setup(){
size(1000, 500);
background(255);
}
void draw(){
}
void keyPressed(){
if(key >= '0' && key <= '9'){
int numberPressed = key - '0' ;
fill(0);
rect(x, height - buildingHeight * numberPressed,
buildingWidth, buildingHeight * numberPressed);
x += buildingWidth;
}
}
This is my result
I try to make a web application.
You can change cells of the array by pressing arrow keys here.
There is a class "Module" with methods display() and update(). These methods change the inner array data[].
class Module {
int i; // index
int x; // coordinate
int y; // coordinate
int[] data = new int[]{0,0,0,0,0};
// Contructor
Module(int x){
this.x = x;
}
void update() {
data[i]=_mas_stor;
}
void display(){
text(data[i], x, 100);
}
}
But how to set the initial value of the array _mass[] at the beginning of the program?
The whole program here.
There is no need of an array of data in the class Module. It is sufficient that each object has it single data member. Wirte a constructor, eher you can pass to the initial data (Module(int x, int d)):
class Module {
int i;
int x;
int y;
int data;
// Contructor
Module(int x, int d){
this.x = x;
this.data = d;
}
void update() {
data=_mas[global_i];
}
void display(){
textSize(30);
text(data, x, 100);
}
}
Now the object can be initialized in a loop with ease:
int[] _mas ={1,2,3,4,5};
int global_i = 0;
Module [] mods;
void setup() {
size(500, 400);
mods = new Module[5];
for (int i = 0; i < 5; ++ i ) {
mods[i] = new Module(i*50+50, _mas[i]);
}
}
Further you have to ensure that global_i doesn't go out of bounds in keyPressed:
void keyPressed() {
if (keyCode == UP) {
_mas[global_i]++;
}
if (keyCode == DOWN) {
_mas[global_i]--;
}
if (keyCode == LEFT) {
global_i--;
if (global_i < 0)
global_i = 4;
}
if (keyCode == RIGHT) {
global_i++;
if (global_i > 4)
global_i = 0;
}
}
Note, you can further improve you program, if you skip the global variable _mas and add a increment method (inc) and decrement method (dec) to the class Module, instead of the update method:
int global_i = 0;
Module [] mods;
void setup() {
size(500, 400);
mods = new Module[5];
for (int i = 0; i < 5; ++ i ) {
mods[i] = new Module(i*50+50, i);
}
}
void draw() {
background(50);
for (int i = 0; i < 5; ++ i ) {
mods[i].display();
}
}
void keyPressed() {
if (keyCode == UP) {
println("up");
mods[global_i].inc();
}
if (keyCode == DOWN) {
mods[global_i].dec();
}
if (keyCode == LEFT) {
global_i--;
if (global_i < 0)
global_i = 4;
}
if (keyCode == RIGHT) {
global_i++;
if (global_i > 4)
global_i = 0;
}
}
class Module {
int i;
int x;
int y;
int data;
// Contructor
Module(int x, int d){
this.x = x;
this.data = d;
}
void inc() {
this.data ++;
}
void dec() {
this.data --;
}
void display(){
textSize(30);
text(data, x, 100);
}
}
You usually set the initial value of an array using a for loop. Something like this:
String myArray = new String[10];
for(int i = 0; i < myArray.length; i++){
myArray[i] = "hello world";
}
What you put inside the for loop depends on what values you want your array to start with.
I am trying to make a simple top down shooter. When the user presses W, A, S or D a 'bullet' (rectangle) will come out of the 'shooter'. With my code, you can only shoot one bullet per direction until it reaches the end of the screen. Is there a way to make it so they (the user) can shoot multiple bullets in one direction?
Here's my code:
package topdownshooter;
import processing.core.PApplet;
import processing.core.PImage;
public class TopDownShooter extends PApplet {
PImage shooter;
float shooterX = 400;
float shooterY = 300;
float u_bulletSpeed;
float l_bulletSpeed;
float d_bulletSpeed;
float r_bulletSpeed;
boolean shootUp = false;
boolean shootLeft = false;
boolean shootDown = false;
boolean shootRight = false;
public static void main(String[] args) {
PApplet.main("topdownshooter.TopDownShooter");
}
public void setup() {
shooter = loadImage("shooter.png");
}
public void settings() {
size(800, 600);
}
public void keyPressed() {
if(key == 'w') {
shootUp = true;
}
if(key == 'a') {
shootLeft = true;
}
if(key == 's') {
shootDown = true;
}
if(key == 'd') {
shootRight = true;
}
}
public void draw() {
background(206);
imageMode(CENTER);
image(shooter, shooterX, shooterY);
if(shootUp == true) {
rect(shooterX, shooterY-u_bulletSpeed, 5, 5);
u_bulletSpeed += 2;
if(u_bulletSpeed > 300) {
u_bulletSpeed = 0;
shootUp = false;
}
}
if(shootLeft == true) {
rect(shooterX-l_bulletSpeed, shooterY, 5, 5);
l_bulletSpeed += 2;
if(l_bulletSpeed > 400) {
l_bulletSpeed = 0;
shootLeft = false;
}
}
if(shootDown == true) {
rect(shooterX, shooterY+d_bulletSpeed, 5, 5);
d_bulletSpeed += 2;
if(d_bulletSpeed > 300) {
d_bulletSpeed = 0;
shootDown = false;
}
}
if(shootRight == true) {
rect(shooterX+r_bulletSpeed, shooterY, 5, 5);
r_bulletSpeed += 2;
if(r_bulletSpeed > 400) {
r_bulletSpeed = 0;
shootRight = false;
}
}
}
}
The language is processing and I am using the eclipse IDE.
Thanks!
Here's what I would do if I were you. First I'd encapsulate your bullet data into a class, like this:
class Bullet{
float x;
float y;
float xSpeed;
float ySpeed;
// you probably want a constructor here
void drawBullet(){
// bullet drawing code
}
}
Then I'd create an ArrayList that holds Bullet instances:
ArrayList<Bullet> bullets = new ArrayList<Bullet>();
To add a bullet, I'd create a new instance and add it to the ArrayList like this:
bullets.add(new Bullet(bulletX, bulletY));
Then to draw the bullets, I'd iterate over the ArrayList and call the corresponding function:
for(Bullet b : bullets){
b.drawBullet();
}
Shameless self-promotion:
Here is a tutorial on creating classes.
Here is a tutorial on using ArrayLists.
I am working on a Processing project, but I donĀ“t know how to restart the project once it is over. I have searched and found that the setup() method will make it. But it's not working. Can anyone help me. I would like the sketch to restart by itself once it is finished.
/* OpenProcessing Tweak of *#*http://www.openprocessing.org/sketch/59807*#* */
/* !do not delete the line above, required for linking your tweak if you upload again */
//
// outline: takes an image (image.jpg) and creates a sketch version
//
// procsilas (procsilas#hotmail.com / http://procsilas.net)
//
String iName="image.jpeg";
void setup() {
llegeixImatge("./"+iName);
size(img.width, img.height);
}
// parameters
// NO real control, so be careful
int NP=6000; // 1000 for line art, 10000 for complex images, O(N^2) so be patient!!!
int B=1; // try 2 or 3
float THR=28; // range 5-50
float MD=6; // range 0-10
int NMP=6; // range 1-15
float[][] punts;
color[] cpunts;
int [] usat;
int [] NmP=new int[NMP];
float [] NdmP=new float[NMP];
int inici=0;
PImage img;
void llegeixImatge(String s) {
img = loadImage(s);
img.loadPixels();
}
float fVar(int x, int y) {
// neighborhood 2B+1x2B+1 pixels
float m=0;
for (int k1=-B; k1<=B; k1++) {
for (int k2=-B; k2<=B; k2++) {
color c=img.pixels[(y+k1)*img.width+(x+k2)];
m+=brightness(c);
}
}
m/=float((2*B+1)*(2*B+1));
float v=0;
for (int k1=-B; k1<B; k1++) {
for (int k2=-B; k2<B; k2++) {
color c=img.pixels[(y+k1)*img.width+(x+k2)];
v+=(brightness(c)-m)*(brightness(c)-m);
}
}
v=sqrt(v)/(float) (2*B+1);
return v;
}
void creaPunts() {
punts = new float[NP][2];
cpunts = new color[NP];
usat = new int[NP];
int nint1=0;
int nint2=0;
for (int i=0; i<NP;) {
int x=B+int(random(width-2*B));
int y=B+int(random(height-2*B));
//println(i+" = "+x+", "+y+": "+THR+", "+MD);
// points need to be at least MD far from each other
int flag=0;
if (MD>0.0) {
for (int j=0; flag==0 && j<i; j++) {
if (dist(x, y, punts[j][0], punts[j][1])<MD) {
flag=1;
}
}
}
if (flag==0) {
nint1=0;
float f=fVar(x, y);
// use only "valid" points
if (f>=THR) {
nint2=0;
punts[i][0]=x;
punts[i][1]=y;
cpunts[i]=img.pixels[y*img.width+x];
usat[i]=0;
i++;
}
else {
nint2++;
if (nint2>=10) {
THR/=(1+1.0/float(NP-i));
MD/=(1+1.0/float(NP-i));
nint2=0;
}
}
}
else {
nint1++;
if (nint1>=10) {
MD/=2.0;
THR*=1.618;
nint1=0;
}
}
}
}
int NessimMesProper(int i) {
if (NMP<=1) {
int mP=-1;
float dmP=dist(0, 0, width, height);
for (int j=0; j<NP; j++) {
if (usat[j]==0) {
float jmP=dist(punts[i][0], punts[i][1], punts[j][0], punts[j][1]);
if (jmP<dmP) {
dmP=jmP;
mP=j;
}
}
}
return mP;
}
else {
for (int j=0; j<NMP; j++) {
NmP[j]=-1;
NdmP[j]=dist(0, 0, width, height);
}
for (int j=0; j<NP; j++) {
if (usat[j]==0) {
float jmP=dist(punts[i][0], punts[i][1], punts[j][0], punts[j][1]);
int k=NMP;
while(k>0 && NdmP[k-1]>jmP) {
k--;
}
if (k<NMP) {
for (int l=0; l<(NMP-k)-1; l++) {
NmP[(NMP-1)-l]=NmP[(NMP-1)-(l+1)];
NdmP[(NMP-1)-l]=NdmP[(NMP-1)-(l+1)];
}
NmP[k]=j;
NdmP[k]=jmP;
}
}
}
return NmP[NMP-1];
}
}
int fase=0;
void draw() {
if (fase==0) {
creaPunts();
background(#FFFFFF);
fase=1;
}
else {
if (inici!=-1) {
stroke(#000000);
usat[inici]=1;
int seguent=NessimMesProper(inici);
if (seguent!=-1) {
line(punts[inici][0], punts[inici][1], punts[seguent][0], punts[seguent][1]);
}
inici=seguent;
}
else {
//save("outline_"+iName);
}
}
}
You should not call setup() yourself.
Step 1: Encapsulate the state of your program in a set of variables.
Step 2: Use those variables to draw your sketch.
Step 3: Modify those variables to change what's being drawn.
Step 4: Simply reset those variables to their initial values when you want to reset the sketch.
Here's an example program that stores its state (the positions the user has clicked) in an ArrayList. It uses that ArrayList to draw the sketch, and new points are added whenever the user clicks. When the user types a key, the sketch is reset by clearing out the ArrayList:
ArrayList<PVector> points = new ArrayList<PVector>();
void setup(){
size(500, 500);
}
void draw(){
background(0);
for(PVector p : points){
ellipse(p.x, p.y, 20, 20);
}
}
void mousePressed(){
points.add(new PVector(mouseX, mouseY));
}
void keyPressed(){
points.clear();
}
I'm using controlP5 processing GUI, and I need counter inside draw() function.
I know that draw() function run continuously and every for loop is shown in its last step.
I need to see every step. I have two inputs, and I can input only once, because of draw(). I need draw() function to wait for input or something like that.
import controlP5.*;
Textarea myTextareaMI;
Textarea myTextareaVI;
Textarea my;
String textValueBODOVI = "";
String textValueZVANJE = "";
boolean mi=false, vi=false;
int i=1;
ControlP5 cp5;
int myColor = color(255);
int c1, c2;
float n, n1;
void setup() {
size(320, 480);
noStroke();
PFont font = createFont("arial", 20);
cp5 = new ControlP5(this);
cp5.addButton("NOVA PARTIJA")
.setValue(0)
.setPosition(0, 0)
.setSize(480, 19)
;
cp5.addButton("PONISTI ZADNJI UNOS")
.setValue(100)
.setPosition(0, 20)
.setSize(480, 19)
;
cp5.addBang("MI")
.setPosition(60, 80)
.setSize(60, 20)
.getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER)
;
cp5.addBang("VI")
.setPosition(123, 80)
.setSize(60, 20)
.getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER)
;
cp5.addTextfield("BODOVI")
.setPosition(60, 41)
.setSize(60, 20)
.setFont(font)
.setColor(color(255, 0, 0))
;
cp5.addTextfield("ZVANJE")
.setPosition(123, 41)
.setSize(60, 20)
.setFont(font)
.setColor(color(255, 0, 0))
;
;
int l=0;
for (int i=1;i<11;i++,l=l+22) {
my = cp5.addTextarea("MIVI"+i).setPosition(60, 101+l).setSize(123, 20).setFont(createFont("arial", 14))
.setLineHeight(14)
.setColor(color(128))
.setColorBackground(color(255, 100))
.setColorForeground(color(255, 100))
;
}
}
public void bang() {
}
void draw() {
delay(15);
background(myColor);
myColor = lerpColor(c1, c2, n);
n += (1-n)* 0.1;
funkcija();
}
void funkcija(){
int suma= 0;
for( i=1;i<=11;i++){
int brojmi=0;
textValueZVANJE = cp5.get(Textfield.class, "ZVANJE").getText();
textValueBODOVI = cp5.get(Textfield.class, "BODOVI").getText();
int bodovi = int(textValueBODOVI);
int zvanje = int(textValueZVANJE);
int mii, vii;
if(bodovi>0){
if (mi) {
println("mi"+brojmi+i);
int ukupno = 162 + zvanje;
vii = ukupno - bodovi;
cp5.get(Textarea.class, "MIVI"+i).setText(textValueBODOVI+" "+vii);
mi=false;
vi=false;
cp5.get(Textfield.class, "BODOVI").clear();
cp5.get(Textfield.class, "ZVANJE").clear();
loop();
}
if (vi) {
println("vi"+i);
int ukupno = 162 + zvanje;
mii = ukupno - bodovi;
cp5.get(Textarea.class, "MIVI"+i).setText(mii+" "+textValueBODOVI);
mi=false;
vi=false;
cp5.get(Textfield.class, "BODOVI").clear();
cp5.get(Textfield.class, "ZVANJE").clear();
}
}
brojmi++; }
}
public void controlEvent(ControlEvent theEvent) {
mi = theEvent.getController().getName().equals("MI");
vi = theEvent.getController().getName().equals("VI");
}
public void clear() {
cp5.get(Textfield.class, "BODOVI").clear();
cp5.get(Textfield.class, "ZVANJE").clear();
}
Mmmmm, I don't know if I got your question.
If your question is: "i need draw() function to wait for input or something like that.", then use a flag (boolean value) to decide if draw or not. When user input something, assign true to that value.
So your code should be:
void draw () {
// put the code you want to run anyways here...
// code
// code
if (hasUserInput) {
// here write the code you should wait to execute
}
}