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

2016/5/30

help with getOneIntersectingObject

Meh Meh

2016/5/30

#
1
2
3
4
5
6
7
8
if(actor1IsUp() & direction == "left" & !getWorld().getObjects(Actor1.class).isEmpty())
        {
            Actor1 actor1 = (Actor1)getOneIntersectingObject(Actor1.class);
            if(actor1 != null)
            {
                actor1.setLocation(actor1.getX() - 1, actor1.getY());
            }
        }
So this is my code in Actor2 (Actor2 moves automatically), I want Actor1 to move when he is on Actor2. some Informations: - Actor1 is in the world - the direction of Actor2 is "left" - the method "actor1IsUp()" is true my problem: Actor1 is not moving my question: When does an object intersect with another? (cuz it seems like Actor1 doesn't intersect with Actor2, but Actor1 is actually touching Actor2) Or is there a different problem?
danpost danpost

2016/5/30

#
Meh wrote...
When does an object intersect with another? (cuz it seems like Actor1 doesn't intersect with Actor2, but Actor1 is actually touching Actor2) Or is there a different problem?
Yes, there is a different problem. Let me explain it this way. If a person gave birth to identical twins, would they (the twins) be the same person? I guess that is quite obvious. Let us go back to the direction of the Actor2 object. Is the String object referenced by 'direction' the same object as the hard-coded "left" in the condition in line 1? Of course not. Even though they both contain the same character sequence and look identical, they are still different objects. To compare the objects for being alike and not necessarily the same, you can use the 'equals' method of the String class. This comparison would look like this:
1
if ("left".equals(direction))
Meh Meh

2016/5/31

#
I don't get it. direction is a variable, which changes if Actor2 is at the min or max X coordinate (also a variable), so that shouldn't be a problem. In Actor2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public void move()
    {
        if(direction == "right")
        {
            move(1);
            checkObjects();
            counter++;
            if(counter > width)
            {
                direction = "left";
                counter = 0;
            }
        }
        else if(direction == "left")
        {
            move(-1);
            checkObjects();
            counter++;
            if(counter > width)
            {
                direction = "right";
                counter = 0;
            }
        }
        counter2 = 0;
    }
I know width is a bad name for this variable :) btw: your code didn't work, but my recent code works somehow if Actor2 is touching the middle of the Image of Actor1
danpost danpost

2016/5/31

#
Your 'move' method is doing the same thing -- trying to compare two distinctly different objects:
1
2
3
4
5
// change line 3 to
if ("right".equals(direction))
 
// and change line 14 to
else if ("left".equals(direction))
Meh Meh

2016/5/31

#
I have a few questions: - In which cases is it possible to use direction == "right"? - Is it the same problem with int variables for example: "v == 5"? - I used direction == "left" in other classes and also in this class, I never got an error and everything was fine. Why? - you can code v == v2, they are also different objects right? - And why does my code work, if Actor 1 is touching the middle of the image of Actor2?
danpost danpost

2016/5/31

#
You only need to use 'equals' when comparing objects. You can continue to compare primitive types ('int', 'double', 'boolean', 'long', 'short', 'byte' and 'char') using '=='; in fact, you cannot call a method on a primitive type anyway. You can also use '==' to compare objects; but, only when you are sure that the known object is the same object you are hoping to compare it to. Using a literal String like ' "left" ' is equivalent to creating a new object like ' new String("left") '. If you are creating a new object to compare to an already existing object, then they cannot be the same object and you should then use the 'equals' method. Two different fields, 'v' and 'v2', used as object references may or may not contain a reference to the same object (and both could possibly contain no reference -- or have 'null' values). So, you should probably do something like this when comparing two different fields for referring to the same object:
1
if (v != null && v == v2)
There are occasions where you might have a class field hold one object of many objects created from the class and then all objects, when running the 'act' method on it, would check to see if it is the special (or selected) object of the class using something like:
1
if (selected == this)
so it can behave differently than the others when selected.
Meh Meh

2016/5/31

#
Ok, I don't really get it, but I managed it to work with my code above, Actor1 just falls deeper, so it's touching the middle of Actor2.
danpost danpost

2016/5/31

#
Now, from your initial post, you should have something like this:
1
2
3
4
5
6
7
8
if (actor1IsUp() && "left".equals(direction))
{
    Actor actor1 = getOneIntersectingObject(Actor1.class);
    if (actor1 != null)
    {
        actor1.setLocation(actor1.getX()-1, actor1.getY());
    }
}
I removed the check to see if any Actor1 objects were in the world because it was not really needed. If there were none, then 'actor1' would end up 'null' and nothing would happen anyway. Also, there was no need to typecast the intersector to Actor1 as no fields or methods in that class were being used here -- only Actor class methods. Now, this should cause any intersecting Actor1 object to move left regardless of where intersecting. The only other influential thing (that I see right now) is determined by the return value of 'actor1IsUp'. Maybe it has something to do with the deeper falling (so it's touching the middle of Actor2). But, it may be something like the code in the Actor1 class that is causing it.
Meh Meh

2016/5/31

#
ok thank you
You need to login to post a reply.