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

2013/1/4

the world doesn't show up when compiled?

AnneMacaroni AnneMacaroni

2013/1/4

#
ok... this is weird, so I call the method in the constructor for MyWorld, everything compiles. However, the world doesn't show up. Like there's nothing on the main screen. The run button is grey. The entire page is blank.... I want to keep adding blocks to the world to create a platform. When I use a while loop, it doesn't work. But when I use a for loop where i < 100 does work............. The code below shows what I tried to do: MyWorld:
public void createContinualPlatform()
    {
        while (true)
        {
                grassPlain = new GrassPlain();
                addObject(grassPlain, XCo, YCo);

                XCo += 48;
         }
    }
vonmeth vonmeth

2013/1/4

#
That is the definition of an infinite loop. Since the loop will never break since the condition itself is simply "true".
while (XCo < getWidth()) // will perform loop until XCO is greater than the world's width  
        {  
                grassPlain = new GrassPlain();  
                addObject(grassPlain, XCo, YCo);  
  
                XCo += 48;  
         }
AnneMacaroni AnneMacaroni

2013/1/4

#
Sorry to not have mentioned it, but the platform will be moving -2 to the left and the platform can slide off the screen... how will it work then?
danpost danpost

2013/1/4

#
One thing you can do is create enough GrassPlain objects to go across the screen and then add two or three more. Then in the GrassPlain class, add a check in the 'act' method to move plain to the next location off one side of the window after it completely moves off the other side.
// in world constructor
GrassPlain grassPlain = new GrassPlain();
int gpWidth = grassPlain.getImage().getWidth();
int gpCount = (getWidth()/gpWidth)+2;
GrassPlain.totalPlains = gpCount;
for(int i=0; i<gpCount; i++)
{
    addObject(new GrassPlain(), i*gpWidth+gpWidth/2, YCo);
    // 'YCo' above needs either to be previously set or replaced with the actual value
}
Then, in the GrassPlain class
// add a class field
public static int totalPlains;
// in the 'act' method
if(getX()<-getImage().getWidth()/2)
{
    setLocation(getX()+getImage().getWidth()*totalPlains, getY());
}
AnneMacaroni AnneMacaroni

2013/1/4

#
thanks for your input :)
AnneMacaroni AnneMacaroni

2013/1/4

#
Also, how come my actor doesn't fall even if I call the method from the actor's act() method? This is in the Dog class:
public void fall()
    {   
        fallSpeed = 0;
        acceleration = 1;
        
        setLocation(getX(), getY() + fallSpeed);
        fallSpeed += acceleration;
    }
but I call the method from the smallDog class which is a subclass of Dog. Please help!!
danpost danpost

2013/1/4

#
Follow the logic: line 3 sets 'fallSpeed' to zero before being used in line 6, which therefore sets the location of the Dog object to the location it is already at. First remove line 3; then change line 4 to 'acceleration +=1;'. Incrementing 'acceleration' is in effect adding gravity. The only time you want to zero 'fallSpeed' is when an obstacle is encountered either above or below the Dog object while the Dog object is moving toward it.
AnneMacaroni AnneMacaroni

2013/1/4

#
There is an obstacle below the smallDog. I followed the tutorial (running, falling, jumping) given by Greenfoot on youtube and that's what the guy told me to do In Dog class:
private int fallSpeed;
private int acceleration;
private int jumpStrength = -10;
private Actor actorUnder;

public void jump()
    {
        fallSpeed = jumpStrength;
        fall();
    }
    
    public void checkFall()
    {
        if (onGround() == false)
            fallSpeed = 0;
        else
            fall();
    }
    
    public void fall()
    {   
        fallSpeed = 0;
        acceleration = 1;
        
        setLocation(getX(), getY() + fallSpeed);
        fallSpeed += acceleration;
    }
    
    public boolean onGround()
    {
        actorUnder = getOneObjectAtOffset(0, getImage().getHeight()/2, Grass.class);
        
        if (actorUnder != null)
            return true;
        else
            return false;
    }
}
danpost danpost

2013/1/4

#
First, you need an 'act' method to call the methods you have in the class. Plus, there is no condition coded to start a jump; and there is not any code for horizonal movement of the Dog object. One more thing, the logic in 'checkFall' seems to be amiss.
You need to login to post a reply.