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

2015/2/16

Specify Entities from an Actor

1
2
FlamingWaters FlamingWaters

2015/2/16

#
I have a program where you can pick up rocks and throw them at your opponents. To pick them up, you press and hold "A". When you release "A", the rock will launch right. In the Rock class:
private void checkIfThrown()
{
      if (Character.shot == true)
     {
            if (getOneIntersectingObject(Character.class) == true)

     }
}
FlamingWaters FlamingWaters

2015/2/16

#
Hold on, updating my post. I've accidentally posted while editing my code.
FlamingWaters FlamingWaters

2015/2/16

#
Updated Post: I have a program where you can pick up rocks and throw them at your opponents. To pick them up, you press and hold "A". When you release "A", the rock will launch right. In the "Rock" class
    private void checkIfThrown()
    {
        if (Jellie.shootWall == true)
        {
            if (getOneIntersectingObject(Jellie.class) !=null
            {
                continuallyMove = true;
            }
        }
        if (continuallyMove == true)
        {
            move(2);
        }
        if (isAtEdge() == true)
        {
            continuallyMove = false;
            getWorld().removeObject(this);
        }
    }

In the "Character" class
 
        if (Greenfoot.isKeyDown("a"))
        {
            shootWall = true;
        }
This worked.. partially. When I went up to a rock and pressed "A", all of the rocks started moving right. How do I specify a single entity if I have many other entities of the same actor?
danpost danpost

2015/2/16

#
The boolean field 'shootWall' does not specify any Rock object in particular. Have the Character object 'tell' a Rock object to move, instead. I am a bit confused about your 'shootWall' field(s). Is it in your Character class or your Jellie class -- or both? You use 'Jellie.shootWall' in the Rock class and 'shootWall = true' in what you say is your Character class. Anyway best approach is to have a Rock field in the Character class called 'heldRock' with a boolean field to track the state of the "a" key. If the key was last up and found to be down, get any intersecting Rock and assign it to the 'heldRock' field. If the key was last down AND found to be up AND the 'heldRock' field contains a Rock object (is not null), set 'continuallyMove' to true for that rock and reset the 'heldRock' field back to null. You could rename 'continuallyMove' to 'thrown' making the code more readable. I would remove the 'checkIfThrown' method from the Rock class and just use this for the act method:
public void act()
{
    if (thrown)
    {
        move(2);
        if (isAtEdge()) getWorld().removeObject(this);
    }
}
and add the following method to the Rock class:
public void setThrown()
{
    thrown = true;
}
so the Character object can set a Rock object to move
FlamingWaters FlamingWaters

2015/2/16

#
To clarify, the Character = Jellie, and Wall = Rock. The shootWall method is in Jellie class. Jellie
        if (Greenfoot.isKeyDown("a"))
        {
            shootWall = true;
        }
So, then the rock checks if 'shootWall' is true. Rock
FlamingWaters FlamingWaters

2015/2/16

#
To clarify, the Character = Jellie, and Wall = Rock. The shootWall method is in Jellie class. Jellie
        if (Greenfoot.isKeyDown("a"))
        {
            shootWall = true;
        }
So, then the rock checks if 'shootWall' is true. Rock
    if (Jellie.shootWall == true)
    {
        if (getOneIntersectingObject(Jellie.class) !=null
        {
            thrown = true;
        }
    }
    if (thrown)
    {
              move(2);
              if (isAtEdge()) getWorld().removeObject(this);
    }
However, I'm not sure how to convert a rock to heldRock. I assume it's a command. But what does does the command do?
FlamingWaters FlamingWaters

2015/2/16

#
I'm sorry about my messy posts, I should reread them more carefully.
danpost danpost

2015/2/16

#
FlamingWaters wrote...
I'm not sure how to convert a rock to heldRock. I assume it's a command. But what does does the command do?
It is not a command -- and there is no converting. 'heldRock' would be a field that holds a value. Your fields might look like this:
private boolean aDown = false;
private Rock heldRock = null;
Just like the boolean field 'aDown' can hold a true or false value, the Rock field 'heldRock' can have a null value (meaning it does not contain a reference to any object) or a Rock object (rather, a reference to a specific Rock object). An object reference field can be considered a pointer, in that it can point to an object of the given type and, if it is not pointing to any object, it returns a null value (said to contain a null value). So, you can assign a Rock object or a null value to it:
if ( !aDown && Greenfoot.isKeyDown("a"))
{
    aDown = true;
    heldRock = (Rock)getOneIntersectingObject(Rock.class);
}
would "pick up" any currently intersecting Rock object.
danpost danpost

2015/2/16

#
FlamingWaters wrote...
The shootWall method is in Jellie class.
        if (Greenfoot.isKeyDown("a"))
        {
            shootWall = true;
        }
'shootWall' is NOT a method -- it is a field (or variable). The only methods I have specifically seen so far in this discussion thread are the 'checkIfThrown' method you began with and the 'act' and 'setThrown' methods I supplied.
FlamingWaters FlamingWaters

2015/2/16

#
For future reference, I would like to try "decipher" the code. In Jellie class? if (not holding anything, and the player presses "A") { I am holding something All rocks I am touching, are held rocks. } So now, we have assigned a title to seperate held rocks to not held rocks. How do I tell all the held rocks to move? Also, I believe I'm missing a big amount of the Greenfoot tutorials. I went over to the "About" tab, and checked out the simple tutorials. I glossed over the complex tutorials too. Is there any sort of website or book I'm missing? Sorry for all the trouble I'm causing you.
danpost danpost

2015/2/16

#
FlamingWaters wrote...
In Jellie class? if (not holding anything, and the player presses "A") { I am holding something All rocks I am touching, are held rocks. }
No. if (last state of "A" key was not down and "A" key is now down) { set last state of "A" key to down set held rock to any one intersecting rock } Use 'heldRock.setThrown();' to throw the held rock. Then use 'heldRock = null;' to clear the field. The conditions for throwing a rock would be (1) last state of "A" key was down (2) "A" key is now up and (3) 'heldRock' is not null. The 'aDown' field should be reset back to false when a rock is thrown also.
FlamingWaters FlamingWaters

2015/2/16

#
.
sofa sofa

2015/2/16

#
hello
FlamingWaters FlamingWaters

2015/2/16

#
I've been fiddling on it for a while, but can't get the exact code. if (aDown && !Greenfoot.isKeyDown("a") && heldRock != null) { heldRock = null; aDown = true; heldRock.setThrown(); } I understand what the code is doing. As you have been trying to tell me the past two hours, aDown is the previous state of the key "A". You can specify classes using: private CLASS TITLE and tell the title to do something using title.command Thank you so much for your patience!
danpost danpost

2015/2/16

#
You need to throw the rock before resetting the field back to null.
There are more replies on the next page.
1
2