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

2022/9/29

Scroller for jump´n run game

Mauckenweg Mauckenweg

2022/9/29

#
I need a scroller for a mariobros style game which only scrolls horizontally. I have tried multiple codes from the internet but none worked. pls help
Mauckenweg Mauckenweg

2022/9/29

#
here is the code from my game so far
public class Waluigi extends Spieler
{
    int ySpeed;
    int xSpeed;
    int apexTimer;
    int richtung=2;
    int animZaehler;
    int fallzaehler;
    private GreenfootImage bildL, bildR, bildLs, bildRs, bild1L, bild2L, bild3L, bild4L, bild1R, bild2R, bild3R, bild4R;
    public Waluigi()
    {
        bildL = new GreenfootImage("WaluigiL.png");
        bildR = new GreenfootImage("WaluigiR.png");
        bildLs = new GreenfootImage("WaluigiLS.png");
        bildRs = new GreenfootImage("WaluigiRS.png");
        bild1L = new GreenfootImage("WaluigiLL1.png");
        bild2L = new GreenfootImage("WaluigiLL2.png");
        bild3L = new GreenfootImage("WaluigiLL3.png");
        bild4L = new GreenfootImage("WaluigiLL4.png");
        bild1R = new GreenfootImage("WaluigiRL1.png");
        bild2R = new GreenfootImage("WaluigiRL2.png");
        bild3R = new GreenfootImage("WaluigiRL3.png");
        bild4R = new GreenfootImage("WaluigiRL4.png");
    }
    public void act()
    {
        animZaehler++;
        fallzaehler++;
        richtung();
        movement();
        animationen();
        int groundLevel = getWorld().getHeight() - getImage().getHeight()/2;
        boolean onGround = (isTouching(ground.class));
        if (!onGround) // in middle of jump
        {   if (ySpeed == 0 && apexTimer > 0) apexTimer--; // run apex timer
            if (ySpeed == 0 && apexTimer > 0) return; // apex timer still running
            ySpeed++;// adds gravity effect
            setLocation(getX(), getY()+ySpeed-2); // fall (rising slower or falling faster)
            if (getY()>=groundLevel) setLocation(getX(), groundLevel); // set on ground
        }
        else // on ground
        {
            if (Greenfoot.isKeyDown("space")) // jump key detected
            {
                ySpeed = -20; // add jump speed
                setLocation(getX(), getY()+ySpeed); // leave ground
                apexTimer = 8;  // set apex timer (adjust value to suit)
            }}
        
    }
    public void richtung()
    {
        if (Greenfoot.isKeyDown("D"))richtung= 1;
        if (Greenfoot.isKeyDown("A"))richtung= 2;
    }
    public void movement()
    {
        if (Greenfoot.isKeyDown("D")&&!Greenfoot.isKeyDown("M"))move(2);
        if (Greenfoot.isKeyDown("A")&&!Greenfoot.isKeyDown("M"))move(-2);
        if (Greenfoot.isKeyDown("D")&&Greenfoot.isKeyDown("M"))move(5);
        if (Greenfoot.isKeyDown("A")&&Greenfoot.isKeyDown("M"))move(-5);
    }
    public void animationen()
    {
        int groundLevel = getWorld().getHeight() - getImage().getHeight()/2;
        boolean onGround = (getY() == groundLevel);
        if (!onGround)
        {
            if (richtung==1)setImage(bildRs);
            if (richtung==2)setImage(bildLs);
            animZaehler=0;   
        }
        if (onGround)
        {
         if (!Greenfoot.isKeyDown("A")&&!Greenfoot.isKeyDown("D"))
         {
            if (richtung==1)setImage(bildR);
            if (richtung==2)setImage(bildL);
            animZaehler=0;
         }
         if (Greenfoot.isKeyDown("M"))
           {
               if (animZaehler==1)
               {
                   if (Greenfoot.isKeyDown("A"))bildWechselnL();
                   if (Greenfoot.isKeyDown("D"))bildWechselnR();
               }
               if (animZaehler==6)
               {
                   if (Greenfoot.isKeyDown("A"))bildWechselnL();
                   if (Greenfoot.isKeyDown("D"))bildWechselnR();
               }
               if (animZaehler==11)
               {
                   if (Greenfoot.isKeyDown("A"))bildWechselnL();
                   if (Greenfoot.isKeyDown("D"))bildWechselnR();
               }
               if (animZaehler==16)
               {
                   if (Greenfoot.isKeyDown("A"))bildWechselnL();
                   if (Greenfoot.isKeyDown("D"))bildWechselnR();
               }
               if (animZaehler==21)animZaehler=1;
           }
         else
         if (!Greenfoot.isKeyDown("M"))
           {
               if (animZaehler==1)
               {
                   if (Greenfoot.isKeyDown("A"))bildWechselnL();
                   if (Greenfoot.isKeyDown("D"))bildWechselnR();
               }
               if (animZaehler==11)
               {
                   if (Greenfoot.isKeyDown("A"))bildWechselnL();
                   if (Greenfoot.isKeyDown("D"))bildWechselnR();
               }
               if (animZaehler==21)
               {
                   if (Greenfoot.isKeyDown("A"))bildWechselnL();
                   if (Greenfoot.isKeyDown("D"))bildWechselnR();
               }
               if (animZaehler==31)
               {
                   if (Greenfoot.isKeyDown("A"))bildWechselnL();
                   if (Greenfoot.isKeyDown("D"))bildWechselnR();
               }
               if (animZaehler==41)animZaehler=1;
           }
        }   
        if (animZaehler==21)animZaehler=1;   
    }
    private void bildWechselnL()
    {
        if (getImage() == bild1L)
        {
            setImage(bild2L);
        }
        else
        setImage(bild1L);
    }
    private void bildWechselnR()
    {
        if (getImage() == bild1R)
        {
            setImage(bild2R);
        }
        else
        setImage(bild1R);
    }
}    
my class
danpost danpost

2022/9/30

#
Mauckenweg wrote...
I need a scroller for a mariobros style game which only scrolls horizontally. I have tried multiple codes from the internet but none worked.
Please see my Scroller Class Demos scenario. It has a link to the Scroller class codes used in the demos. There is also my Scrolling Tutorial scenario, if you want to try a build a scrolling system yourself.
Mauckenweg Mauckenweg

2022/9/30

#
But i dont konw how to apply the code from that into my class
Mauckenweg Mauckenweg

2022/9/30

#
Everytime i tried to fill in your code the jumping and moving stopped working
danpost danpost

2022/9/30

#
Mauckenweg wrote...
Everytime i tried to fill in your code the jumping and moving stopped working
The tutorial describes an Actor following type, which can be easily modified to scroll only horizontally. To assist with your codes, you will need to show what you did. You should have an unmodified Scroller class added to your project, a reference to a Scroller object in your scrolling World subclass (with a Scroller object assigned to it);, and an act method in that world that controls what the Scroller object does. Show world class codes for further assistance.
You need to login to post a reply.