# Push() and pop() function isn't working

**URL:** <https://discourse.processing.org/t/push-and-pop-function-isnt-working/12593>\
**Category:** Processing for Android\
**Created:** [July 8, 2019, 5:39pm UTC](https://discourse.processing.org/t/push-and-pop-function-isnt-working/12593 "2019-07-08T17:39:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![CubingProgrammer](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@CubingProgrammer](https://discourse.processing.org/u/CubingProgrammer)\
**Post date:** [July 8, 2019, 5:39pm UTC](https://discourse.processing.org/t/push-and-pop-function-isnt-working/12593/1 "2019-07-08T17:39:37Z")

</div>

I want to use push() and pop() function when i run the project it fails.  
What can i do?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 8, 2019, 5:48pm UTC](https://discourse.processing.org/t/push-and-pop-function-isnt-working/12593/2 "2019-07-08T17:48:39Z")

</div>

We can’t tell without seeing your code.

Can you post your code please or make a new sketch to demonstrate the problem?

---

<div class="post-metadata">

**Author:** ![CubingProgrammer](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@CubingProgrammer](https://discourse.processing.org/u/CubingProgrammer)\
**Post date:** [July 8, 2019, 6:04pm UTC](https://discourse.processing.org/t/push-and-pop-function-isnt-working/12593/3 "2019-07-08T18:04:23Z")

</div>

import peasy.\*;

PeasyCam cam;

float speed = 0.05;  
int dim = 3;  
Cubie[] cube = new Cubie[dim_dim_dim];

Move[] allMoves = new Move[] {  
new Move(0, 1, 0, 1),  
new Move(0, 1, 0, -1),  
new Move(0, -1, 0, 1),  
new Move(0, -1, 0, -1),  
new Move(1, 0, 0, 1),  
new Move(1, 0, 0, -1),  
new Move(-1, 0, 0, 1),  
new Move(-1, 0, 0, -1),  
new Move(0, 0, 1, 1),  
new Move(0, 0, 1, -1),  
new Move(0, 0, -1, 1),  
new Move(0, 0, -1, -1)  
};

ArrayList sequence = new ArrayList();  
int counter = 0;

boolean started = false;

Move currentMove;

void setup() {  
size(1000, 1000, P3D);  
//fullScreen(P3D);  
cam = new PeasyCam(this, 400);  
int index = 0;  
for (int x = -1; x \<= 1; x++) {  
for (int y = -1; y \<= 1; y++) {  
for (int z = -1; z \<= 1; z++) {  
PMatrix3D matrix = new PMatrix3D();  
matrix.translate(x, y, z);  
cube[index] = new Cubie(matrix, x, y, z);  
index++;  
}  
}  
}

for (int i = 0; i \< 25; i++) {  
int r = int(random(allMoves.length));  
Move m = allMoves[r];  
sequence.add(m);  
}

currentMove = sequence.get(counter);

for (int i = sequence.size()-1; i \>= 0; i–) {  
Move nextMove = sequence.get(i).copy();  
nextMove.reverse();  
sequence.add(nextMove);  
}

currentMove.start();  
}

void draw() {  
background(51);

cam.beginHUD();  
fill(255);  
textSize(32);  
text(counter, 100, 100);  
cam.endHUD();

rotateX(-0.5);  
rotateY(0.4);  
rotateZ(0.1);

currentMove.update();  
if (currentMove.finished()) {  
if (counter \< sequence.size()-1) {  
counter++;  
currentMove = sequence.get(counter);  
currentMove.start();  
}  
}

scale(50);  
for (int i = 0; i \< cube.length; i++) {  
**push();**  
if (abs(cube[i].z) \> 0 && cube[i].z == currentMove.z) {  
rotateZ(currentMove.angle);  
} else if (abs(cube[i].x) \> 0 && cube[i].x == currentMove.x) {  
rotateX(currentMove.angle);  
} else if (abs(cube[i].y) \> 0 && cube[i].y ==currentMove.y) {  
rotateY(-currentMove.angle);  
}  
cube[i].show();  
**pop();**  
}  
}

class Cubie {  
PMatrix3D matrix;  
int x = 0;  
int y = 0;  
int z = 0;  
color c;  
Face[] faces = new Face[6];

Cubie(PMatrix3D m, int x, int y, int z) {  
this.matrix = m;  
this.x = x;  
this.y = y;  
this.z = z;  
c = color(255);

```
faces[0] = new Face(new PVector(0, 0, -1), color(0, 0, 255));
faces[1] = new Face(new PVector(0, 0, 1), color(0, 255, 0));
faces[2] = new Face(new PVector(0, 1, 0), color(255, 255, 255));
faces[3] = new Face(new PVector(0, -1, 0), color(255, 255, 0));
faces[4] = new Face(new PVector(1, 0, 0), color(255, 150, 0));
faces[5] = new Face(new PVector(-1, 0, 0), color(255, 0, 0));

```

}

void turnFacesZ(int dir) {  
for (Face f : faces) {  
f.turnZ(dir\*HALF\_PI);  
}  
}

void turnFacesY(int dir) {  
for (Face f : faces) {  
f.turnY(dir\*HALF\_PI);  
}  
}

```
void turnFacesX(int dir) {
for (Face f : faces) {
  f.turnX(dir*HALF_PI); 
}

```

}

void update(int x, int y, int z) {  
matrix.reset();  
matrix.translate(x, y, z);  
this.x = x;  
this.y = y;  
this.z = z;  
}

void show() {  
//fill©;  
noFill();  
stroke(0);  
strokeWeight(0.1);  
pushMatrix();  
applyMatrix(matrix);  
box(1);  
for (Face f : faces) {  
f.show();  
}  
popMatrix();  
}  
}

class Face {  
PVector normal;  
color c;

Face(PVector normal, color c) {  
this.normal = normal;  
this.c = c;  
}

void turnZ(float angle) {  
PVector v2 = new PVector();  
v2.x = round(normal.x \* cos(angle) - normal.y \* sin(angle));  
v2.y = round(normal.x \* sin(angle) + normal.y \* cos(angle));  
v2.z = round(normal.z);  
normal = v2;  
}

void turnY(float angle) {  
PVector v2 = new PVector();  
v2.x = round(normal.x \* cos(angle) - normal.z \* sin(angle));  
v2.z = round(normal.x \* sin(angle) + normal.z \* cos(angle));  
v2.y = round(normal.y);  
normal = v2;  
}

void turnX(float angle) {  
PVector v2 = new PVector();  
v2.y = round(normal.y \* cos(angle) - normal.z \* sin(angle));  
v2.z = round(normal.y \* sin(angle) + normal.z \* cos(angle));  
v2.x = round(normal.x);  
normal = v2;  
}

void show() {  
pushMatrix();  
fill©;  
noStroke();  
rectMode(CENTER);  
translate(0.5_normal.x, 0.5_normal.y, 0.5\*normal.z);  
if (abs(normal.x) \> 0) {  
rotateY(HALF\_PI);  
} else if (abs(normal.y) \> 0) {  
rotateX(HALF\_PI);  
}  
rect(0, 0, 1, 1);  
popMatrix();  
}  
}

class Move {  
float angle = 0;  
int x = 0;  
int y = 0;  
int z = 0;  
int dir;  
boolean animating = false;  
boolean finished = false;

Move(int x, int y, int z, int dir) {  
this.x = x;  
this.y = y;  
this.z = z;  
this.dir = dir;  
}

Move copy() {  
return new Move(x, y, z, dir);  
}

void reverse() {  
dir \*= -1;  
}

void start() {  
animating = true;  
finished = false;  
this.angle = 0;  
}

boolean finished() {  
return finished;  
}

void update() {  
if (animating) {  
angle += dir \* speed;  
if (abs(angle) \> HALF\_PI) {  
angle = 0;  
animating = false;  
finished = true;  
if (abs(z) \> 0) {  
turnZ(z, dir);  
} else if (abs(x) \> 0) {  
turnX(x, dir);  
} else if (abs(y) \> 0) {  
turnY(y, dir);  
}  
}  
}  
}  
}

void keyPressed() {  
if (key == ’ ') {  
currentMove.start();  
counter = 0;  
}  
// applyMove(key);  
}

void applyMove(char move) {  
switch (move) {  
case ‘f’:  
turnZ(1, 1);  
break;  
case ‘F’:  
turnZ(1, -1);  
break;  
case ‘b’:  
turnZ(-1, 1);  
break;  
case ‘B’:  
turnZ(-1, -1);  
break;  
case ‘u’:  
turnY(1, 1);  
break;  
case ‘U’:  
turnY(1, -1);  
break;  
case ‘d’:  
turnY(-1, 1);  
break;  
case ‘D’:  
turnY(-1, -1);  
break;  
case ‘l’:  
turnX(-1, 1);  
break;  
case ‘L’:  
turnX(-1, -1);  
break;  
case ‘r’:  
turnX(1, 1);  
break;  
case ‘R’:  
turnX(1, -1);  
break;  
}  
}

void turnZ(int index, int dir) {  
for (int i = 0; i \< cube.length; i++) {  
Cubie qb = cube[i];  
if (qb.z == index) {  
PMatrix2D matrix = new PMatrix2D();  
matrix.rotate(dir\*HALF\_PI);  
matrix.translate(qb.x, qb.y);  
qb.update(round(matrix.m02), round(matrix.m12), round(qb.z));  
qb.turnFacesZ(dir);  
}  
}  
}

void turnY(int index, int dir) {  
for (int i = 0; i \< cube.length; i++) {  
Cubie qb = cube[i];  
if (qb.y == index) {  
PMatrix2D matrix = new PMatrix2D();  
matrix.rotate(dir\*HALF\_PI);  
matrix.translate(qb.x, qb.z);  
qb.update(round(matrix.m02), qb.y, round(matrix.m12));  
qb.turnFacesY(dir);  
}  
}  
}

void turnX(int index, int dir) {  
for (int i = 0; i \< cube.length; i++) {  
Cubie qb = cube[i];  
if (qb.x == index) {  
PMatrix2D matrix = new PMatrix2D();  
matrix.rotate(dir\*HALF\_PI);  
matrix.translate(qb.y, qb.z);  
qb.update(qb.x, round(matrix.m02), round(matrix.m12));  
qb.turnFacesX(dir);  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [July 9, 2019, 12:19am UTC](https://discourse.processing.org/t/push-and-pop-function-isnt-working/12593/4 "2019-07-09T00:19:21Z")

</div>

In non-p5.js versions of Processing, `push()` and `pop()` are called `pushMatrix()` and `popMatrix()`. You might also call `pushStyle()` and `popStyle()`.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [July 9, 2019, 1:19am UTC](https://discourse.processing.org/t/push-and-pop-function-isnt-working/12593/5 "2019-07-09T01:19:34Z")

</div>

> [@TfGuy44](#):
>
> In non-p5.js versions of Processing, `push()` and `pop()`

in newer versions ( current processing IDE 3.5.x )  
these functions exist and have same function as in p5.js

> **[push() / Reference](https://processing.org/reference/push_.html)**
>
> The push() function saves the current drawing style settings and transformations, while pop() restores these settings. Note that these functions are always used together. They allow you …

as a combination of  
push = pushMatrix + pushStyle
