This discussion is archived
9 Replies Latest reply: Apr 14, 2010 3:54 AM by gimbal2 RSS

help with side-scrolling platformer

843853 Newbie
Currently Being Moderated
ok, I'm making this game- it's like one of those basic side-scrollers, and I had the collision working, but i changed something in it, and now it doesn't work:

// this method: wilfordScanUp, is in a diferent class, but was put here so you know what's happening
// public void wilfordScanUp(Floor[] platforms){
// for (Floor f : platforms)
// f.checkAbove(this);
// }
// }


public void checkAbove(Wilford, Brimley){
if
((this.x <= Brimley.getX() || this.x <= Brimley.getRight()) &&
((this.x + this.xS) >= Brimley.getX() || (this.x + this.xS) >= Brimley.getRight()) &&
(this.y + this.yS >= Brimley.getBottom()))
this.wilfordAbove = true;
else this.wilfordAbove = false;
}
}

basically, it's supposed to check whether the player is above, not landed on the platform. what happens though, is it checks to see if the player is above the each platform in the array, and then sets changed wilfordAbove to what it should be, but then, changes ALL of the platforms' "wilfordAbove" field to whatever it should be for the next platform in the array
  • 1. Re: help with side-scrolling platformer
    843853 Newbie
    Currently Being Moderated
    and for some reason, these formums get rid of all your +'s?
  • 2. Re: help with side-scrolling platformer
    gimbal2 Guru
    Currently Being Moderated
    ZoraKirby wrote:
    and for some reason, these formums get rid of all your +'s?
    For some reason you didn't use the code tags. When posting, select your code and press the CODE button to do it the lazy way.
  • 3. Re: help with side-scrolling platformer
    843853 Newbie
    Currently Being Moderated
    ok, so here it is, but I put it in a different class:
    public void wilfordScanUp(Floor[] platforms){
    for (Floor f : platforms)
    {if
    ((f.getX() <= this.getX() || f.getX() <= this.getRight()) && 
    ((f.getX() + f.getXS()) >= this.getX() || (f.getX() + f.getXS()) >= this.getRight()) &&
    (f.getY() + f.getYS() >= this.getBottom()))
    f.yesAbove();
    else f.notAbove();
    }}}
    XS is the width (as in x-size), and YS is the height (as in y-size)
    oh, and it works if there is only one platform in the array, but putting in more than one messes it up, only changing the wilfordAbove to true/false based only on the last platform in the array.
    so if the player is above the first platform in the array, (and not above the last one) then wilfordAbove = false for ALL of the platforms,
    but if the player is above the last platform in the array, then wilfordAbove is changed to true for ALL of the platforms. I only want it to change for that specific platform.
  • 4. Re: help with side-scrolling platformer
    843853 Newbie
    Currently Being Moderated
    I think what it's doing is checking it for all platforms, and if the player is above ANY of the platforms, then for each floor in platforms, wilfordAbove = true (or false)
  • 5. Re: help with side-scrolling platformer
    796262 Newbie
    Currently Being Moderated
    I'm not really sure what you're trying to do, as you've posted two different pieces of code.

    But what I think you're doing is something like this:
    boolean setMe;
    for(SomeObject o : someList){
       if(o.someCase()){
          setMe = true;
       }
       else{
          setMe = false;
       }
    }
    System.out.println(setMe);
    Obviously, that's only going to set setMe to the last case. That's not what you want to do. What you do want to do is still unclear to me though.

    Also, always surround if/else blocks with brackets, especially messy ones like yours. And notice that my above if statement could have been reduced to setMe = o.someCase();
  • 6. Re: help with side-scrolling platformer
    843853 Newbie
    Currently Being Moderated
    a platform is:
    public class Floor{
    private double x; //x-position
    private double y; //y-position
    private int xS; //width
    private int yS; //height
    private char type; //type of floor, so i can have things like lava, ice, snow, etc.
    private static boolean wilfordAbove; // whether the player is above the platform or not
    private static boolean wilfordBelow; //whether the player is below the platform or not(but doesn't do anything yet)'
    Okay what this is supposed to do is
    1: take the first Floor in an array
    2: check to see if the player is above it (does not have to be touching it)
    3: if player is above the platform, set
    wilfordAbove
    to
    true
    for that platform
    4: if not, wilfordAbove is set to
    false
    5: then take the next Floor, and repeat.

    so let's take 3 Floors (only concerning location and size):
    f1 = new floor (20, 50, 100, 30);
    f2 = new floor (40, 70, 70, 30);
    f1 = new floor (125, 50, 100, 30);


    so, if the player's position is (43,35) then:
    f1.wilfordAbove = true
    f2.wilfordAbove = true
    f3.wilfordAbove = false

    because f1's and f2's x "boundaries" overlap, and the player is above the overlapping area.

    and, if the player's position is (1,35) then (assuming the width of the player < 39):
    f1.wilfordAbove = true
    f2.wilfordAbove = false
    f3.wilfordAbove = false

    because the player is only above f1

    but, instead, what happens is:
    if the player's position is (130,35) then:
    f1.wilfordAbove = true
    f2.wilfordAbove = true
    f3.wilfordAbove = true

    because the player is above the last platform in the array, and it for some reason changes ALL of the platforms' wilfordAbove field to whatever the last platform's wilfordAbove field should be.
  • 7. Re: help with side-scrolling platformer
    796262 Newbie
    Currently Being Moderated
    Ah. That's because your wilfordAbove and wilFordBelow variables are static. All your platforms share one instance of those variables. Pretty sure that's not what you want.
  • 8. Re: help with side-scrolling platformer
    843853 Newbie
    Currently Being Moderated
    ...... Wow, i can't believe it was that easy. it works now. I spent at least an entire class period trying to figure out what was wrong. LOL
  • 9. Re: help with side-scrolling platformer
    gimbal2 Guru
    Currently Being Moderated
    ZoraKirby wrote:
    ...... Wow, i can't believe it was that easy. it works now. I spent at least an entire class period trying to figure out what was wrong. LOL
    Be assured, you are not the only one. Many people fall for the static trap when they first start out with Java. It should be a banned topic until you are familiar with the language in my opinion.