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/19

#
What must I write that an actor can not go through an other actor? I have no Idee what I must write that an actor can not go through an other actor.
Nosson1459 Nosson1459

2017/4/19

#
if(!isTouching(Actor.class/*name of actor to not go thru*/))
{
    //put your movement code here
}   
Timon Timon

2017/4/20

#
It dont worked.
Timon Timon

2017/4/20

#
 public void blocade()
    {
        if(!isTouching(Boxes.class))
        {
            Move(0)
        }

        
    }
Timon Timon

2017/4/20

#
What is wrong??
danpost danpost

2017/4/20

#
Timon wrote...
What is wrong??
Please show the entire class code so that we can determine how to fix it.
Timon Timon

2017/4/20

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class Mans extends Actor
{
    /**
     * Act - do whatever the Mans wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        this.jump();
        this.blocade();
    }   

    public int ySpeed;
    public int groundLevel=489;
    private boolean pressed = false;
    public void jump()
    {
        boolean onGround = (getY() == groundLevel);
        if (!onGround)
        {
            ySpeed++;
            setLocation(getX(), getY()+ySpeed);

            if (getY()==groundLevel)
            {
                setLocation(getX(), groundLevel);
            }
        }
        else
        {
            if (Greenfoot.isKeyDown("up"))
            {
                ySpeed = -15;
                setLocation(getX(), getY()+ySpeed);
            }
        }
       
        if(this.pressed == Greenfoot.isKeyDown("space"))
        {

            this.pressed =! this.pressed;
            if(this.pressed == true)
            {

                this.getWorld().addObject(new Weapons(), this.getX() + 80, this.getY()-25 ); 
            }

        } 
    }

    public void blocade()
    {
        if(!isTouching(Boxes.class))
       {
           move(0);
       }

    }
}
danpost danpost

2017/4/20

#
First thing is that lines 27 through 30 do not do a thing (if at ground level, set at ground level?). Better would be to set the actor at ground level if below ground level. Change the '==' in line 27 to '>'. Next thing is you currently only have vertical movement for the actor, so the blockade will therefore either be hit on the underside or landed on. Since no information on what you are aiming at is given, I will only give what is needed for hitting the underside of a blockade (you are calling it a blockade and only have vertical movement). Change your blocade method to this:
public void blocade()
{
    Actor box = getOneIntersectingObject(Boxes.class);
    if (box != null)
    {
        setLocation(getX(), box.getY()+(box.getImage().getHeight()+this.getImage().getHeight())/2+1);
        ySpeed = 0;
}
Timon Timon

2017/4/20

#
I have done it, but when I touch the Boxes class I fall down under the Boxes
danpost danpost

2017/4/20

#
Timon wrote...
I have done it, but when I touch the Boxes class I fall down under the Boxes
Is that not what you wanted? If the actor does not move horizontally and only jumps, then the blockade must be above it and therefore, the actor will hit the underside and fall back to ground level.
Timon Timon

2017/4/20

#
No I want to jump on the Boxes that I can jump from on Box to the other.
danpost danpost

2017/4/20

#
Timon wrote...
No I want to jump on the Boxes that I can jump from on Box to the other.
So, you want to be able to jump up through the box and then land on it?
Timon Timon

2017/4/20

#
Yes
Timon Timon

2017/4/20

#
No not through it. I want to jump from the site on it
danpost danpost

2017/4/20

#
Timon wrote...
No not through it. I want to jump from the site on it
You will never land on it if the actor cannot move horizontally.
There are more replies on the next page.
1
2
3