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

2016/10/4

I use a for loop and i'm stuck with an infinite loop

DuckGod DuckGod

2016/10/4

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Platform here.
 * 
 * @author (Allen) 
 * @version (10/3/16)
 */
public abstract class Platform extends World
{
    String[] map;
    /**
     * Constructor for objects of class Platform.
     * 
     */
    public Platform()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        setFields();
        for (int i=0; i<map.length; i++) for (int j=0; j<map[i].length(); j++)
        {
                int kind = "abcde".indexOf(""+map[i].charAt(j));
                if (kind < 0)  continue;
                Actor actor = null;
                if (kind == 0) actor = new Job();
                if (kind == 1) actor = new IoS();
                if (kind == 2) actor = new GrassOne();
                if (kind == 3) actor = new GrassTwo();
                if (kind == 4) actor = new BoB();
                addObject(actor, 16+j*32, 16+i*32);
        }
    }

    public void setFields() {}

    public void nextLevel() {}
}
danpost danpost

2016/10/4

#
DuckGod wrote...
I use a for loop and i'm stuck with an infinite loop
The for loop shown does not appear to be where the problem is. It could possibly be in one of your actor constructors (Job, IoS, GrassOne, GrassTwo, or BoB). By the way, it does not necessarily need to be a 'do', 'while' or 'for' structure that creates an infinite loop.
DuckGod DuckGod

2016/10/5

#
ok so my IoS, GrassOne, GrassTwo, and BoB right now have no code but it might be in my Job
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Job extends Jumper
{
    private int vSpeed = 0;
    private int acceleration = 1;
    private boolean jumping;
    private int jumpStrength = 16;
    private int speed=4;
    private int direction = 1;
    private int shootingCounter = 20;
    
    private GreenfootImage RStill= new GreenfootImage("Job.png");
    private GreenfootImage RStillOne= new GreenfootImage("Job1.png");
    private GreenfootImage RStillTwo= new GreenfootImage("Job2.png");
    private GreenfootImage RWalking1= new GreenfootImage("JobWalkingLeftFootForward.png");
    private GreenfootImage RWalking2= new GreenfootImage("JobWalkingRightFootForward.png");
    private GreenfootImage RShooting= new GreenfootImage("JobShootingRight.png");
    private GreenfootImage LStill= new GreenfootImage("LeftJob.png");
    private GreenfootImage LStillOne= new GreenfootImage("LeftJob1.png");
    private GreenfootImage LStillTwo= new GreenfootImage("LeftJob2.png");
    private GreenfootImage LWalking1= new GreenfootImage("LeftJobWalkingRightFootForward.png");
    private GreenfootImage LWalking2= new GreenfootImage("LeftJobWalkingLeftFootForward.png");
    private GreenfootImage LShooting = new GreenfootImage("JobShootingLeft.png");
    private int frame = 1;
    private int animationCounter = 0;
    /**
     * Act - do whatever the Job wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkKey();
        checkFall();
        shooting();
        platformAbove();
        shootingCounter --;
        animationCounter++;
    }
    
    public boolean shooting()
    {
        if(Greenfoot.isKeyDown("space") && shootingCounter <= 0 && direction ==1)
        {
            getWorld().addObject(new JobsBullet(), getX(), getY());
            setImage(RShooting);
            shootingCounter=20;
            return true;
        }
        if(Greenfoot.isKeyDown("space") && shootingCounter <= 0 && direction == -1)
        {
            getWorld().addObject(new JobsBulletLeft(), getX(), getY());
            setImage(LShooting);
            shootingCounter = 20;
            return true;
        }
        return false;
    }
    
    public void checkKey()
    {
        if(Greenfoot.isKeyDown("d"))
        {
            direction=1;
            moveRight();
        }
        if(Greenfoot.isKeyDown("a"))
        {
            direction = -1;
            moveLeft();
        }
        if(Greenfoot.isKeyDown("w") && jumping == false)
        {
            jump();
        }
    }
    
    public void moveRight()
    {
        setLocation(getX()+speed, getY());
        if(animationCounter % 4==0)
        {
            animateRight();
        }
    }
    
    public void animateRight()
    {
        if(frame == 1)
        {
            setImage(RWalking1);
        }
        else if (frame==2)
        {
            setImage(RWalking2);
        }
        else if(frame ==3)
        {
            setImage(RStill);
            frame = 1;
            return;
        }
        frame++;
    }
    public void moveLeft()
    {
        setLocation(getX()-speed, getY());
        if(animationCounter % 4==0)
        {
            animateLeft();
        }
    }
    
    public void animateLeft()
    {
        if(frame == 1)
        {
            setImage(LWalking1);
        }
        else if(frame==2){
            setImage(LWalking2);
        }
        else if(frame ==3)
        {
            setImage(LStill);
            frame = 1;
            return;
        }
        frame++;
    }
    
    public boolean platformAbove()
    {
        int spriteHeight = getImage().getHeight();
        int yDistance = (int)(spriteHeight/-2);
        Actor ceiling = getOneObjectAtOffset(0, yDistance, Grounds.class);
        if(ceiling != null)
        {
            vSpeed = 1;
            bopHead(ceiling);
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public void bopHead(Actor ceiling)
    {
        int ceilingHeight = ceiling.getImage().getHeight();
        int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/2;
        setLocation(getX(), newY);
    }
    
    public boolean onGround()
    {
        int spriteHeight = getImage().getHeight();
        int yDistance = (int)(spriteHeight/2) + 5;
        Actor ground = getOneObjectAtOffset(0, getImage().getHeight()/2, Grounds.class);
        if(ground == null)
        {
            jumping = true;
            return false;
        }
        else
        {
            moveToGround(ground);
            return true;
        }
    }
    
    public void moveToGround(Actor ground)
    {
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
        setLocation(getX(), newY);
        jumping = false;
    }
    
     public void fall()
    {
        setLocation(getX(), getY() + vSpeed);
        if(vSpeed <=9)
        {
            vSpeed = vSpeed + acceleration;
        }
        jumping = true;
    }
    
    public void checkFall()
    {
        if(onGround())
        {
            vSpeed=0;
        }
        else
        {
            fall();
        }
    }
    
    public void jump()
    {
        vSpeed = vSpeed - jumpStrength;
        jumping = true;
        fall();
    }
}
danpost danpost

2016/10/5

#
There is no constructor in this class; but, I see that it extends the Jumper class. It could be in there or, if your other classes extend something other than Actor, it could be in that class one of them extend.
danpost danpost

2016/10/5

#
Also, being your Platforrm class is abstract, you must be trying to instantiate a subclass of it to create your world. It could be there as well.
DuckGod DuckGod

2016/10/5

#
danpost wrote...
There is no constructor in this class; but, I see that it extends the Jumper class. It could be in there or, if your other classes extend something other than Actor, it could be in that class one of them extend.
well there is no code in Jumper class right now but i think that it could be in my bullet classes
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class JobsBullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class JobsBullet extends Actor
{
    private int shootingSpeed = 8;
    /**
     * Act - do whatever the JobsBullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX() + shootingSpeed, getY());
    }    
}
or
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class JobsBulletLeft here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class JobsBulletLeft extends Actor
{
    private int shootingSpeed = 8;
    /**
     * Act - do whatever the JobsBulletLeft wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX() - shootingSpeed, getY());
    }    
}
danpost danpost

2016/10/5

#
DuckGod wrote...
well there is no code in Jumper class right now but i think that it could be in my bullet classes
No. No constructors or methods being called during object creation in either of those. Are you sure you have an 'infinite loop'? What makes you think you have one?
DuckGod DuckGod

2016/10/5

#
because when i try to compile greenfoot says "The constructor for the world is taking a long time. You may have an infinite loop."
danpost danpost

2016/10/5

#
danpost wrote...
Also, being your Platforrm class is abstract, you must be trying to instantiate a subclass of it to create your world. It could be there as well.
There is also this.
DuckGod DuckGod

2016/10/5

#
Oh so the subclass of platform could be causing it if so then my LvlOne subclass might be the problem right?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class LvlOne here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LvlOne extends Platform
{

    /**
     * Constructor for objects of class LvlOne.
     * 
     */
    public void setFields()
    {
        map = new String[] {"                  a             ",
                            "  bbbbbb                bbbbcc  ",
                            "            cccc bbbbb          ",
                            "                                ",
                            "     cccc         cccbcb       c",
                            "                 ccc         cb ",
                            "         cbc                    ",
                            " cccbbcc                  c     ",
                            "bbb    cccc        bbbcb    bb  ",
                            "                                ",
                            "                                ",
                            " cbcb        cbcbc    cb  cb    ",
                            " cbcb            cbbbc      cb  "};
    }
}
danpost danpost

2016/10/5

#
DuckGod wrote...
because when i try to compile greenfoot says "The constructor for the world is taking a long time. You may have an infinite loop."
Try right clicking on the LvlOne class and selecting 'new LvlOne()'.
DuckGod DuckGod

2016/10/5

#
oh what? XD thats just weird i spent 4 days trying to figure it out and you fix it in like 1-2 hours. Thank you
danpost danpost

2016/10/5

#
DuckGod wrote...
oh what? XD thats just weird i spent 4 days trying to figure it out and you fix it in like 1-2 hours. Thank you
Apparently, the initial world that was problematic was not the LvlOne world. Maybe, it was attempting to create a Platform world; but, being abstract, it was unable to (but, I doubt this). Do you have other subclasses of Platform yet? Maybe, it was attempting to create a different type subclass of Platform where you may have an infinite loop. Btw, where did you acquire the code for the Platform class? It looks like a cross between my Map class and my Level class (it looks like something I would have written).
DuckGod DuckGod

2016/10/6

#
Oh I got it from my friend he was helping me for programming class.
You need to login to post a reply.