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

2014/7/26

getOneObjectAtOffset

davemib123 davemib123

2014/7/26

#
Hi, i'm trying to move my Hero back or forward depending on the direction he comes from when he collides with a particular object. I have got this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public void act()
   {
       Actor HeroLeft = getOneObjectAtOffset (8, 0, Hero.class);
       Actor HeroRight = getOneObjectAtOffset (-8, 0, Hero.class);
       if (HeroLeft != null)
       {
           HeroLeft.setLocation (HeroLeft.getX() + 1, HeroLeft.getY());
           return;
       }
       if (HeroRight != null)
       {
           HeroRight.setLocation (HeroRight.getX() - 1, HeroRight.getY());
           return;
       }
   }
But i'm finding that when the hero is moved back or forward it seems to far of a move. How do I stop the hero rom moving to far back / forward? I have added this to the trees class to test. My scenario is located here: http://www.greenfoot.org/scenarios/11862
erdelf erdelf

2014/7/26

#
In the provided scenario i dont recognize the problem you described. But basically if the value in the objectAtOffset is the half size of the object pic added to the half size of the hero it should work without any problems.
danpost danpost

2014/7/26

#
erdelf wrote...
In the provided scenario i dont recognize the problem you described.
The same for me. You only have left and right (or horizontal) collisions accounted for in your code (so far), which seems to be working ok. So, if you mean forward/backward as up/down (or vertically) then that is why.
davemib123 davemib123

2014/7/26

#
danpost wrote...
You only have left and right (or horizontal) collisions accounted for in your code (so far), which seems to be working ok.
yea sorry, that is what I meant. left and right collisions, when the hero collides with the tile he moves to far back instead of just staying next to the tile.
danpost danpost

2014/7/26

#
You can decrease the 8's in lines 3 and 4 to have the hero stay closer.
davemib123 davemib123

2014/7/27

#
I found a better solution, it is similar to how you have done it in your MapWorld SuperClass, where the collision is checked by Me.class.
davemib123 davemib123

2014/7/27

#
Is there a shorter / more efficient way of writing this:
1
2
3
4
5
6
7
8
9
10
11
12
13
if (getOneObjectAtOffset(-2, -8, Buildings.class) != null ||
        getOneObjectAtOffset(-4, -8, Buildings.class) != null ||
        getOneObjectAtOffset(-6, -8, Buildings.class) != null ||
        getOneObjectAtOffset(-8, -8, Buildings.class) != null ||
        getOneObjectAtOffset(0, -8, Buildings.class) != null ||
        getOneObjectAtOffset(2, -8, Buildings.class) != null ||
        getOneObjectAtOffset(4, -8, Buildings.class) != null ||
        getOneObjectAtOffset(6, -8, Buildings.class) != null ||
        getOneObjectAtOffset(8, -8, Buildings.class) != null)
        {
            stopWalking();
            setLocation (getX(), getY() + speed);
        }
davemib123 davemib123

2014/7/27

#
I'll try a loop and get back if I'm stuck
erdelf erdelf

2014/7/27

#
I think this should work
1
2
3
4
5
6
boolean stop = true;
for(int i = 8; i>=-8 && !stop;i-=2) {
   stop = getOneObjectAtOffset(i, -8, Buildings.class) != null; }
if(stop) {
   stopWalking();
   setLocation(getX(), getY() +speed); }
davmac davmac

2014/7/27

#
I think this should work
I don't think so. You have set 'stop' to true initially, so '!stop' is false and the loop will terminate immediately.
davemib123 davemib123

2014/7/27

#
great thanks for that. I had part of line 2 and 3 figured before you posted, but the solution works great :)
davemib123 davemib123

2014/7/27

#
when adding more than 1 class i.e. trees.class, fences.class. would I need to make separate loops? or can they be declared within the same loop. Each class would have a different Y offset due to how the image is. I've tried adding this:
1
2
stop = getOneObjectAtOffset(i, -8, Buildings.class) != null;
stop = getOneObjectAtOffset(i, -6, Trees.class) != null;
but that simply reads the last statement.
danpost danpost

2014/7/27

#
You could do something like this:
1
2
3
boolean stop1 = getOneObjectAtOffset(i, -8, Buildings.class) != null;
boolean stop2 = getOneObjectAtOffset(i, -6, Trees.class) != null;
boolean stop = stop1 || stop2;
or change line 2 above to
1
stop = stop || getOneObjectAtOffset(i, -6, Trees.class) != null;
or just combine both lines
1
2
3
stop =
    getOneObjectAtOffset(i, -8, Buildings.class) != null ||
    getOneObjectAtOffset(i, -6, Trees.class) != null;
davemib123 davemib123

2014/7/27

#
great. i used the last option, seems more understandable to me :)
You need to login to post a reply.