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

2024/8/11

Second Player in a Platform game

CowboyIggy CowboyIggy

2024/8/11

#
I'm creating a Platform game with 2 playable characters, but I'm having a problem with the Scrolling, since I'm using the Scrolling code from danpost "https://www.greenfoot.org/scenarios/18226" I created a ScrollActor for my first character but my second character can't scroll the world, also tried to make the two of them as ScrollActors but the screen bugged and tried to scroll to each side at the same time, can somebody help me? //DANPOST SCROLLING WORLD CODE
public static final int WIDE = 600;
    public static final int HIGH = 400;
    
    Scroller scroller;
    Actor ScrollActor;
    
    public PraiaCarry()
    {    
        super(WIDE, HIGH, 1, false);
        GreenfootImage bg = new GreenfootImage("Fase da Praia2.png");
        int bgWide = bg.getWidth();
        int bgHigh = bg.getHeight();
        scroller = new Scroller(this, bg, 6000, 400); 
        ScrollActor = new Player1();
        addObject(ScrollActor, 110, 290);
        scroll();
        prepare();
    }
public void act()
    {
        if(ScrollActor != null) scroll();
    }
    
    public void scroll()
    {
        /*
        int dsx = ScrollActor.getX() - WIDE/2;
        int dsy = ScrollActor.getY() - HIGH/2;
        scroller.scroll(dsx,dsy);
        */
       
       int loX = 200;
       int hiX = WIDE - 200;
       int loY = 50;
       int hiY = HIGH - 50;
       
       int dsx = 0, dsy = 0;
       if(ScrollActor.getX()<loX)dsx = ScrollActor.getX() - loX;
       if(ScrollActor.getX()>hiX)dsx = ScrollActor.getX() - hiX;
       if(ScrollActor.getY()<loY)dsy = ScrollActor.getY() - loY;
       if(ScrollActor.getY()>hiY)dsy = ScrollActor.getY() - hiY;
       scroller.scroll(dsx,dsy);
    }
danpost danpost

2024/8/12

#
CowboyIggy wrote...
I'm using the Scrolling code from danpost "https://www.greenfoot.org/scenarios/18226" I created a ScrollActor ...
A much better choice would be to use my Scroller class, found in my Scroller Class Demos scenario and its use is discussed in my Everything Scrolling scenario.
CowboyIggy CowboyIggy

2024/8/12

#
danpost wrote...
CowboyIggy wrote...
I'm using the Scrolling code from danpost "https://www.greenfoot.org/scenarios/18226" I created a ScrollActor ...
A much better choice would be to use my Scroller class, found in my Scroller Class Demos scenario and its use is discussed in my Everything Scrolling scenario.
I'm using the Scroller class in "Scroller scroller" but It is working only for 1 Actor, is it possible to create 2 actors that can scroll at the same time and when 1 actor is in the left edge and the other is in the right the scroll just stops? Or it would be better make the world only scroll to the right so the actors can scroll the world since one of them goes to the right?
danpost danpost

2024/8/12

#
CowboyIggy wrote...
it would be better make the world only scroll to the right so the actors can scroll the world since one of them goes to the right?
Use the Scroller class I mentioned. Place a static field in the class of the scrolling actors:
public static Actor leader = null;
and in the act method of the class, after moving the actor, add:
if (leader == null || leader.getX() < getX()) leader = this;
Now, the world can scroll with regard to the leading, or most-right, actor:
public void act() { //    (in World subclass)
    Actor rightest = << ScrollingActorClassName >>.leader;
    if (rightest == null) return;
    scroller.scroll(rightest.getX()-getWidth()/2, 0);
}
Also, in the same World subclass, add the following line to the constructor ( "public MyWorld()" ):
<< ScrollingActorClassName >>.leader = null;
This will remove the old scrolling actor from the field during scenario resets.
CowboyIggy CowboyIggy

2024/8/12

#
danpost wrote...
CowboyIggy wrote...
it would be better make the world only scroll to the right so the actors can scroll the world since one of them goes to the right?
Use the Scroller class I mentioned. Place a static field in the class of the scrolling actors:
public static Actor leader = null;
and in the act method of the class, after moving the actor, add:
if (leader == null || leader.getX() < getX()) leader = this;
Now, the world can scroll with regard to the leading, or most-right, actor:
public void act() { //    (in World subclass)
    Actor rightest = << ScrollingActorClassName >>.leader;
    if (rightest == null) return;
    scroller.scroll(rightest.getX()-getWidth()/2, 0);
}
Also, in the same World subclass, add the following line to the constructor ( "public MyWorld()" ):
<< ScrollingActorClassName >>.leader = null;
This will remove the old scrolling actor from the field during scenario resets.
It solved the problem, thx
You need to login to post a reply.