This site requires JavaScript, please enable it in your browser!
Greenfoot back
ddvink
ddvink wrote ...

2013/10/21

How to know which portal?

ddvink ddvink

2013/10/21

#
Dear, In my world I placed a couple of portals with a 'ourWorld' field. How can I get the ourWorld-value? I place a getter and setter in the portal class.
 public Portal(int ourworld){
        this.ourworld = ourworld;
    }

    public void setOurWorld(int ourworld){
        this.ourworld = ourworld;        
    }

    public int getOurWorld(){
        return ourworld;
    }
I want to know on which portal I am standing.
    public void checkForPortal(){
        Actor portal = getOneObjectAtOffset(10, 10, Portal.class);

        System.out.println( /* portat.getOuwWorld()        */);
        
        // Te lang op de portal geeft andere effecten
        // opvangen met getKey (indien mogelijk)
        if(portal != null && Greenfoot.isKeyDown("up")){
            if(getWorld() instanceof TrainingGround){
                Greenfoot.setWorld(new Questmap());                
            }
            if(getWorld() instanceof Questmap){
                Greenfoot.setWorld(new TrainingGround());                       
            }
        }

    }
Yours, Dennis
Gevater_Tod4711 Gevater_Tod4711

2013/10/21

#
You have a reference to the portal. So it should be easy. You could use a switch case clause like this:
   public void checkForPortal(){  
        Actor portal = getOneObjectAtOffset(10, 10, Portal.class); 
        if(portal != null && Greenfoot.isKeyDown("up")){  
         switch (portal.getOurWorld()) {
             case 1:
                 //portal 1;
                break;
             case 2:
                 portal 2;
                break;
         }

        //...
    }  
You just need to construct the portals with the numbers you want to use.
ddvink ddvink

2013/10/21

#
      Actor portal = getOneObjectAtOffset(10, 10, Portal.class);

        // Te lang op de portal geeft andere effecten
        // opvangen met getKey (indien mogelijk)
        if(portal != null && Greenfoot.isKeyDown("up")){
            switch(portal.getOurWorld()){
                case 1:
                    System.out.println("Nice");
                    break;
            }
        }
For one or another reason, it doesn't recognize the getOurWorld() method. You just need to construct the portals with the numbers you want to use. - I did.....
danpost danpost

2013/10/21

#
The field with the number is not in the Actor class, but in the Portal class. You need to typecast your reference.
Portal portal = (Portal) getOneObjectAtOffset(10, 10, Portal.class);
ddvink ddvink

2013/10/21

#
Thanx!!!!!!!!!!!!
You need to login to post a reply.