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

2011/5/27

Setting paths for the actor to follow and switching between them

1
2
moiraxlaband moiraxlaband

2011/5/27

#
Hi! I'm making a game where the character needs to climb up a tree with objects falling down on both sides of the tree. I want to set it so that the character will have to follow two paths up the tree (switch between both by using the -> and <- keys) in order to dodge falling objects. How do I set the paths? Also, how do I make it so that by using the left and right arrow keys, the actor can switch from path to path? thanks!
danpost danpost

2011/5/27

#
Set up a boolean World variable -- maybe 'isPathA'. Then 'true' would signify one path and 'false' would signify the other. All you have to do then is set the value 'true/false' if 'left/right' arrow key is pressed. In the 'public void act()' of the climbing actor, setImage and setLocation by that variable. If you need more specific instruction, re-post, and be specific.
danpost danpost

2011/5/27

#
The following code in 'JungleWorld' (or whatever you named your world) is the basic idea.
    public boolean isPathA = true;

    public void act()
    {
        String myKey = Greenfoot.getKey();
        if (myKey == "left" ) 
        {   
            isPathA = true;
        }
        if (myKey == "right")
        {
            isPathA = false;
        }
    }
Then in Actor class you should have something like this:
    // LeftX and RightX are determined by a central X point ( center-point of trunk of tree), plus or minus a constant (half the width of the tree plus half the width of the character)
    public void act()
    {
        int myHeight = getY(); // How far climber climbed
        if (JungleWorld.isPathA)
        {
            setImage("LeftImage.gif");
            setLocation(LeftX, myHeight);
        }
        else
        {
            setImage("RightImage.gif");
            setLocation(RightX, myHeight);
        }
    }
moiraxlaband moiraxlaband

2011/5/31

#
Thanks, I tried this, however the following message pops up when I compiled it: int myHeight = getY(); // How far Jack climbed if (Village.isPathA) { setImage("LeftImage.gif"); setLocation(LeftX, myHeight); } else { setImage("RightImage.gif"); setLocation(RightX, myHeight); } } non-static variable isPathA cannot be referenced from a static context. ^ | | | how do i fix this?
davmac davmac

2011/5/31

#
Replace "Village.isPathA" with "((Village) getWorld()).isPathA". This:
  • gets the world (the type is World)
  • tells the compiler that we know it is actually an instance of Village
  • accesses the isPathA field from the Village instance
danpost danpost

2011/6/1

#
Thanks davmac. I am fairly new to Java programming myself, and I have been getting that kind of error message quite a bit. Although I am quite familiar with programming, self-teaching does have its dis-advantages. Other languages I have worked in, in the past are (1) The Basic language and (2) Assembly language, which also was self-taught. Your assistance was quite appreciated here, in that I now have a better understanding of the workings of Java. But still, I have written code similar to that above, and it works fine. In my ChessWorld program, I have public static variables whose values are passed on to the Actor classes with 'ChessWorld.varName'. I even pass arrayed variables to Actor classes from the World class with similar code. Any ideas as to why moiraxlaband needed to change that line as you stated? Never mind!! All moiraxlaband has to do is add the word 'static' when declaring 'isPathA'. If I am incorrect about this, please let me know. Thanks
davmac davmac

2011/6/1

#
Never mind!! All moiraxlaband has to do is add the word 'static' when declaring 'isPathA'. If I am incorrect about this, please let me know. Thanks
That's correct, however, using static variables is sometimes frowned upon from a design point of view, and in Greenfoot it adds a complication because static variables aren't reset when the "reset" button is clicked (for technical reasons). So it's usually best to avoid them.
moiraxlaband moiraxlaband

2011/6/2

#
Thanks for the replies! I have replaced "Village.isPathA" with "((Village) getWorld().isPathA)". However, once I compile it, the following message pops up: cannot find symbol - variable isPathA when the code is: int myHeight = getY(); // How far Jack climbed if ((Village) getWorld().isPathA) { setImage("Jack.image2"); setLocation(LeftX, myHeight); } else { setImage("Jack.image3"); setLocation(RightX, myHeight); } Where do I declare this method (is this the problem?)? How can I fix this? Or should I just try out the static thing, how would that look like?
davmac davmac

2011/6/2

#
I have replaced "Village.isPathA" with "((Village) getWorld().isPathA)"
You should have replaced it with "(((Village) getWorld()).isPathA)". You're missing a pair of parentheses!
moiraxlaband moiraxlaband

2011/6/2

#
thanks that worked. but then for the following part, it says: missing return statement. Im very bad at noticing the tiny flaws. How can I fix this? public boolean isPathA = true; /** * */ public boolean isPathA() { String myKey = Greenfoot.getKey(); if (myKey == "left" ) { isPathA = true; } if (myKey == "right") { isPathA = false; } } }
danpost danpost

2011/6/3

#
moiraxlaband, The isPathA in the first line is a variable declaration statement in your Jungle class. It tells the compiler that each instance of Jungle will have a boolean variable (that holds a true or false value) named isPathA; isPathA is not the name of a method that returns a boolean value. The statements within your public boolean isPathA() method in your last post should be in your act() method for Jungle. That way, when a key changes the value from true to false, or vice versa your climber's act() method can adjust the image and location of the climber, depending on isPathA's value. If you had more that one climber in your Jungle, then you would have to put that first line in your Climber class and add a method 'public void setPath(String myKey)' and call it from Jungle to set the value; which is probably the way it should have been set up to begin with (that way you can expand on the scenario without having to change everything). Hope all the input from davmac and myself is/was helpful. davmac, thanks again; though, I do not think it would matter much which side of the tree the climber started on.
davmac davmac

2011/6/3

#
moiraxlaband, just to clarify why you got the error:
    public boolean isPathA()
    {
        ...
... here you declare your method "isPathA" to return a boolean value. However, there are no "return" statements in the method, so it's not possible for the compiler to tell what value was supposed to be returned. Either: a) you didn't mean to return a value from the method, so you should have said "void" instead of "boolean" b) you meant to return a value, probably "isPathA" (the variable), so you should add "return isPathA;" at the end of the method. As danpost noted, it's a bit confusing that you have both a variable and a method named "isPathA", and it's probably simplest to remove the "isPathA()" method and put the contents in the act() method instead.
moiraxlaband moiraxlaband

2011/6/3

#
Hi! Just to clarify my problem, here is what the code for act() for my actor: public void act() { MouseInfo mouse = Greenfoot.getMouseInfo(); setLocation(getX(),getY()-1); hitObject(); int myHeight = getY(); // How far Jack climbed if (((Village) getWorld()).isPathA) { setImage("Jack.image2"); setLocation(300, 0); } else { setImage("Jack.image3"); setLocation(250, 0); } } and here is the code for the method public void isPathA() in the world: public void isPathA = true; /** * */ public void isPathA() { String myKey = Greenfoot.getKey(); if (myKey == "left" ) { isPathA = true; } if (myKey == "right") { isPathA = false; } } Is there something I'm doing wrong? Please help! I can never seem to get it to work!
davmac davmac

2011/6/3

#
Like I said in my previous post:
Either: a) you didn't mean to return a value from the method, so you should have said "void" instead of "boolean" b) you meant to return a value, probably "isPathA" (the variable), so you should add "return isPathA;" at the end of the method. As danpost noted, it's a bit confusing that you have both a variable and a method named "isPathA", and it's probably simplest to remove the "isPathA()" method and put the contents in the act() method instead.
If there's something in that you don't understand, ask a specific question. Davin
moiraxlaband moiraxlaband

2011/6/3

#
yes, I did change "boolean" to "void", however it still doesn't work. Also, where do I add "return isPathA"? in the actor or the Village world? And at what point of the method do I insert this in? Thanks, and sorry for the many questions :/
There are more replies on the next page.
1
2