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

2016/5/29

Platformer Floor Code not working

RussellBlo RussellBlo

2016/5/29

#
I am working on a platformer at the moment and I can't seem to get a couple pieces of code working. The following code:
      Actor floor = getOneObjectAtOffset(0, 0, floor.class);
      if (floor != null) {
          groundLevel = (getY()-62);
        }
      else {groundLevel=545;}
Is not producing desirable results. My actor is phasing through the other actor rather than standing on top. The 62 pixel offset is to make it stand on top instead of the middle of the floor. The following code:
    public void act() 
    {
        floorY = getY();
        if (Greenfoot.isKeyDown("d")){
          move(-2);
        }
        if (getX()<637 && getX()>635) {
            getWorld().addObject(new floor(),2580,635);
        }
        if (isOffScreen()) {
            getWorld().removeObject(this);
        }
    }    
    public boolean isOffScreen()
    {
     if (getX() < 0 - getImage().getWidth()/2){
          return true;
     }
     return false;
    }
Makes the floor move to the left under the player and then should keep generating ones under it. It was working before, although not perfectly, a few number adjustments to be made. But now there are massive gaps between floor actors. The floor actor is this: And the player actor is this:
danpost danpost

2016/5/29

#
RussellBlo wrote...
The 62 pixel offset is to make it stand on top instead of the middle of the floor.
That may be, but the 'getX()' is that of the player, not that of the floor. So. 'getX()-62' refers to a point 62 pixels above the player and has nothing to do with where the floor is at.
Makes the floor move to the left under the player and then should keep generating ones under it. It was working before, although not perfectly, a few number adjustments to be made. But now there are massive gaps between floor actors.
It would actually be easier just to keep the same floor objects in the world and have them play leap frog with themselves. Simply:
if (isOffScreen())
{
    setLocation(getX()+getImage().getWidth()*getWorld().getObjects(floor.class).size(), getY());
}
That will remove the need to add and remove floor objects. The initial placement of the floor actors when using this code is crucial, however:
floor floorObj = new floor(); // need a floor object
int floorWidth = floorObj.getWidth(); // to get width of a floor
int floorsNeeded = (getWidth()-1)/floorWidth+2; // to determine how many we need in world
for (int i=0; i<floorsNeeded; i++) // then they can be added
{
    addObject(new floor(), i*floorWidth , 635);
}
RussellBlo RussellBlo

2016/6/1

#
floor floorObj = new floor(); // need a floor object
int floorWidth = floorObj.getWidth(); // to get width of a floor
int floorsNeeded = (getWidth()-1)/floorWidth+2; // to determine how many we need in world
for (int i=0; i<floorsNeeded; i++) // then they can be added
{
    addObject(new floor(), i*floorWidth , 635);
}
The line:
int floorWidth = floorObj.getWidth(); //to get width of a floor
Calls an error. The error is "cannot find symbol - method getWidth()". This occurs when it is placed in either the world or the actor itself. I assumed that because you said that it had to occur at the initial placement it was used for adding it to the world. Thanks for the help Danpost but why is this happening?
SPower SPower

2016/6/1

#
There is no method getWidth (or getHeight for that matter) for Actors. You'll have to use getImage().getWidth().
SPower SPower

2016/6/1

#
Also, as programming advice, classes should start with an Uppercase letter, not a lowercase one.
danpost danpost

2016/6/1

#
SPower wrote...
There is no method getWidth (or getHeight for that matter) for Actors. You'll have to use getImage().getWidth().
That was a slip. Actually, it should be 'floorObj.getImage().getWidth()'. I have been around long enough to be aware that at one time you could call those methods, 'getWidth' and 'getHeight', directly on the Actor object and do catch myself still trying to do that occasionally.
You need to login to post a reply.