# Accessing variables from PVector arraylist from draw()

**URL:** https://discourse.processing.org/t/accessing-variables-from-pvector-arraylist-from-draw/4179
**Category:** Coding Questions
**Created:** [October 6, 2018, 4:44am UTC](https://discourse.processing.org/t/accessing-variables-from-pvector-arraylist-from-draw/4179 "2018-10-06T04:44:30Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![LRyuugaHideki](https://avatars.discourse-cdn.com/v4/letter/l/91b2a8/32.png) [@LRyuugaHideki](https://discourse.processing.org/u/LRyuugaHideki)
#### Post date: [October 6, 2018, 4:44am UTC](https://discourse.processing.org/t/accessing-variables-from-pvector-arraylist-from-draw/4179/1 "2018-10-06T04:44:30Z")

</div>

I’m coding for a two-player game where each player has a colour which is changed when the beam shot out from their opponent touches them. i.e. When Red player’s beam hits blue player, the the tint of the blue player supposedly changes to purple and is slowly converted to red. When it reaches the full saturation of red (something like fill(255,0,0)), the game ends.

Currently I’m having trouble accessing the variables of the x positions of the beams shot from a PVector array list in order to code its contact with the other play in my draw method. I would greatly appreciate it if anyone can help me out! I currently have 3 tabs.

main tab

> [@](#):
>
> PImage buildings;  
> PImage monL;  
> PImage monR;  
> MonL mL;  
> MonR mR;
> 
> boolean gameOver;
> 
> void setup() {
> 
> size(1024, 768);  
> buildings = loadImage(“building.jpg”);  
> monL = loadImage(“monsterleft.png”);  
> mL = new MonL();
> 
> monR = loadImage(“monsterright.png”);  
> mR = new MonR();  
> }  
> void draw() {
> 
> background(0);  
> image(buildings, 0, 0);
> 
> mL.aniMonL();  
> mR.aniMonR();
> 
> if (keyPressed) {
> 
> ```
> //moving MonL
> if (key == 'w' || key == 'W') {
> mL.moveupMonL();
> }
> if (key == 's' || key == 'S') {
> mL.movedownMonL();
> } 
> if (key == 'a' || key == 'A') {
> mL.moveleftMonL();
> } 
> if (key == 'd' || key == 'D') {
> mL.moverightMonL();
> }
> 
> //shoot MonLbeam
> if (key == ' ') {
> mL.createMonLBeam();
> }
> 
> ```
> 
> }
> 
> //moving MonR  
> if (key == CODED) {  
> if (keyCode == UP) {  
> mR.moveupMonR();  
> }  
> if (keyCode == DOWN) {  
> mR.movedownMonR();  
> }  
> if (keyCode == LEFT) {  
> mR.moveleftMonR();  
> }  
> if (keyCode == RIGHT) {  
> mR.moverightMonR();  
> }  
> if (keyCode == CONTROL) {  
> mR.createMonRBeam();  
> }  
> }
> 
> mL.drawMonLbeam();  
> mR.drawMonRbeam();  
> mL.restrictMonL();  
> mR.restrictMonR();  
> }
> 
> //monsters interacting with beams  
> void contactMonLbeam() {  
> if ((PVector).get(monLp.x) \> mR.monR\_x) {  
> mR.MonRred = mR.MonRred + 10;  
> mR.MonRblue = mR.MonRblue - 10;  
> }  
> }
> 
> void contactMonRbeam() {  
> if ((PVector).get(monRp.x) \< mL.monL\_x) {  
> mL.MonLred = mL.MonLred - 10;  
> mL.MonLblue = mL.MonLblue + 10;  
> }  
> }
> 
> /\*void gameOver() {
> 
> gameOver = true;  
> if (mL.MonLred == 0 || mR.MonRblue == 0) {  
> reset();  
> }  
> }
> 
> void reset() {  
> size(1024, 768);  
> buildings = loadImage(“building.jpg”);  
> monL = loadImage(“monsterleft.png”);  
> mL = new MonL();
> 
> monR = loadImage(“monsterright.png”);  
> mR = new MonR();  
> }\*/

Left player:

> [@](#):
>
> class MonL {
> 
> PImage monLArray;  
> float monL\_x = 20;  
> float monL\_y = height/2;  
> int numberOfFrames = 50;  
> float monL\_s = 5;  
> float monL\_restrict = 5;
> 
> float mon\_bspeed = 3;
> 
> ArrayList monLBeams;  
> float MonLred=255;  
> float MonLblue=0;
> 
> MonL() {
> 
> ```
> monLBeams = new ArrayList<PVector> ();
> monLArray = new PImage[5];  
> tint(0, 0, 255);
> for (int i =0; i< 5; i++) {
> 
> monLArray[i] = monL.get(100*i, 10, 100, 100); 
> tint(255, 0, 0);
> image(monL, 0, 0);
> 
> }
> noTint();
> 
> ```
> 
> }
> 
> void aniMonL() {  
> frameRate(80);  
> tint(255, 0, 0);  
> image(monLArray[frameCount%numberOfFrames/10], monL\_x, monL\_y);  
> noTint();  
> }
> 
> //movingmonLsprite  
> void moveupMonL() {  
> monL\_y = monL\_y - monL\_s;  
> }
> 
> void movedownMonL() {  
> monL\_y = monL\_y + monL\_s;  
> }
> 
> void moveleftMonL() {  
> monL\_x = monL\_x - monL\_s;  
> }
> 
> void moverightMonL() {  
> monL\_x = monL\_x + monL\_s;  
> }
> 
> //restricting monL
> 
> void restrictMonL() {  
> if (monL\_y \< 0) {  
> monL\_y = monL\_y + monL\_restrict;  
> }
> 
> ```
> if (monL_y + 100 > 768) {
> monL_y = monL_y - monL_restrict;
> }
> 
> if (monL_x < 0) {
> monL_x = monL_x + monL_restrict;
> }
> if (monL_x + 100 > width) {
> monL_x = monL_x - monL_restrict;
> }
> 
> ```
> 
> }
> 
> void createMonLBeam(){  
> PVector Lbeam = new PVector(monL\_x + 50, monL\_y + 45);  
> monLBeams.add(Lbeam);  
> }
> 
> void drawMonLbeam(){  
> ArrayList Lbeams = new ArrayList();  
> for (PVector monLp: monLBeams){  
> fill(MonLred,0,MonLblue);  
> strokeWeight(0);  
> ellipse(monLp.x,monLp.y,20,10);  
> monLp.x = monLp.x + mon\_bspeed;  
> if(monLp.x\>width){  
> Lbeams.add(monLp);  
> }  
> }  
> for (PVector monLp: Lbeams){  
> monLBeams.remove(monLp);  
> }
> 
> }
> 
> }

Right player:

> [@](#):
>
> class MonR {
> 
> PImage monRArray;  
> float monR\_x = 904;  
> float monR\_y = height/2;  
> int numberOfFrames = 50;  
> float monR\_s = 5;  
> boolean upR, downR;  
> float monR\_restrict = 5;
> 
> float MonRred = 0;  
> float MonRblue = 255;
> 
> float mon\_bspeed = 3;
> 
> ArrayList monRBeams;
> 
> MonR() {
> 
> ```
> monRBeams = new ArrayList<PVector> ();
> monRArray = new PImage[5];  
> for (int i =0; i< 5; i++) {
> 
> monRArray[i] = monR.get(100*i, 10, 100, 100); 
> tint(0, 0, 255);
> image(monR, 0, 0);
> }
> noTint();
> 
> ```
> 
> }
> 
> void aniMonR() {  
> frameRate(80);  
> tint(0, 0, 255);  
> image(monRArray[frameCount%numberOfFrames/10], monR\_x, monR\_y);  
> noTint();  
> }
> 
> //movingmonRsprite  
> void moveupMonR() {  
> monR\_y = monR\_y - monR\_s;  
> }
> 
> void movedownMonR() {  
> monR\_y = monR\_y + monR\_s;  
> }
> 
> void moveleftMonR() {  
> monR\_x = monR\_x - monR\_s;  
> }
> 
> void moverightMonR() {  
> monR\_x = monR\_x + monR\_s;  
> }
> 
> //restricting monL
> 
> void restrictMonR() {  
> if (monR\_y \< 0) {  
> monR\_y = monR\_y + monR\_restrict;  
> }
> 
> ```
> if (monR_y + 100 > 768) {
> monR_y = monR_y - monR_restrict;
> }
> 
> if (monR_x < 0) {
> monR_x = monR_x + monR_restrict;
> }
> if (monR_x + 100 > width) {
> monR_x = monR_x - monR_restrict;
> }
> 
> ```
> 
> }
> 
> void createMonRBeam() {  
> PVector Rbeam = new PVector(monR\_x+50, monR\_y + 45);  
> monRBeams.add(Rbeam);  
> }  
> void drawMonRbeam() {  
> ArrayList Rbeams = new ArrayList();  
> for (PVector monRp : monRBeams) {  
> fill(MonRred, 0, MonRblue);  
> strokeWeight(0);  
> ellipse(monRp.x, monRp.y, 20, 10);  
> monRp.x = monRp.x - mon\_bspeed;  
> if (monRp.x\<0) {  
> Rbeams.add(monRp);  
> }  
> }  
> }  
> }

Thank you very much in advance!
