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

2015/6/25

help (invinity scrolling)

1
2
3
jaki jaki

2015/6/25

#
hi can anybody explane me who to do this. I watched a lot of demos but dont understand ow to implimantate this ;(
danpost danpost

2015/6/25

#
jaki wrote...
hi can anybody explane me who to do this. I watched a lot of demos but dont understand ow to implimantate this ;(
What part or parts do you not understand? How far have you gotten? What code have you tried?
jaki jaki

2015/6/25

#
can you tell me just the way to do this. I mean the idea behind this
danpost danpost

2015/6/25

#
jaki wrote...
can you tell me just the way to do this. I mean the idea behind this
This is quite vague. To answer that question, there would be a lot of 'if this situation, then this is done' in the response and it would get quite confusing. Also, you are asking us to write a chapter as opposed to a section or paragraph (most people are not willing to do that). Best would be for you to explain what you want to achieve as far as the scrolling is concerned. Is there a main character that is to be followed or is the scrolling automatic? Would it be scrolling horizontally, vertically, or both? Is there a background image that needs to be scrolled and what size is it compared to the world window? These basic questions should suffice in narrowing down what is required in your case and giving the idea behind it would not be a book.
jaki jaki

2015/6/25

#
ok sorry no i try to ask exactly i want to programm a thing like super mario and the character (mario) should stay always in zhe middle of the screen. So i just need a scrolling world. At first the background can be always the same. Oh and i just want scroll vertical ;)
danpost danpost

2015/6/25

#
Then when Mario (or whosever) gets off the vertical center, have the world move him back and everything else in the world the same direction and amount. In other words in the act method of the world, place code to check to position of the main character. If the main character is not in the center vertically, determine how far from the center it is and move all objects with respect to each other, placing the main character back in the vertical center.
jaki jaki

2015/6/26

#
i tried everything but it is always a error. pls can you show me how the act method in the world class look like ?
danpost danpost

2015/6/26

#
Basically, you would need to keep a reference to Mario (or whoever) in the world class:
private Actor mainCharacter;
and in the constructor, set its value:
mainCharacter = new Mario();
and in the act method:
if (mainCharacter.getWorld() == this) // ensure main character is in this world
{
    int dy = mainCharacter.getX()-getHeight()/2; // offset from vertical center
    if (dy != 0) // if not in center
    {
        for (Object obj : getObjects(null)) // for all actors in world
        {
            Actor actor = (Actor)obj; // refer to object as an Actor object
            actor.setLocation(actor.getX(), actor.getY()-dy); // adjust location
        }
    }
}
Now, if you are using a smooth moving system or have special treatment to the location of any of your actors, the adjusting of their location may need to be changed to work properly with it. If you have any Actor objects that need to stay in place, such as scores, lives, etc, add the following to their class codes:
public void move() {};
public void setLocation(int x, int y) {};
This will prevent them from being moved by other classes (you can use 'super.move' or 'super.setLocation' from within the class to change their locations).
jaki jaki

2015/6/27

#
ty very much you such a nice guy ;) but one question: i can compile it but then in the greenfoot terminal windos ("java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.") BUT : the actor mario is in the world ( addObject (new mario(),50,50);) why doesnt work it ?
danpost danpost

2015/6/27

#
jaki wrote...
i can compile it but then in the greenfoot terminal windos ("java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.") BUT : the actor mario is in the world ( addObject (new mario(),50,50);) why doesnt work it ?
That is probably an issue in your mario class code. Cannot help you without seeing what you have there. Next time you get an error message like that, please post the entire stack trace (not just the top line). The information it provides can determine the source of the error.
jaki jaki

2015/6/28

#
NOW IT DOES WORK ;) but how to go on ? my world class
import greenfoot.*;

public class worlds extends World
{
    private Actor maincharacter;

    public worlds() {
          super(600, 400, 1); 
         
          addObject (new mario(),300,200);
           addObject (new object(),200,200);
     
           maincharacter= new mario();
       
    } 
    
    public void act (){
        if (maincharacter.getWorld() == this){
            int b;
            int dy = maincharacter.getX()-getHeight()/2;
            if (dy != 0 )  {// nicht in der Mitte 
                for (Object obj : getObjects(null)) {
                    Actor actor = (Actor)obj; 
                    actor.setLocation(actor.getX(), actor.getY()-dy); 
                   
                }
            }

        }
    }
}
actor class mario
import greenfoot.*;

public class mario extends Actor
{

    public void act() 
    {
        links();
        rechts();
    }    

     public void rechts (){

        if (Greenfoot.isKeyDown("right")){

            move(2);
        }

     }

    public void links (){
        int x = getX ();

        int y= getY();
        if (Greenfoot.isKeyDown("left")){

            move(-2);
        }

    }

}
danpost danpost

2015/6/28

#
You are creating two separate mario objects. One you are adding into the world and the other you are setting to the reference field. You must only create one mario object and set that one to the field and also add that same one into the world. In the worlds class, move line 13 to line 9 and add 'mainCharacter' into the world at line 10. Line 19 in the worlds class and lines 22 an 24 in the mario class should be removed as none of those declared fields ('b', 'x', and 'y') are being used anywhere. After the required changes, you will only see the scrolling work when you manually move the main character up or down away from the center and then click on the 'Act' button' (there is no apparent code to move mario vertically yet).
jaki jaki

2015/6/28

#
ok ty ;) is this right
 maincharacter= new mario();
        addObject (new  maincharackter(),300,200);
but then (cannot find symbol- class maincharackter)
Super_Hippo Super_Hippo

2015/6/28

#
'maincharater' is a actor variable, you want to add this object to the world. 'new ClassName()' creates a new actor of the class, you already created the object though:
maincharacter= new mario();
addObject (maincharackter,300,200);
jaki jaki

2015/6/28

#
ok but now the object and mario are instand rushing upwards ;(
There are more replies on the next page.
1
2
3