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

2020/12/31

Get variable from specific object an actor is touching

1
2
Gbasire Gbasire

2020/12/31

#
Hello, I've been trying to do a simple collision thing in my pokémon game, because I wanted to be able to walk "in front of trees" while touching them, and colliding with them only at a certain point, and not just colliding when the player is touching them. So I wrote a little code :
if(isTouching(Tree.class))
            {
                treeY = ((Tree)getWorld().getObjects
                (Tree.class).get(0)).getY();
                treeX = ((Tree)getWorld().getObjects
                (Tree.class).get(0)).getX();
                if(getY() - treeY < 50)
                {
                    setLocation(originalX, originalY);
                }
                if(getX() - treeX < 50)
                {
                    if(treeX - getX() < 50)
                    {
                        setLocation(originalX, originalY);
                    }
                }
            }
But the problem is, it only takes the variable from the first tree that was added into the world (as I added many), so it works perfectly for the first tree added, but not well for the other trees, as the treeX and treeY variable (just public int treeX and public int treeY) come from the same tree. I don't know how to make the actor get the variable from the specific tree he's touching. What can I do ?
Gbasire Gbasire

2020/12/31

#
(I haven't uploaded it on the website, the collision in the game is the previous one that is way more simple)
danpost danpost

2020/12/31

#
Gbasire wrote...
Hello, I've been trying to do a simple collision thing in my pokémon game, because I wanted to be able to walk "in front of trees" while touching them, and colliding with them only at a certain point, and not just colliding when the player is touching them.
Use getOneIntersectingObject(Tree.class) to acquire the colliding tree. Then get its x and y.
Gbasire Gbasire

2021/1/1

#
hello, so I've done this, but it doesn't seem to change, it still takes the value from the same tree
if(getOneIntersectingObject(Tree.class) != null)
            {
                treeY = ((Tree)getWorld().getObjects
                (Tree.class).get(0)).getY();
                treeX = ((Tree)getWorld().getObjects
                (Tree.class).get(0)).getX();
                if(getY() - treeY < 50)
                {
                    setLocation(originalX, originalY);
                }
                if(getX() - treeX < 50)
                {
                    if(treeX - getX() < 50)
                    {
                        setLocation(originalX, originalY);
                    }
                }
            }
danpost danpost

2021/1/1

#
danpost wrote...
Use getOneIntersectingObject(Tree.class) to acquire the colliding tree.
(after checking for collision with a tree)
Gbasire Gbasire

2021/1/1

#
oh sorry that's true. But I don't really know what to write instead.
danpost danpost

2021/1/1

#
Gbasire wrote...
oh sorry that's true. But I don't really know what to write instead.
if (isTouching(Tree.class))
{
    Tree tree = (Tree)getOneIntersectingObject(Tree.class);
    ...
Or, better might be:
Tree tree = (Tree)getOneIntersectingObject(Tree.class);
if (tree != null) ...
Gbasire Gbasire

2021/1/1

#
I wrote something, but I obviously knew it wasn't going to work, I don't think I have enough experience with greenfoot and java to know what to do
if(isTouching(Tree.class))
            {
                Tree tree = (Tree)getOneIntersectingObject(Tree.class);
                if(tree != null)
                {
                    treeY = ((Tree)getWorld().getObjects
                    (Tree.class).get(0)).getY();
                    treeX = ((Tree)getWorld().getObjects
                    (Tree.class).get(0)).getX();
                }
                if(getY() - treeY < 50)
                {
                    setLocation(originalX, originalY);
                }
                if(getX() - treeX < 50)
                {
                    if(treeX - getX() < 50)
                    {
                        setLocation(originalX, originalY);
                    }
                }
            }
danpost danpost

2021/1/1

#
treeX = tree.getX();
and similarly for treeY. Plus, later stuff needs to be within initial brackets after removing lines 1, 2 and 22.
danpost danpost

2021/1/1

#
You should have something like this, now:
Tree tree = (Tree)getOneIntersectingObject(Tree.class);
if (tree != null)
{
    if ( (getY()-tree.getY() < 50 && tree.getY()-getY() < 50) ||
         (getX()-tree.getX() < 50 && tree.getX()-getX() < 50) )
        setLocation(originalX, originalY);
}
Gbasire Gbasire

2021/1/1

#
I uploaded the game on the website and thought the code worked perfectly, but for an unknown reason the hitbox of Trees isn't correctly working when the player comes from the left. At some point, the player is able to walk in the trees. This doesn't happen if he comes from the right though. here is the code : (I slightly modified some things, but it doesn't change anything)
Tree tree = (Tree)getOneIntersectingObject(Tree.class);
        if (tree != null)
        {
            treeX = tree.getX();
            treeY = tree.getY();
            if((getY() - treeY < 50 && treeY - getY() < 50) ||
            (getX() - treeY < 42 && treeX - getX() < 42))
            {
                setLocation(originalX, originalY);
            }
        }
danpost danpost

2021/1/1

#
Gbasire wrote...
the hitbox of Trees isn't correctly working when the player comes from the left.
You have a treeY on line 7 that should be a treeX.
Gbasire Gbasire

2021/1/1

#
Thanks, it fixed the initial problem, but sadly created another one : now the player may get a tiny bit into the trees at certain points, it's really weird... The scenario is published on the website if you want to see.
danpost danpost

2021/1/1

#
Try replacing lines 6 and 7 with the following one line:
if (Math.abs(treeX-getX()) < 42 || Math.abs(treeY-getY()) < 50)
Gbasire Gbasire

2021/1/1

#
It doesn't seem to change anything
There are more replies on the next page.
1
2