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

2015/1/27

Problem

60sChallange 60sChallange

2015/1/27

#
How can l call one varible from one to other subclass ? and X and Y coordinate too? ( l mean how to get value of one variable from one to another edtior)
davmac davmac

2015/1/27

#
Documentation - tutorial #6
danpost danpost

2015/1/27

#
Be more specific. What exactly are you trying to do? How are the classes related? Show what you have tried.
60sChallange 60sChallange

2015/1/27

#
actor- "ws" subclass; public void act() { GreenfootImage image =getImage(); setLocation(getX() , getY() + WS_SPEED); and actor- "wb" subclass; public void act() { setLocation(getX()+ dy,getY()); } now l want to create if in "wb" subclass; if (l don't know how to call x and y coordinate "ws" > x,y coordinate from "wb") { dy = 4 } so can you write me a code that has to be between ()
60sChallange 60sChallange

2015/1/27

#
and there is one more question beacouse l am finishing a game.. can greenfoot game be posted on google play? and can it be used on android?
danpost danpost

2015/1/27

#
If there is always exactly one "ws" object in the world, you can use:
Actor actor = (Actor)getWorld().getObjects(ws.class).get(0);
then you can execute the 'getX' and 'getY' methods on 'actor'. The line that gets the reference to the ws object gets the first ('0') element from the list returned by 'getObjects' of ws class objects in the world. The list returns the objects as Object type objects and the compiler needs to be told that the element extracted from the list is indeed an Actor before it will look in the Actor class for the 'getX' and 'getY' methods (or before it can store the reference in a field that holds an Actor type object).
60sChallange 60sChallange

2015/1/31

#
but l need to put that in "if"between () ... how can l call x and y then?
danpost danpost

2015/1/31

#
60sChallange wrote...
but l need to put that in "if"between () ... how can l call x and y then?
I think you want something like this (after using my line above):
if (actor.getX() > getX() && actor.getY() > getY())
60sChallange 60sChallange

2015/1/31

#
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:635) at java.util.ArrayList.get(ArrayList.java:411) at WoodBall.act(WoodBall.java:43) (this line is marked red) at greenfoot.core.Simulation.actActor(Simulation.java:583) at greenfoot.core.Simulation.runOneLoop(Simulation.java:541) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) that appears in terminal window and when l click red marked line, editor opens with marked "Actor actor = (Actor)getWorld().getObjects(ws.class).get(0);"..... tnx for help danpost
60sChallange 60sChallange

2015/1/31

#
btw game starts just for second and stops
danpost danpost

2015/1/31

#
The error message is given because you do not always have a ws object in the world.
danpost wrote...
If there is always exactly one "ws" object in the world, you can use:
Actor actor = (Actor)getWorld().getObjects(ws.class).get(0);
Since you may not have one in the world, you need to ensure that the list returned by 'getObjects' is not empty:
if (! getWorld().getObjects(ws.class).isEmpty())
{
    Actor actor = (Actor)getWorld().getObjects(ws.class).get(0);
    if (actor.getX() > getX() && actor.getY() > getY())
    {
        /** whatever you do */
    }
}
60sChallange 60sChallange

2015/2/3

#
ty
You need to login to post a reply.