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

2017/1/17

Understanding code i found..

Jochends Jochends

2017/1/17

#
Hi, I found some code, that i occasionally see that people use in their games.. . I tryed to google the code and find some information about it, but i just find any decent info about it. I'm new @ this and find it quite confusing but interesting.
    private int[] Monster1X = { 380 };
        for(int t=0; t<Monster1X.length; t++) { 
            addObject(new Monster1(), Monster1X[t], 347);
        }
                List<Monster1> Monsters1 = getObjects(Monster1.class);
                for( int t=0; t<Monsters1.size(); t++) {
                    Monster1 monster1 = Monsters1.get(t);
                    monster1.scroll();
                }
    private void CheckEdgeWorld(){
        World MyWorld;
        MyWorld = getWorld();
        Object obj= MyWorld.getObjects(mannetje.class).get(0);
        mannetje Mannetje =(mannetje) obj;
        intmannetjeX = Mannetje.getX();
        if(!getObjects(mannetje.class).isEmpty()){ // Als de wereld NIET leeg is, gaat het mannetje de getX aanroepen van het mannetje.
            Actor man = (Actor)getObjects(mannetje.class).get(0);
            manX = man.getX();
danpost danpost

2017/1/18

#
Jochends wrote...
I found some code, that i occasionally see that people use in their games.. . I tryed to google the code and find some information about it, but i just find any decent info about it. I'm new @ this and find it quite confusing but interesting.
Snippet 1: a declaration of an int array field given an array of length 1. Snippet 2: a 'for' loop that iterates through the int array. Snippet 3: a List object created on the first line is iterated through via a 'for' loop. Snippet 4: beginning of a method, getting the x-coordinate of an object in the world. Snippet 5: getting thee x-coordinate of an object, only if one is in the world. These are brief descriptions of what they do. For specific information, please be concise as to what you do not understand and do them one at a time, please.
Jochends Jochends

2017/1/20

#
Hi Danpost, About the code:
private void CheckEdgeWorld(){
    World MyWorld;
    MyWorld = getWorld();
    Object obj= MyWorld.getObjects(mannetje.class).get(0);
    mannetje Mannetje =(mannetje) obj;
    intmannetjeX = Mannetje.getX();
It tells the code to see on what X-coordinatie an object is at, is what it does exactly? The code seems so confusing by repeating itself consistently. I don't understand to what each word in it is referring to. 'World MyWorld' i know my world is called MyWorld but what does the World do in it? Also the same with 'Object obj' 'mannetje Mannetje' ... . Thanks in advance
danpost danpost

2017/1/20

#
To declare a variable (or field), you need two things -- the name of the field and the type of the field. The type goes first, then the name you give it. Everything except the first line in the code above can be written like this:
int mannetjeX = ((mannetje)getWorld().getObjects(mannetje.class).get(0)).getX();
// or this would do just as well
int mannetjeX = ((Actor)getWorld().getObjects(mannetje.class).get(0)).getX();
This page of the java tutorials discusses what you are asking about. Also, this page expands on the idea.
You need to login to post a reply.