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

2017/4/19

What must I write that an actor can not go through an other actor?

1
2
3
Timon Timon

2017/4/20

#
NO he will, because the boxes move to him.
danpost danpost

2017/4/20

#
Timon wrote...
NO he will, because the boxes move to him.
Oh, okay. Then try this instead:
public void blocade()
{
    Actor box = getOneIntersectingObject(Boxes.class);
    if (box != null)
    {
        int yDir = (int)Math.signum(ySpeed);
        setLocation(getX(), box.getY()-yDir*((box.getImage().getHeight()+this.getImage().getHeight())/2+1));
        ySpeed = 0;
}
I fear this will mess with your on ground code at the beginning of the act method, unless you are not allowed to jump while on a box.
Timon Timon

2017/4/20

#
It worked, but I cant jump anyway. The actor jumps alone.
Timon Timon

2017/4/20

#
What must I Chance in the on ground code that I jump when I press "up" and that I can jump when I am on a Box.
danpost danpost

2017/4/20

#
Timon wrote...
What must I Chance in the on ground code that I jump when I press "up" and that I can jump when I am on a Box.
We would have to re-arrange the whole class code because now you have two different conditions for jumping and we need to know not only is the actor touching a box, but is the actor standing on a box. About as simple as it gets is something like this:
import greenfoot.*;

public class Mans extends Actor
{
    public int ySpeed;
    public int groundLevel=489;
    private boolean pressed;
     
    public void act()
    {
        boolean onGround = false;
        ySpeed++;
        setLocation(getX(), getY()+ySpeed);
        // at ground level
        if (getY() == groundLevel)
        {
            setLocation(getX(), groundLevel);
            onGround = true;
        }
        // hitting box
        Actor box = getOneIntersectingObject(Boxes.class);
        if (box != null)
        {
            int yDir = (int)Math.signum(ySpeed);
            setLocation(getX(), box.getY()-yDir*((box.getImage().getHeight()+getImage().getHeight())/2+1));
            ySpeed = 0;
            if (yDir > 0) onGround = true;
        }
        // jumping
        if (onGround && Greenfoot.isKeyDown("up")) ySpeed = -15; 
        // firing weapon       
        if (pressed == Greenfoot.isKeyDown("space"))
        {
            pressed = !pressed;
            if (pressed) getWorld().addObject(new Weapons(), getX()+80, getY()-25); 
        } 
    }
}
Timon Timon

2017/4/20

#
When I press up nothing is hapen and the man(the actor) is on the bottom of the Grenfootscreen.
Timon Timon

2017/4/20

#
I only can jump when I am on a box
Timon Timon

2017/4/20

#
When the box move to me the man jump from his self.
danpost danpost

2017/4/20

#
I carried your mistake across when I copied the main code. In line 15, change '==' to '>'.
Timon Timon

2017/4/21

#
I changed it to > but now when I press nothing he jumps from alone. What must I write that he jumps only when I press "up"
danpost danpost

2017/4/21

#
Timon wrote...
I changed it to > but now when I press nothing he jumps from alone. What must I write that he jumps only when I press "up"
Please show your current code.
Timon Timon

2017/4/21

#
import greenfoot.*;
public class Mans extends Actor
{
    public int ySpeed;
    public int groundLevel=489;
    private boolean pressed;

    public void act()
    {
        boolean onGround = false;
        ySpeed++;
        setLocation(getX(), getY()+ySpeed);
        // at ground level
        if (getY() > groundLevel)
        {
            setLocation(getX(), groundLevel);
            onGround = true;
        }
        // hitting box
        Actor box = getOneIntersectingObject(Boxes.class);
        if (box != null)
        {
            int yDir = (int)Math.signum(ySpeed);
            setLocation(getX(), box.getY()-yDir*((box.getImage().getHeight()+getImage().getHeight())/2+1));
            ySpeed = 0;
            if (yDir > 0) onGround = true;
        }
        // jumping
        if (onGround && Greenfoot.isKeyDown("up")) ySpeed = -15; 
        // firing weapon       
        if (pressed == Greenfoot.isKeyDown("space"))
        {
            pressed = !pressed;
            if (pressed) getWorld().addObject(new Weapons(), getX()+80, getY()-25); 
        } 
    }
}
danpost danpost

2017/4/21

#
Insert the following line after line 15:
ySpeed = 0;
I am not sure that will fix your current issue however. Please inform.
Timon Timon

2017/4/21

#
No. Now when I press no bottom and a box came he is going underthe box and after the box he Comes up.
Timon Timon

2017/4/21

#
Sorry for my bad english
There are more replies on the next page.
1
2
3