How to avoid my processing become really slow after one images array???

edited January 2018 in Library Questions

import ddf.minim.*;

Minim minim;

AudioInput in;

PImage[] img = new PImage[4];

PImage[] blue = new PImage[31];

PVector player;

PVector velocity;

PVector leftHand;

float gravity = 0.1;

float r=100;

float force;

PImage body;

PImage zuo;

PImage zuoshou;

PImage you;

PImage youshou;

int n = 15;

PVector enemy[] = new PVector[n];

boolean show[] = new boolean[n];

boolean blink[] = new boolean[n];

int times[] = new int[n];

int count[] = new int[n];

boolean start = false;

boolean change = true;

boolean judge(int k) {

//int i=0;

for (int i=0; i<k; i++) {

if (dist(enemy[k].x, enemy[k].y, enemy[i].x, enemy[i].y) < 100) {

  return true;
}

} return false; }

void setup() {

size(1000, 1000);

frameRate(24);

velocity = new PVector(0, 0);

player = new PVector(width/2, height/2);

for (int i = 0; i < n; i ++) {

enemy[i] = new PVector(random(0, width), random(200, height/2));

while ((dist(width/2, height/2, enemy[i].x, enemy[i].y) < 300)||judge(i)) {

  enemy[i] = new PVector(random(0, width), random(200, height/2));

}

show[i] = true;

blink[i] = false;

times[i] = 0;

count[i] = 0;

}

minim = new Minim(this);

in = minim.getLineIn();

imageMode(CENTER);

body=loadImage("main.png");

//left arm

zuo=loadImage("zuo.png");

zuoshou=loadImage("zuoshou.png");

//right arm

you=loadImage("you.png");

youshou=loadImage("youshou.png");

//MY IMAGE ARRAY

for(int i=0;i<blue.length;i++){

String imageName = nf(i,3)+".png";

blue[i] = loadImage(imageName);

}

for (int i=0; i<4; i++) {

img[i] = loadImage(i%4+".png");

}

}

void draw() {

background(255);

if (getLevel() > 0.01) {

start = true;

}

force = map(getLevel(), 0, 0.1, 0, 0.2);

force = constrain(force, 0, 0.2);

println(force, player.x, player.y, velocity.x, velocity.y);

r = map(getLevel(), 0, 0.1, 0.3, 0.5);

r = constrain(r, 0.3, 0.5);

for (int i = 0; i < n; i ++) {

if (blink[i]) {

  if (frameCount % 30 < 15)

    fill(255, 209, 238);

  else fill(255);

  if (frameCount%30 == 29) {

    times[i] ++;

  }

  if (times[i]>20) {

    blink[i] = false;

    times[i]=0;

  }

} else {

  fill(255, 209, 238);

}

if (show[i]) {

  image(img[i%4], enemy[i].x, enemy[i].y, height/20, height/20);

  println(i%4+".png"+i);

}else{

  pushMatrix();

  fill(0);

  //MY IMAGE ARRAY HERE> 

  count[i]++;

  if(count[i] >= blue.length)

    count[i]--;

  image(blue[count[i]],enemy[i].x, enemy[i].y,100,150);

  popMatrix();

}

}

drawPlayer();

}

void drawPlayer() {

if (abs(gravity)<0.0005) {

velocity.y = 0;

} //wald check edges

if (player.y <= r*body.height/2) {

velocity.y *= -1;

}

if (player.y>=height-body.height*r) {

if (force < 0.01) {

  velocity.y *=-1;

}

if (change) {

  velocity.x = random(-8, 8);

  change = false;

}

}

if (player.x >= width-rbody.width/2 || player.x <= rbody.width/2) {

velocity.x *= -1;

}

player.add(velocity);

if (start) {

velocity.y += (gravity-force);

velocity.y -= 0.03*velocity.y/abs(velocity.y);

}

player.y = constrain(player.y, rbody.height/2, height-body.heightr);

player.x = constrain(player.x, rbody.width/2, width-rbody.width/2);

//pictures image(body, player.x, player.y, 369r, 721r);

float angle0 = atan2(mouseY-player.x, mouseX-player.y);

float leftHandx=0;

float leftHandy=90;

pushMatrix();

translate(player.x-40, player.y-50);

rotate(angle0);

image(zuo, 0, 50, 85r, 306r);

image(zuoshou, 0, 90, 180r, 180r);

leftHand = new PVector (leftHandx, leftHandy);

leftHand.x=leftHandx;

leftHand.y=leftHandy;

for (int i = 0; i < n; i ++) {

if ((dist(player.x, player.y-50, enemy[i].x, enemy[i].y) < r*body.width/2) && show[i]) {

  velocity.x *= -1;

  velocity.y *= -1;

  blink[i] = true;

  times[i] = 0;

  println(i);

}

}

for (int i = 0; i < n; i ++) {

if ((dist(player.x-40+90*sin(-angle0), player.y+40+90*cos(-angle0), enemy[i].x, enemy[i].y) < 100) && show[i]) {

  velocity.x *= -1;

  velocity.y *= -1;

  show[i] = false;  

  println("get"+i);

}

}

ellipse(leftHand.x, leftHand.y, 180r, 180r);

popMatrix();

float angle1 = atan2(mouseY-player.x, mouseY-player.y);

pushMatrix();

translate(player.x+40, player.y-50);

rotate(angle1);

image(you, 0, 50, 85r, 306r);

image(youshou, 0, 90, 180r, 180r);

popMatrix(); }

float getLevel() {

float t = 0;

for (int i = 0; i < in.bufferSize() - 1; i++) {

t += abs(in.mix.get(i));

}

t /= in.bufferSize();

return t;

}

Hi,everyone,above are my coding part.Im kinda new with processing.I suppose it is because I loaded images in draw(),so it would load an image 60 times every second.But i don't know how to tackle that . I would be really appreciate it if any of you guys help me out of it.Thanks!;)

Tagged:

Answers

  • sorry Im new here apologize about bad reading experience

  • You can fix the reading experience by editing your post and properly formatting your code using the code button. Better yet, please post a MCVE that demonstrates the problem instead of your whole project.

  • Edit post, highlight code, press ctrl-o to format

    The obvious thing is all the image resizing that you are doing. Either resize the images using a graphics program or do it immediately after the load in setup, don't do it in draw because it's expensive and slow.

    But we don't have your images so I'm guessing.

Sign In or Register to comment.