r/processing • • Dec 17 '18

Collision Struggles

Hi. I have to finish my Schoolproject but ive got a problem. I want my character to be able to stand on a table ( so i do gravity = 0).... but it appears every time the same problem in the end of my program (void kollision)

"Cannot make a static reference to the non-static field Tisch.XPosition/Schueler.y/Tisch.YPosition

I would be very happy if anyone could give me some details why it isnt possible .. thanks a lot

final float gravity = 0.7; // Gravitation

boolean jumping = false;

float x_speed = 0;

float y_speed = 0;

//Objekte deklarieren

Schueler schueler1;

ArrayList<Tisch> tische = new ArrayList<Tisch>();

int TischlastTime = 0;

ArrayList<Aufgestuhlt> aufstuhlen = new ArrayList<Aufgestuhlt>();

int AufgestuhltlastTime = 0;

void setup(){

size(1024, 768); //, P2D //<-- Für Vollbildmodus

noCursor();

fill(255);

smooth();

//Objekte initialisieren

schueler1 = new Schueler(width/7, 330);

}

void draw(){

background(100);

create();

for(int i = tische.size()-1; i>= 0; i--){

Tisch tischx = tische.get(i);

tischx.entgegenkommen();

tischx.render();

if(tischx.XPosition>width){

tische.remove(i);

}

}

for(int i = aufstuhlen.size()-1; i>= 0; i--){

Aufgestuhlt gestuhltx = aufstuhlen.get(i);

gestuhltx.entgegenkommen();

if(gestuhltx.XPosition>width){

aufstuhlen.remove(i);

}

}

schueler1.display();

fill(0);

rect(0,740, 1024, 768);

}

void create(){

if((millis() - TischlastTime) > random(5000,20000)){

tische.add(new Tisch(1000,500,1,1));

TischlastTime = millis();

}

if((millis() - AufgestuhltlastTime) > random(10000,31000)){

aufstuhlen.add(new Aufgestuhlt(1000,500,1,1));

AufgestuhltlastTime = millis();

}

}

//Objekt-Prozeduren

//Plattform

//Tasteneingaben

void keyPressed(){

if(keyCode == CONTROL){

}

if(keyCode == LEFT){

}

if(keyCode == RIGHT){

}

if(keyCode == UP && !jumping){

y_speed = -21;

jumping = true;

}

if(keyCode == DOWN){

}

if(keyCode == 'X'){

}

}

class Schueler{

//Datenfelder

int x;

int y;

//Konstruktor

Schueler(int xpos, int ypos){

x = xpos;

y = ypos;

}

//Display-Prozedur

void display(){

fill(200,60,0);

rect(x,y,150,300);

noStroke();

y_speed += gravity; //Gravitation wird bestimmt

y += y_speed;

if (y >= height -330) { //Kollisionsabfrage mit dem Boden

y_speed *= 0; //Da es in diesem Moment nicht am Boden ist

jumping = false;

}

}

}

class Tisch{

//Datenfelder

float XPosition;

float YPosition;

float XSpeed;

float YSpeed;

//Konstruktor

Tisch(int x, int y, int xs, int ys){

XPosition = x;

YPosition = y;

XSpeed = xs;

YSpeed = ys;

}

//Entgegenkommen-Prozedur

void entgegenkommen(){

XPosition = XPosition - XSpeed;

render ();

}

//Tisch darstellen

void render() {

fill(130, 130, 130);

rect(XPosition, YPosition, 15, 400);

fill(130, 130, 130);

rect(XPosition + 210, YPosition, 15, 400);

fill(255, 255, 255);

rect(XPosition, YPosition, 225, 20); //Hier: Nur dieses Rechteck für Kolisionsabfrage!

}

}

class Aufgestuhlt extends Tisch{

//Konstruktor

Aufgestuhlt(int x, int y, int xs, int ys) {

//bezieht sich auf Variablen der Superklasse (Tisch)

super(x,y,xs,ys);

}

//gleiche Methoden bleiben erhalten (man muss nicts schrtieben)

//Bei Methoden, die von Vaterklasse abweichen, neu definieren

void render(){

fill(130, 130, 130);

rect(XPosition, YPosition, 15, 400);

fill(130, 130, 130);

rect(XPosition + 210, YPosition, 15, 400);

fill(255, 255, 255);

rect(XPosition, YPosition, 225, 20); //Hier: Nur dieses Rechteck für Kolisionsabfrage!

//AbHierStuhl

fill(180, 180, 180);

rect(XPosition + 45, YPosition - 150, 140,10);

rect(XPosition + 45, YPosition - 150, 10, 150);

rect(XPosition + 180, YPosition - 150, 10, 150);

rect(XPosition + 45, YPosition - 250, 145,100);

}

}

//PROBLEM!!!!

void kollision(){

if(width/7 == Tisch.XPosition && Schueler.y == Tisch.YPosition){

gravity = 0;

} else {

gravity = 0.7;

}

}

//if(width/7 ==Tisch.XPosition && Schueler.y < Tisch.YPosition){

// Gameover-Screen

0 Upvotes

2 comments sorted by

3

u/remy_porter Dec 17 '18

FIRST: See the link in the sidebar on How to FORMAT code.

Second: Tisch is the name of your class. You need an instance of your Tisch class, presumably from your tische array list.

2

u/forgotmyusernamedamm Dec 17 '18

See sidebar for formatting tips - you'll get more people willing to help if it's easier to read.
Ticsh is the name of your class, but you have a whole bunch of Ticsh objects. The error message is essentially saying "you're asking for a value, but you haven't told me which Ticsh object you want that value from".
You've already solved this problem in your draw with the for loops.

  for (int i = tische.size()-1; i>= 0; i--) {
    Tisch tischx = tische.get(i);
    tischx.entgegenkommen();  

This is going through your list arralyist of objects, putting each one, one at time into tichx, then running the entgegenkommen() routine that is on that object.
I would probably put the kollision() function inside the class along with entgegenkommen() and render() and then call it from within the for loop in the draw that you already have - this would be the easiest solution.
I'm not sure how you're envisioning the collision working, and I don't think this is going to work as you anticipate just yet, but at least this should solve that first error message.