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

2017/3/26

Don`t use the Middlepoint of the Player while hitting the wall

Malmi Malmi

2017/3/26

#
So I have a Problem with my Player. I`m kinda new in Greenfoot. So let me get to the point. The Problem is that when I`m hitting a Wall my Player is moving half inside of the Wall- So now i got the question how can I change my QC to get the fullImage of my Player.
public boolean Spieler()
   {
       int SpHöhe = getImage().getHeight()+15;
       int SpBreite = getImage().getWidth()+15;
       return true;
   }
   public boolean Wand()
    {
        Object Spieler = getOneObjectAtOffset(getImage().getWidth()-30, getImage().getHeight()-30, Wand.class);
        return Spieler != null;
        
    }
   public void checkWandOben()
    {
        if (Wand())
        {
            setLocation(getX(), getY()+2*speed);
        }
        } 
   public void checkWandUnten()
    {
        if (Wand())
        {
            setLocation(getX(), getY()-2*speed); 
        }
        } 
   public void checkWandRechts()
    {
        if (Wand())
        {
            setLocation(getX()+2*speed, getY());
        }
        } 
   public void checkWandLinks()
    {
        if (Wand())
        {
            setLocation(getX()-2*speed, getY());
        }
        } 
   public void Finalcheck()
    {
        if (Greenfoot.isKeyDown("a"))
        {
            checkWandRechts();
        }
        if (Greenfoot.isKeyDown("w"))
        {
            checkWandOben();
        }
        if (Greenfoot.isKeyDown("d"))
        {
            checkWandLinks();
        }
        if (Greenfoot.isKeyDown("s"))
        {
           checkWandUnten();
        }
    }  
Super_Hippo Super_Hippo

2017/3/26

#
Try to use this:
public void act()
{
    int dx=0, dy=0;
    if (Greenfoot.isKeyDown("a")) dx--; //not sure if right and left was different on purpose or by accident
    if (Greenfoot.isKeyDown("d")) dx++;
    if (Greenfoot.isKeyDown("w")) dy--;
    if (Greenfoot.isKeyDown("s")) dy++;
    if (dx != 0 || dy != 0)
    {
        setLocation(getX()+dx*speed, getY()+dy*speed);
        if (isTouching(Wand.class)) //not why why you are checking for a Spieler. Is the Spieler the Wall?
        {
            setLocation(getX()-dx*speed, getY()-dy*speed);
        }
    }
}
Malmi Malmi

2017/3/26

#
Spieler = Player. That all is written in the Player(Spieler) class
Super_Hippo Super_Hippo

2017/3/26

#
Then you don't have to check for an intersecting player, but for an intersecting wall. Well, does the code I gave you work?
Malmi Malmi

2017/3/26

#
I got it now thx
You need to login to post a reply.