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

2016/2/25

Help with getting more than one of same actor's X and Y in code

Sammi Sammi

2016/2/25

#
Hello! I'm writing a code which is sort of like a variation of Doodle Jump, so I wrote this to create gravity and also make my actor jump when it touches a brick. (Y is the y position of the actor, v is an integer, g is 1, and t is 1. Timer is only to delay the action)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public void Gravity(){
        if(timer == 5){
            Y=Y+V*t;
            V=V+g*t;
            Actor brick = getWorld().getObjects(Brick.class).get(0);
            int By= brick.getY();
            int Bx= brick.getX();
            if( Y>=By){
                if(getX()<Bx+100 & getX()>Bx-100){int delta= Y-By;
                    Y= By- delta;
                    V=-V*5/6;}
            }
            setLocation(getX(), Y);
        }
    }
I actually copied the "Actor brick = getWorld().getObjects(Brick.class).get(0)" part from the internet, so I'm not exactly sure how it works...Anyway, the actor only sees one brick, so when I try to jump on to a second brick, it passes right through. So how do you make the actor see more than one brick? Thanks!
danpost danpost

2016/2/25

#
The code you copied from the internet will probably always return the same brick each time you execute it. The 'getObjects' method of the World class returns a list of all objects in the world of the given type. Your actor is not in any way used in determining what is put in that list. There are Actor class methods that returns lists of objects that either are colliding or are near the actor; and there are some that return just one, if any exists, colliding or near object. You should probably be using one of them. Look over the Actor class API to see what methods are available, what each does, and decide which one will best suit your needs.
Super_Hippo Super_Hippo

2016/2/25

#
1
getWorld().getObjects(Brick.class).get(0);
Look what it does. 'get(0)' gets the first element of the the list of all objects created from the 'Brick' class which are currently in the world (in which the doodler also is). To get all brick objects, you can use a for-each:
1
2
3
4
5
6
7
for (Object obj : getWorld().getObjects(Brick.class))
{
    Actor b = (Actor) obj;
    int bx = b.getX();
    int by = b.getY();
    //... and so on
}
But as danpost said, it is usually better to use an easy method which Greenfoot provides.
Sammi Sammi

2016/2/26

#
I did some great methods like "intercepts" or "getInterceptingObject" or "getOneInterceptingObject" but I have no idea how to use them in my code... I've never used these methods before( sorry I'm nearly an absolute beginner). Could you help explain how I could use them? Thank you both for your advice!
danpost danpost

2016/2/26

#
Well, the 'intersects' method checks to see if a given object intersects the actor -- but you are looking for any of a specific type. The 'getIntersectingObjects' method will return a list which contain any and all intersecting object of the given type. This might help to initially determine if any blocks are intersecting. The 'getOneIntersectingObject' method returns one intersecting object of the given type, or 'null' in none of the given type intersects the actor. This can be used to move your actor off each intersecting object individually. A 'while' loop can be used to enssue thee actor is moved off any and all intersecting blocks each act cycle.
Sammi Sammi

2016/2/28

#
Got it, thanks!!
You need to login to post a reply.