access Global variable from inner Class in android studio

edited March 2016 in Android Mode

Hi

When I use android in eclipse, I can easily use a global variable (a variable that I have simply declare on the upper part of the Main Class) in an inner class. I just add the name I gave to the parent class to the name of the variable : parent.myVariable

I try to do the same in android studio and I cannot get it to work

Any idea on why it is so ?

cheers

Tagged:

Answers

  • edited March 2016
    • Java doesn't have "global" variables. They're rather called fields. :-@
    • All members of some class can access everything of that class, including fields of course.
    • You can follow how things work in Processing's IDE (PDE) and use nested classes as well.
    • Or those top classes are gonna need to request the PApplet's reference in order to access those field members. :-B
  • I didn't understand a thing in your answer GoTo :]

    but ! I found the answer myself I did implement my inner class the way it is explained here and it helped my get the errors fixed as far as calling processing methods like rect() or dist()...

    In this tutorial, it was said to use PApplet parent; // The parent PApplet that we will render ourselves onto in the beginning of the class and then refer to parent (or any other name you want to call it) in the rest of the Class code to "tell a Class object about a MyProcessingSketch "

    Thing is, it didn't work for my global variables. But here is the Solution don't use

    PApplet parent;

    instead use

    MyProcessingSketch parent; (or whatever your main Class name is)

    So in the case of my processing android app, the main Class is called PhoebusGame_Main and my Dot Class goes like

    package gg.gamer.droid.phoebus.sketch;
    
    import processing.core.*;
    
    
    public class Dot {
        PhoebusGame_Main p5;
        ////////////////////////////////////////////////////////////class variables
        int x, y;
        PVector dotLoc;
        int col;
        float pSize;
    
        /////////////////////////////////////////////////////////////constructor
        Dot(PhoebusGame_Main p, int _x, int _y, float _pSize, int _col){
            p5 = p;
            //
            x = _x;
            y = _y;
            dotLoc = new PVector(x, y);
            pSize = _pSize;
            col = _col;
        }
    
    
        /////////////////////////////////////////////////////////////method
        void run() {
            p5.fill(col);
            p5.ellipse(x, y, pSize, pSize);
            isEaten();
        }
    
    
        PVector getLoc() {
            return dotLoc;
        }
    
        boolean isEaten() {
    
            if (p5.dist(x, y, p5.pacX, p5.pacY) < 10) {
                return true;
            } else {
                return false;
            }
        }
    }
    

    see how I use p5. to call things from my main ? :)

    cheers

Sign In or Register to comment.