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

2017/3/1

How exactly does this work?

1
2
DuckGod DuckGod

2017/3/1

#
My teacher recommend me some code but i can't quite understand it here it is
private void Grounds()
    {
        if (world.isGround(getX(), getY()+16))
        { yspeed = 0; 
            canjump = true; }// to check if standing on the color black

        if (world.isGround(getX(), getY()+14) )
        { y -= 1;}//

        if (world.isGround(getX(), getY()-14) )
        { y += 1; 
            if (yspeed < 0){yspeed = 0;}
        }//to check if under color black

        if (world.isGround(getX()+14, getY()) )
        { x -= 1;
            if (xspeed > 0) {xspeed = 0;}
        }// to check if trying to run into color black from the right side

        if (world.isGround(getX()-14, getY()) )
        { x += 1;
            if (xspeed < 0) {xspeed = 0;}
        }// to check if trying to run into color black from left side
    }
That's form the player class.
private void setColors()
    {
        groundColor = codes.getColorAt(1,1);
        AIColor=codes.getColorAt(22,1);
        spikeColor = codes.getColorAt(57,1);
    }

    public boolean isGround(Color mg)
    {
        if (groundColor.equals(mg)) 
        {return true;}
        return false;
    }

    public boolean isGround(int x, int y)
    {
        if (x > swidth-1 || x < 0 || y > sheight-1 || y < 0) {return false;}

        Color ez = bgmask.getColorAt(x,y);
        if (groundColor.equals(ez)) 
        {return true;}
        return false;
    }

    public boolean isPatrol(int x, int y)
    {
        if (x > swidth-1 || x < 0 || y > sheight-1 || y < 0) {return false;}

        Color ez = bgmask.getColorAt(x,y);
        if (AIColor.equals(ez)) 
        {return true;}
        return false;
    }

    public boolean isSpike(int x, int y)
    {
        if (x > swidth-1 || x < 0 || y > sheight-1 || y < 0) {return false;}

        Color ez = bgmask.getColorAt(x,y);
        if (spikeColor.equals(ez)) 
        {return true;}
        return false;
    }
    
    static  public void paintBg(int x, int y)
    {
        background.setColorAt(x,y, Color.RED);
        background.setColor(Color.RED);
        background.fillRect(x,y, 10,10);
    }
DuckGod DuckGod

2017/3/1

#
oh i forgot to mention that the 2ed peice of code is in the world class
Nosson1459 Nosson1459

2017/3/1

#
What part do you want an explanation on? What happens that you don't understand?
DuckGod DuckGod

2017/3/1

#
I get an error every time i put the code in my act method
DuckGod DuckGod

2017/3/1

#
the grounds(); one
Nosson1459 Nosson1459

2017/3/1

#
DuckGod wrote...
I get an error every time i put the code in my act method
What error do you get? A compiling error? If you get a compiling then say what and where it is. If it's a different type of error then say what and where that error is.
DuckGod DuckGod

2017/3/1

#
i get this. java.lang.NullPointerException at Egg.Grounds(Egg.java:204) at Egg.act(Egg.java:141) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211)
Nosson1459 Nosson1459

2017/3/1

#
Which line is that? It says line 204 and 141 but there aren't so many lines in the shown code.
DuckGod DuckGod

2017/3/1

#
here's the whole code.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Egg here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Egg extends Actor
{
    float x = 160;// where the player's x cords are
    float y = 600;// where the player's y cords are
    float xspeed;
    float yspeed;

    float maxspeed=3;// maximum speed obtainable

    boolean canjump=false;
    static GreenfootImage[] frames = new GreenfootImage[18];

    float currentframe=0;
    float animationspeed=0.1f;
    float startframe=0;
    float endframe=0;
    int animno=0;
    int dir=0;//pretty sure this is direction
    int animationdelay=0;

    boolean dead = false;

    private WorldLoader world;

    public Egg()
    {
        if(frames[0]==null)
        {
            frames[0] = new GreenfootImage("EggStandStill.png");
            frames[1] = new GreenfootImage("EggStandStill.png");
            frames[2] = new GreenfootImage("EggMove1.png");
            frames[3] = new GreenfootImage("EggMove2.png");
            frames[4] = new GreenfootImage("EggMove3.png");
            frames[5] = new GreenfootImage("EggMove4.png");
            frames[6] = new GreenfootImage("EggMove5.png");
            frames[7] = new GreenfootImage("EggMove6.png");

            //mirrored frames //just add 16
            frames[0+8] = new GreenfootImage("EggStandStill.png");
            frames[1+8] = new GreenfootImage("EggStandStill.png");
            frames[2+8] = new GreenfootImage("EggMove1.png");
            frames[3+8] = new GreenfootImage("EggMove2.png");
            frames[4+8] = new GreenfootImage("EggMove3.png");
            frames[5+8] = new GreenfootImage("EggMove4.png");
            frames[6+8] = new GreenfootImage("EggMove5.png");
            frames[7+8] = new GreenfootImage("EggMove6.png");
            for (int i = 8; i<7+9;i++)
            {
                frames[i].mirrorHorizontally();
                frames[i].scale(25,50);
            }

            for(int i = 0; i<8;i++)
            {
                frames[i].scale(25,50);
            }

            setanim_stand();
        }
    }

    private void animate(){
        if (animationdelay > 0) {animationdelay--;}

        currentframe += animationspeed;
        if (currentframe > endframe) { currentframe = startframe;}

        this.setImage(frames[(int)currentframe + dir]);
    }//animate

    private void setanim_stand()
    {
        if (animno == 0 || canjump == false || animationdelay > 0) {return;}
        animno = 0;
        animationspeed = 0; 
        currentframe = 0;  
        startframe = 0;  
        endframe = 0;
    }//stand

    private void setanim_run()
    {
        if (animno == 1 || canjump == false || animationdelay > 0) {return;}
        animno = 1;
        animationspeed = 0.2f; 
        currentframe = 0;  
        startframe = 0;  
        endframe = 7;
    }//run

    /*private void setanim_jump()
    {
        if (animno == 2 || animationdelay > 0) {return;}
        animno = 2;
        animationspeed = 0; 
        currentframe = 2;  
        startframe = 2;  
        endframe = 2;
    }//jump

    private void setanim_fall()
    {
        if (animno == 5 || canjump == true || animationdelay > 0) {return;}
        animno = 5;
        animationspeed = 0; 
        currentframe = 5;  
        startframe = 5;  
        endframe = 5;
    }//fall*///I will come back to this

    public void act()
    {
        animate();

        standStill();

        checkKeys();

        //physics
        physics();

        fallSpeed();
        
        atWorldBottom();

        if (y < 16) {y = 16;}

        worldLimits();

        setLocation((int)x,(int)y-8);
        //world.setPlayerCoords(getX(),getY() );

        //Grounds();
    }

    public void addedToWorld(World world) {
        world = (WorldLoader) world;  

        x = getX();
        y = getY();
    }//addedtoworld 
    
    public void standStill()
    {
        if(Math.abs(xspeed)<0.3){setanim_stand();}
    }

    public void checkKeys()
    {
        //run left
        if (Greenfoot.isKeyDown("left") && xspeed > -maxspeed ||Greenfoot.isKeyDown("a") && xspeed > -maxspeed )
        {            xspeed -= 0.75; setanim_run(); 
            if (animationdelay <= 0 ) {dir = 8;} 
        }
        //run right
        if (Greenfoot.isKeyDown("right") && xspeed < maxspeed || Greenfoot.isKeyDown("d") && xspeed < maxspeed )
        {            xspeed += 0.75; setanim_run(); 
            if (animationdelay <= 0 ) {dir = 0;}
        }
        //jump
        /*if (Greenfoot.isKeyDown("up") && canjump || Greenfoot.isKeyDown("w") && canjump)
        {            yspeed = -6; canjump = false;  setanim_jump();      }*/
    }
    
    public void physics()
    {
        x+=xspeed;// to increce speed
        y+=yspeed;// to increce fall speed

        xspeed *=0.91;//used to slow down but will never reach 0 cuz science!
    }
    
    public void fallSpeed()
    {
        if (yspeed > 2 ) {/*setanim_fall();*/ canjump = false;}// if falling faster then 2 velocity cant jump and animate fall
        if (yspeed < 5 ) {yspeed += 0.3;}//gravity and terminal velocity
    }
    
    public void atWorldBottom()
    {
        if (y > 500)
        {
            x = world.startx;
            y = world.starty;
        }
    }
    
    public void worldLimits()
    {
        if (x > (world.swidth-16)) { x = (world.swidth-16); }//to stop players from going past world limits
        if (x < 0) {x = 0;}
    }

    private void Grounds()
    {
        if (world.isGround(getX(), getY()+16))
        { yspeed = 0; 
            canjump = true; }// to check if standing on the color black

        if (world.isGround(getX(), getY()+14) )
        { y -= 1;}//

        if (world.isGround(getX(), getY()-14) )
        { y += 1; 
            if (yspeed < 0){yspeed = 0;}
        }//to check if under color black

        if (world.isGround(getX()+14, getY()) )
        { x -= 1;
            if (xspeed > 0) {xspeed = 0;}
        }// to check if trying to run into color black from the right side

        if (world.isGround(getX()-14, getY()) )
        { x += 1;
            if (xspeed < 0) {xspeed = 0;}
        }// to check if trying to run into color black from left side
    }
}
Nosson1459 Nosson1459

2017/3/1

#
Your class variable "world" has the same name as the variable in the addedToWorld method. Try changing line 145 to:
this.world = (WorldLoader)world;
DuckGod DuckGod

2017/3/1

#
java.lang.NullPointerException at WorldLoader.isGround(WorldLoader.java:73) at Egg.Grounds(Egg.java:204) at Egg.act(Egg.java:141) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211) now i get this.
Nosson1459 Nosson1459

2017/3/1

#
Show the WorldLoader class code. How do you get an error on line 141 if that line is commented out?
DuckGod DuckGod

2017/3/1

#
oh I uncommeted it to test it out
DuckGod DuckGod

2017/3/1

#
because it was the code that was messing everything up
DuckGod DuckGod

2017/3/1

#
but here's the world loader class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class TutorialLvl here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class WorldLoader extends World
{
    static GreenfootImage background;
    static GreenfootImage bgmask;//bg means background

    static int swidth=700;//S means Super
    static int sheight=500;//S means Super

    static GreenfootImage codes = new GreenfootImage("codes.gif");// The colors of grounds

    static Color groundColor;
    static Color AIColor;//AI means Artifical Intenigents
    static Color spikeColor;
    
    static int currentLevel = 0;
    static int maxLevel = 1;//total amount of levels
    
    static public int playerx, playery;//for enemies
    static public int startx, starty;//the starting place of the player
    /**
     * Constructor for objects of class TutorialLvl.
     * 
     */
    public WorldLoader()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(swidth, sheight, 1); 
        setColors();// this sets the colors for all of the other actors
    }
    
    public void act()
    {
        lastlevel();
    }
    
    private void lastlevel()
    {
        if (currentLevel == 2
        && playerx > 300
        && playery < 100 
        )
        {
            changeLevel();
        }
    }

    private void setColors()
    {
        groundColor = codes.getColorAt(1,1);
        AIColor=codes.getColorAt(22,1);
        spikeColor = codes.getColorAt(57,1);
    }

    public boolean isGround(Color mg)
    {
        if (groundColor.equals(mg)) 
        {return true;}
        return false;
    }

    public boolean isGround(int x, int y)
    {
        if (x > swidth-1 || x < 0 || y > sheight-1 || y < 0) {return false;}

        Color ez = bgmask.getColorAt(x,y);
        if (groundColor.equals(ez)) 
        {return true;}
        return false;
    }

    public boolean isPatrol(int x, int y)
    {
        if (x > swidth-1 || x < 0 || y > sheight-1 || y < 0) {return false;}

        Color ez = bgmask.getColorAt(x,y);
        if (AIColor.equals(ez)) 
        {return true;}
        return false;
    }

    public boolean isSpike(int x, int y)
    {
        if (x > swidth-1 || x < 0 || y > sheight-1 || y < 0) {return false;}

        Color ez = bgmask.getColorAt(x,y);
        if (spikeColor.equals(ez)) 
        {return true;}
        return false;
    }
    
    static  public void paintBg(int x, int y)
    {
        background.setColorAt(x,y, Color.RED);
        background.setColor(Color.RED);
        background.fillRect(x,y, 10,10);
    }
    
    public void setPlayerCoords(int px, int py)
    {
        playerx = px;
        playery = py;
        //    background.fillRect(px,py, 3,3);
    }
    
    public void cleanUp()
    {
        this.removeObjects(this.getObjects(Egg.class));
    }
    
    public void changeLevel()
    {
        currentLevel++;
        if(currentLevel > maxLevel)
        {
            currentLevel=-1;
        }
        setLevel(currentLevel);
    }
    
    public void restartLevel()
    {
        setLevel(currentLevel);
    }
    
    public void restartGame()
    {
        currentLevel=-1;
        setLevel(currentLevel);
    }
    
    public void setLevel(int i)
    {
        if (i <-2 || i > maxLevel) {return;}
        cleanUp();
        currentLevel=i;
        switch(i)
        {
            case -2: testLvl(); break;
        }
    }

    public void testLvl()
    {
        background= new GreenfootImage("MaskTest.png");
        setBackground(background);
        bgmask = new GreenfootImage("MaskTest.png");
        startx= 100;
        starty=500;
        addObject(new Egg(), startx,starty);
    }
}

i'm still working on it.
There are more replies on the next page.
1
2