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

2015/1/12

Two final bugs with game - help needed

1
2
Dillybar Dillybar

2015/1/12

#
We have nearly finished our game! We have two big errors that we can't seem to fix and were hoping the community would be able to help us. Error One Our Object at Offset code is a bit weird when you try to jump on enemies, on some enemies it won't work at all, even though they have the same code. Error Two When mario attempts to jump on the horizontal moving platform (A platform that moves horizontally on the screen), he jumps forward a large amount. This only occurs as soon mario jumps on the platform and will only happen if he is moving when he lands on the platform. Here is a link to my scenario - I have attached the source code Another link in case that one doesn't work: http://www.greenfoot.org/scenarios/13053 Thanks for any input that you have
danpost danpost

2015/1/12

#
No code available to work with.
Dillybar Dillybar

2015/1/12

#
My apologies, here is the relevant code: Error One The code to remove mario from the world when hit on the left or right:
if (getOneObjectAtOffset (-getImage().getWidth()/2, 0, Enemies.class)!= null && invincibilityDelayCount >= invincibilityTime)
        {
            SWorld sWorld = (SWorld)getWorld();
            sWorld.marioIsSmall();
            sWorld.decreaseLives();
            gotHit = true;
        }
        if (getOneObjectAtOffset (getImage().getWidth()/2, 0, Enemies.class)!= null && invincibilityDelayCount >= invincibilityTime)
        {
            SWorld sWorld = (SWorld)getWorld();
            sWorld.marioIsSmall();
            sWorld.decreaseLives();
            gotHit = true;
        }
(In mario.class) The code to remove the enemy when it hits mario:
if (getOneObjectAtOffset (0, getImage().getHeight()/2-3, MarioS.class)!= null)
        {
            imminentDeath = true;
        }
        if (imminentDeath == true)
        {
            getWorld().removeObject(this);
        }
(In the enemy's .class) I think the error could possibly be in that the code is in different classes Error Two Here is the code for moving the platform (had to put a delay count of two on the moving platform because it seemed like mario would move half as often)
if (horpLeft == true && moveDelayCount == moveDelayTime)
        {
            move(-1);
            moveDelayCount = 0;
        }
        if (horpLeft == false && moveDelayCount == moveDelayTime)
        {
            move(1);
            moveDelayCount = 0;
        }
and finally, here is the code to make mario move
ySpeed ++;
onGround = false;
setLocation(getX()+xSpeed, getY()+ySpeed/2);
while (getOneObjectAtOffset (0, getImage().getHeight()/2-4, HorPlatform.class)!= null)
        {
            onGround = true;
            ySpeed = 0;
            terrainLeft = ((HorPlatform) getOneIntersectingObject(HorPlatform.class)).getLeft();
            if (terrainLeft == true)
            {
                xSpeed = -1;
            }
            if (terrainLeft == false)
            {
                xSpeed = 1;
            }
            if (terrainLeft == true && Greenfoot.isKeyDown("a"))
            {
                xSpeed = -6;
            }
            if (terrainLeft == true && Greenfoot.isKeyDown("d"))
            {
                xSpeed = 4;
            }
            if (terrainLeft == false && Greenfoot.isKeyDown("a"))
            {
                xSpeed = -4;
            }
            if (terrainLeft == false && Greenfoot.isKeyDown("d"))
            {
                xSpeed = 6;
            }
            setLocation(getX()+xSpeed, getY()-1);
        }
if (getOneObjectAtOffset (0, getImage().getHeight()/2-4, HorPlatform.class)== null)
        {
            if (Greenfoot.isKeyDown("d"))
            {
                xSpeed = 5;
                SWorld sWorld = (SWorld)getWorld();
                sWorld.marioL = false;
            }
            if (Greenfoot.isKeyDown("a"))
            {
                xSpeed = -5;
                SWorld sWorld = (SWorld)getWorld();
                sWorld.marioL = true;
            }
            if (!Greenfoot.isKeyDown("a") && !Greenfoot.isKeyDown("d"))
            {
                xSpeed = 0;
            }
        }
        if (Greenfoot.isKeyDown("w"))
        {
            jump();
        }
Thank you so much for your help! If you need any more code, let me know
danpost danpost

2015/1/13

#
For error 1: your collision detection will only work if the image of the other actor is in line with the center of the actor horizontally or vertically; in other words, if the images of both actors were of size 50x50, you could have one at (100, 100) and the other at (126, 126), where almost one-fourth of the images are intersecting, and the collision will not be detected. For error 2: line 33 of the movement code for Mario is moving Mario horizontally while trying to get him from being sunk into (rise him out of) a platform.
Dillybar Dillybar

2015/1/13

#
Ah I see, how could i improve my code for error 1? I didn't realize that that was part of the code. For error 2, I had to do that in order to make mario move along with the platform.
danpost danpost

2015/1/13

#
Error 1: I prefer to do the vertical movement separate from the horizontal movement and then check collision after each one. That way, depending on the direction of movement, you know were the intersecting object is without having to test each possibility. Error 2: since it is the platform that is making Mario move when standing on it, have the platform more Mario.
Dillybar Dillybar

2015/1/13

#
So, do all the horizontal movements then all the horizontal checks (in the mario class or can the checks be split up?) then do all the vertical movements and the vertical checks
danpost danpost

2015/1/13

#
I am not sure what you mean by 'all horizontal movements'. You should only have one (in the Mario class) based on keys down. Also, I prefer to do vertical movement/collision checking before horizontal. You can review the code of several of my scenario. I have a Jump and Run Demo w/Moving Platform. Just run the scenario and click the buttons along the bottom to view the codes of the 'Who' (main actor) and 'What' (platform) classes.
Dillybar Dillybar

2015/1/13

#
So I tried to fix the platforms by making them move mario as you recommended- however, mario simply slides off when mario lands on the platform. Here is where I call mario's movement:
while (getOneObjectAtOffset (0, -getImage().getHeight()/2+1, MarioS.class)!= null)
        {
            MarioS marioS = (MarioS) getWorld().getObjects(MarioS.class).get(0);
            marioS.moveWithPlatform(horpLeft);
        }
and here is how mario moves
public void moveWithPlatform (boolean horpLeft)
    {
        if (horpLeft == true && Greenfoot.isKeyDown("left"))
        {
            xSpeed = -6;
        }
        else if (horpLeft == true && Greenfoot.isKeyDown("right"))
        {
            xSpeed = 4;
        }
        else
        {
            xSpeed = -1;
        }
        if (horpLeft == false && Greenfoot.isKeyDown("left"))
        {
            xSpeed = -4;
        }
        else if (horpLeft == false && Greenfoot.isKeyDown("right"))
        {
            xSpeed = 6;
        }
        else
        {
            xSpeed = 1;
        }
        setLocation(getX() +xSpeed, getY());
    }
Dillybar Dillybar

2015/1/13

#
I'm trying to open your jump/run demo but it won't work in my browser with java :$. I'll try to figure something out Edit: Got it, looking at the code now
Dillybar Dillybar

2015/1/13

#
Okay so i removed the old code and replaced it with this in the horizontal platform class, however it still does not work. Here is the new code:
private void moveMario()
    {
        Actor marioS = getOneObjectAtOffset (0, -getImage().getHeight()/2+1, MarioS.class);
        while (marioS != null)
        {
            marioS.setLocation(getX()+1, getY());
        }
    }
Dillybar Dillybar

2015/1/13

#
I seem to be struggling with this :$. Mario moves fine but will not kill enemies when he jumps on them. Here is my code for killing the enemies:
Actor ghoomba = getOneObjectAtOffset (0, -getImage().getHeight()/2+1, Ghoomba.class);
        Actor hammerbro = getOneObjectAtOffset (0, -getImage().getHeight()/2+1, HammerBro.class);
        Actor koopakid = getOneObjectAtOffset (0, -getImage().getHeight()/2+1, KoopaKid.class);
        Actor magikoopa = getOneObjectAtOffset (0, -getImage().getHeight()/2+1, MagiKoopa.class);
        if(ghoomba != null)
        {
            getWorld().removeObject(ghoomba);
        }
        if(hammerbro != null)
        {
            getWorld().removeObject(hammerbro);
        }
        if(koopakid != null)
        {
            getWorld().removeObject(koopakid);
        }
        if(magikoopa != null)
        {
            getWorld().removeObject(magikoopa);
        }
This is called before the code to remove mario
danpost danpost

2015/1/13

#
Dillybar wrote...
Okay so i removed the old code and replaced it with this in the horizontal platform class, however it still does not work. Here is the new code:
private void moveMario()
    {
        Actor marioS = getOneObjectAtOffset (0, -getImage().getHeight()/2+1, MarioS.class);
        while (marioS != null)
        {
            marioS.setLocation(getX()+1, getY());
        }
    }
It is no wonder this will not work. If marioS is not null going into the 'while' loop and it has no chance of changing within the loop, then the loop will never be exited (until you 'terminate' the program or force close it). You need to reset the value (re-check collision) within the while loop so that eventually 'marioS' will at some point become 'null' and the loop can be exited. Why are you looking above Mario for actors that he is to jump on?
Dillybar Dillybar

2015/1/13

#
Ah, apparently I was stupid last night. Error one is fixed, but I am still a little confused about Error 2. When mario lands on the platform, he either slides off or the game freezes (as you said it would). I tried adding an extra while loop but it did not seemed to work
Dillybar Dillybar

2015/1/13

#
Still looking for a bit of help on error 2 :$. If you have any ideas, I'd be happy to take them.
There are more replies on the next page.
1
2