How do i initiate a PVector[]?

How do i initiate a PVector[]?

int maxnpcnumber;
PVector[] npc = new PVector[maxnpcnumber];

now in a normal PVector i would say in setup,

npc = new PVector(15, 26);

i need the 1d array equivilent because the PVector[] does not initiate properly.

Answers

  • edited December 2013
    static final int NUM = 5;
    static final PVector[] npc = new PVector[NUM];
    

    Those lines above only instantiate the array structure, the outer shell we may call it.
    All of its 5 elements are still null! And its index goes from 0 up to NUM - 1; that is 4.
    And we use [] to access each 1 of those!

    Thus, if we got an array w/ NUM elements, we're yet to fill in those 5 vacant rooms:

    for ( int i = 0; i != NUM; npc[i++] = new PVector(random(width), random(height)) );
    

    Oh, and instead of NUM, you may also use npc.length! :D

    To know more about arrays, go to the link below:
    http://processing.org/reference/Array.html
    http://processing.org/reference/arrayaccess.html

  • edited December 2013

    I am getting Some interesting problems, The goal of this code is to make a image flollow a point, i got it to work, but then i put it in object oriented with arrays so i can make multiple. i cannot have it be final because i need to be able to change the values.

    PImage thing;
    PImage sprite;
    boolean wPressed = false;
    boolean sPressed = false;
    boolean aPressed = false;
    boolean dPressed = false;
    boolean escPressed = false;
    int dir;
    int dir2;
    float locx = 1;
    float locy = 1;
    
    
    void setup() {
      size(640, 480);
      thing = loadImage("thing.png");
      sprite = loadImage("sprite.png");
    }
    
    
    void draw() {
      background(0);
      sprite();
      addai();
    }
    
    void sprite() {
      dir2 = 0;
      if (wPressed == true) {
        image(sprite, locx, locy);
        locy -= 2;
      }
      if (sPressed == true) {
        image(sprite, locx, locy);
        locy += 2;
      }
      if (aPressed == true) {
        image(sprite, locx, locy);
        locx -= 2;
      }
      if (dPressed == true) {
        image(sprite, locx, locy);
        locx += 2;
      }
    
      if (wPressed == true && aPressed == true) {
        image(sprite, locx, locy);
      }
      if (wPressed == true && dPressed == true) {
        image(sprite, locx, locy);
      }
      if (sPressed == true && aPressed == true) {
        image(sprite, locx, locy);
      }
      if (sPressed == true && dPressed == true) {
        image(sprite, locx, locy);
      }
    }
    
    
    void keyPressed() {
      if (key=='w') {
        wPressed=true;
      }
      if (key=='s') {
        sPressed=true;
      }
      if (key=='a') {
        aPressed=true;
      }
      if (key=='d') {
        dPressed=true;
      }
      if (key==ESC) {
        key = 0;
        escPressed=true;
      }
    }
    
    
    void keyReleased() {
      if (key=='w') {
        wPressed=false;
      }
      if (key=='s') {
        sPressed=false;
      }
      if (key=='a') {
        aPressed=false;
      }
      if (key=='d') {
        dPressed=false;
      }
      if (key==ESC) {
        key = 0;
        escPressed=false;
      }
    }
    
        static final PVector character = new PVector(500, 500);
    PImage[] thenpc = new PImage[500];
    int[] level = new int[500];
    float[] npcx = new float[500];
    float[] npcy = new float[500];
    float[] npcmovex = new float[500];
    float[] npcmovey = new float[500];
    static final PVector[] npc = new PVector[500];
    static final PVector[] characternpc = new PVector[500];
    Ai[] ais = new Ai[500];
    
    
    void addai() {
      for (int i = 0; i < 500; i ++ ) {
        thenpc[i] = thing;
      }
    
      for (int i = 0; i < 500; i ++ ) {
        level[i] = 1;
      }
    
      for (int i = 0; i != 500; npc[i++] = new PVector(500, 500));
    
      for (int i = 0; i != 500; characternpc[i++] = new PVector(500, 500));
    
      for (int i = 0; i < 500; i ++ ) {
        ais[i] = new Ai(thenpc[i], level[i], npcx[i], npcy[i], npcmovex[i], npcmovey[i], npc[i],               characternpc[i]);
      }
    
      for (int i = 0; i < 500; i ++ ) {
        ais[i].crap();
        ais[i].move();
      }
    }
    
    
    class Ai {
      PImage thenpc;
      int level;
      float npcx;
      float npcy;
      float npcmovex;
      float npcmovey;
      PVector npc;
      PVector characternpc;
    
      Ai(PImage tn, int lvl, float nx, float ny, float nmx, float nmy, PVector n, PVector cn) {
        thenpc = tn;
        level = lvl;
        npcx = nx;
        npcmovex = nmx;
        npcmovey = nmy;
        npc = n;
        characternpc = cn;
      }  
    
    
      void move() {
        pushMatrix();
        translate(npcmovex, npcmovey);
        image(thenpc, 1, 1);
        popMatrix();
      }
    
    
      void crap() {
        character.x = (locx);
        character.y = (locy);
        npc.x = npcmovex;
        npc.y = npcmovey;
        PVector.sub(character, npc, characternpc);
        characternpc.normalize();
        characternpc.mult(1);
        npcmovex += characternpc.x;
        npcmovey += characternpc.y;
      }
    }
    
Sign In or Register to comment.