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

2013/11/22

Going through an object on the side while it shouldn't

1
2
Foppen Foppen

2013/11/22

#
In my game I got the actor called Pim and an object called Balk. The balk is meant to jump on, so you can get higher in the game (look at it like doodle jump, but instead of jumping you just stand on it.) The problem is, that if you want to jump on it, and you are too close to the object, you go in it through the side: http://i.imgur.com/3beuvPO.png When you want to jump on it and you hit the side, you should bump off instead of going into it. Here are some codes I have: This is my class called "beweging" (translated it means movement)
public class beweging extends Actor
{
    public void act() 
    {
        // No action required
    }
    
    public int vSpeed = 1;  
    public int acceleration = 1;  
    public int jumpSpeed = 12;
    public static final int speed = 5;            
    
    public void moveRight()
    {
        setLocation ( getX() + speed, getY() );
    }
    
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY() );
    }
    
    public boolean onGround()
    {
        Object under = getOneObjectAtOffset(0, getImage().getHeight()/2 , null);
        return under != null;
    }
    
    public void setVSpeed(int speed)
    {
        vSpeed = speed;
    }

    public void fall()
    {
        vSpeed+=1; // add gravity
        int dir=(int)Math.signum(vSpeed); // determine direction
        for(int step=0; step!=vSpeed; step+=dir) // for each pixel-step
        {
            setLocation(getX(), getY()+dir); // move 
            if(getOneIntersectingObject(null)!=null) // check intersection
            {
                setLocation(getX(), getY()-dir); // resistance (step-back)
                vSpeed=0; // stopped
                break; // forces exit out of 'for' loop
            }
        }
    }
}
In the class pim I use the current function to check if he is falling down:
private void checkFall()
    {
        if (onGround()) 
        {
            setVSpeed(0);
        }
        else
        {
            fall();
        }
    }
danpost danpost

2013/11/22

#
It does not appear that you have included any code for horizontal movement or collision detection at all (although, I do see method for horizontal movement -- 'moveLeft' and 'moveRight'). Also, you were not very clear as to whether 'bewiging' was a superclass to other classes or not and, if so, which.
Foppen Foppen

2013/11/22

#
moveRight and moveLeft are used for the movement. they work with the left and right key and I indeed don't have anything yet to detect the "balk". I do know how to do it, but after that I don't know how to continue. Here is an overview from all the classes: http://i.imgur.com/XGwShfl.png I started with greenfoot a few days ago, so most things I use are things from tutorials or discussions posted on this website.
danpost danpost

2013/11/22

#
If you project compiles you can use the 'Share' button (near top-right of window) to upload it on this site. Use the 'Publish' tab (along the top of the new 'Share' window and be sure to check the 'Publish source code' checkbox.
Foppen Foppen

2013/11/24

#
I added this in the moveRight and moveLeft
if(getOneIntersectingObject(balk.class) != null)
            {
                    if (onGround())
                    {}
                    else
                    {
                        setLocation ( getX() + 10, getY() );
                    }
            }
The only problem left is that now on the edges, it als stops. If I think about it I should somehow "extend the image width" before it uses setLocation ( getX() + 10, getY() ); but how do I do this?
danpost danpost

2013/11/24

#
Extending the image width is (I am sure) not what you need to do. However, since I am not exactly sure why you wanted to do that (and what behavior you want), I cannot say how to fix it.
Foppen Foppen

2013/11/24

#
When my actor Pim is not on the ground and it meets the balk.class, instead of going in the balk, it now set's the direction to the other side. jump left --> meets object balk --> goes 10 pixels to the right so it doesn't go inside the object. The above things only happens when "pim" is not on the ground. When I stand on it and I walk to the edges of the balk, it also wants to go to the other direction, since at the end of the balk, the ground stops http://i.imgur.com/lWMgoDk.png
danpost danpost

2013/11/24

#
I think you will have to show the entire class again; as it is now.
Foppen Foppen

2013/11/24

#
Pim
private void checkKeys()
    {

if (Greenfoot.isKeyDown("left") )
       {
            moveLeft();
            if(getOneIntersectingObject(balk.class) != null)
            {
                    if (onGround())
                    {}
                    else 
                    {
                            setLocation ( getX() + 10, getY() );
                    }        
            }
       }
       
if (Greenfoot.isKeyDown("right") )
       {
            moveRight();
            if(getOneIntersectingObject(balk.class) != null)
            {
                    if (onGround())
                    {}
                    else 
                    {
                            setLocation ( getX() - 10, getY() );
                    }  
            }
            
       }

if (Greenfoot.isKeyDown("up") )
        {
            if (onGround())
            {
                if (jumpTime >= jumpTimeMax)
                {
                    jump();
                    jumpTime = 0;
                }
            }
        }

    }
Beweging
public void moveRight()
    {
        setLocation ( getX() + speed, getY() );
    }
    
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY() );
    }
    
    public boolean onGround()
    {
        Object under = getOneObjectAtOffset(0, getImage().getHeight()/2 , null);
        return under != null;
    }
danpost danpost

2013/11/25

#
I do not understand why the Pim object need be on the ground for intersections to be valid; that is, the condition that the actor is on the ground should not limit whether the balk object is 'visible' to that actor.
Foppen Foppen

2013/11/25

#
When I don't add the onGround in the movement
moveRight();  
            if(getOneIntersectingObject(balk.class) != null)  
            {  
                    if (onGround())  
                    {}  
                    else   
                    {  
                            setLocation ( getX() - 10, getY() );  
                    }    
            }
as soon as he stands on "balk" and you want to move, it does setLocation ( getX() - 10, getY() ); (so it can't move at all since it gets moved the opposite side)
danpost danpost

2013/11/25

#
Change code to:
int balkCount = getIntersectingObjects(balk.class).size();
moveRight();
if (getIntersectingObjects(balk.class).size() > balkCount) moveLeft();
Foppen Foppen

2013/11/25

#
With the code you gave me we're back to the first problem I had http://i.imgur.com/OSEZeeR.png
danpost danpost

2013/11/25

#
danpost wrote...
If you project compiles you can use the 'Share' button (near top-right of window) to upload it on this site. Use the 'Publish' tab (along the top of the new 'Share' window and be sure to check the 'Publish source code' checkbox.
Foppen Foppen

2013/11/25

#
http://www.greenfoot.org/scenarios/10080
There are more replies on the next page.
1
2