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

2017/5/15

how to make platforms?

chlo chlo

2017/5/15

#
I am trying to add platforms into my game for my avatar to jump on and I am creating them all different lengths out of 1 block, kinda like the Mario games with the bricks, but I can't figure out how to make a platform or how to make my avatar jump on it. can anyone help with the basic code for making a platform?
chlo chlo

2017/5/16

#
someone pls helpppp
ketschma ketschma

2017/5/16

#
1
2
Platform platform = new Platform();
        addObject(platform, getWidth() / 2, getHeight() / 2);
Platform is the classname; platform the variable; getWidth()/2 means that you place it at half the maximum X-value
Yehuda Yehuda

2017/5/17

#
ketschma wrote...
<Code Omitted> Platform is the classname; platform the variable; getWidth()/2 means that you place it at half the maximum X-value
The question seems to be asking for the code in the platform class, and the code for jumping.
chlo wrote...
but I can't figure out how to make a platform or how to make my avatar jump on it. can anyone help with the basic code for making a platform?
danpost danpost

2017/5/17

#
chlo wrote...
can anyone help with the basic code for making a platform?
A basic platform is just an object that does nothing but sits somewhere in your world. With a default image for the class, a platform class code could simply be this:
1
public class Platform extends greenfoot.Actor{}
You can extend this class to give certain platforms special abilities (moving, spawning objects, whatever).
chlo chlo

2017/5/17

#
I've tried that but my actor keeps going through the platforms and I don't know what I need to add to make it where the actor can jump on it without falling through.
Yehuda Yehuda

2017/5/17

#
Well that's probably something in the actor. Show what you've tried (and how you've failed).
danpost danpost

2017/5/18

#
In basic terms, you would have something like the following in the jumpers class:
1
2
setLocation(getX()+xSpeed, getY()+ySpeeed);
if (isTouching(Platform.class)) setLocation(getX()-xSpeed, gettY()-ySpeed);
Of course, improvements on the positioni of the actor can be made. That would involve moving horizontal and vertically separately and adjusting the position with respect to the width and height of the platform.
chlo chlo

2017/5/18

#
I added that line of code but I'm unsure how to change it to fit the width and height of the platform. I have my game set up where the world that I'm trying to put the platforms in, calls from an imported class that allows the background to scroll as the actor moves but I don't know if that would have anything to do with the actor not being able to land on the platforms. Here is the jump code I have for my actor when it jumps and where I added the code you suggested:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int groundLevel = getWorld().getHeight()-getImage().getHeight();
        ySpeed++;
        setLocation(getX(), getY()+ySpeed);
        if (getY() >= groundLevel)
        {
            setLocation(getX(), groundLevel);
            ySpeed = 0;
            if (Greenfoot.isKeyDown("up"))
            {
            ySpeed = -20;
            setLocation(getX()+vSpeed, getY()+ySpeed);
            if (isTouching(Platform.class)) setLocation(getX()-vSpeed, getY()-ySpeed);
             }
        }
And here is my ArrayList for the placement of my platforms in the world:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
private void makePlatform()
  {
      int x = 400;
      int y = 200;
      ArrayList P = new ArrayList <Platform> ();
      P.add(new Platform());
      P.add(new Platform());
      P.add(new Platform());
      P.add(new Platform());
      for(int i = 2; i < P.size();i++)
      {
          addObject((Actor)P.get(i), x*i+5, y);
      }
  }
And here is my code called from my separate scroll class that adds the actor to scroll in the world that I am unsure if I have to adjust it to add the Platform to it or not:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void scroll()
    {
        int limitX = 500;
        int ds = avatar.getX();
        if (ds >= limitX && ds <= getWidth()-limitX) return;
        if (ds > getWidth()-limitX) ds -= getWidth()-limitX*2;
        ds -= limitX;
        int scrolledAmt = scroll.changeScrollX(ds);
        for (Object obj : getObjects(null))
        {
            Actor actor = (Actor)obj;
            actor.setLocation(actor.getX()-scrolledAmt, actor.getY());
        }
    }
You need to login to post a reply.