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

2019/5/15

My Actor does not rebound on the corner

EMRX EMRX

2019/5/15

#
Hello, my Actor does not rebound on the corner. He rebounds with his top on the ceiling, but he jumps through the ceiling at the corners. I would be grateful for your help. My Code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Feuerlinks here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Feuer extends Actor
{
    /**
     * Act - do whatever the Feuerlinks wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    private int hspeed=6;
    private int vspeed=0;
    private int beschleunigung = 2;
    private int vmax=8;
    private boolean Sprungtaste;

    private GreenfootImage li1 = new GreenfootImage("Feuer-links1.png");
    private GreenfootImage li2 = new GreenfootImage("Feuer-links2.png");
    private GreenfootImage li3 = new GreenfootImage("Feuer-links3.png");
    private GreenfootImage li4 = new GreenfootImage("Feuer-links4.png");
    private GreenfootImage li5 = new GreenfootImage("Feuer-links.png");

    private GreenfootImage re1 = new GreenfootImage("Feuer-rechts1.png");
    private GreenfootImage re2 = new GreenfootImage("Feuer-rechts2.png");
    private GreenfootImage re3 = new GreenfootImage("Feuer-rechts3.png");
    private GreenfootImage re4 = new GreenfootImage("Feuer-rechts4.png");
    private GreenfootImage re5 = new GreenfootImage("Feuer-rechts.png");

    private int frame = 1 ;
    private int counter = 0;

    private int richtung=0;

    public void moveRight()
    {
        setLocation (getX() + hspeed, getY());
    }

    public void moveLeft()
    {
        setLocation (getX() - hspeed, getY());
    }

    public void fall()
    {
        setLocation (getX(), getY() + vspeed);
        vspeed= vspeed + beschleunigung;
    }

    public boolean unten()
    {

        Actor unten = getOneObjectAtOffset (0,(getImage().getHeight() / 2), abstossen.class);
        return unten != null;
    }

    public void sprung()
    {
        vspeed = -25;
        fall();

    }

    public void Animationlinks()
    {

        if (richtung == 0)
        {
            if(frame == 1)
            {
                setImage(li1);
            }
            else if(frame == 2)
            {
                setImage(li2);
            }
            else if(frame == 3)
            {
                setImage(li3);
            }
            else if(frame == 4)
            {
                setImage(li4);
            }
            else if(frame == 5)
            {
                setImage(li5);
                frame = -15;
                return;
            }

            frame ++;
        }
    }

    public void Animationrechts()
    {
        if (richtung == 1)
        {
            if(frame == 1)
            {
                setImage(re1);
            }
            else if(frame == 2)
            {
                setImage(re2);
            }
            else if(frame == 3)
            {
                setImage(re3);
            }
            else if(frame == 4)
            {
                setImage(re4);
            }
            else if(frame == 5)
            {
                setImage(re5);
                frame = -15;
                return;
            }

            frame ++;

        }
    }

    public void act() 
    {
        counter ++;

        if (Greenfoot.isKeyDown("left"))
        {

            {  Actor links = getOneObjectAtOffset (-1*((getImage().getWidth()/2)),1,abstossen.class);
                if (links != null)
                {

                }
                else
                {
                    setImage("Feuer-linkse.png");
                    moveLeft();
                    richtung=0;

                }
            }
        }
        if (Greenfoot.isKeyDown("right"))
        {

            {  Actor rechts = getOneObjectAtOffset ((getImage().getWidth()/2), 0 , abstossen.class);
                if (rechts != null)
                {

                }
                else
                {
                    setImage("Feuer-rechtse.png");
                    moveRight();
                    richtung=1;
                }
            }
        }
        if (unten())
        {
            vspeed=0;

        }
        else{
            fall();
        }

        if (vspeed > vmax)
        {
            vspeed=vmax;
        }
        if (Sprungtaste != Greenfoot.isKeyDown("up"))
        {
            Sprungtaste = ! Sprungtaste;
            if (Sprungtaste && unten())
            {
                sprung();
            }
        }

        //gegen die Decke sto├čen
        Actor oben = getOneObjectAtOffset (0, -1*(getImage().getHeight()/2), abstossen.class);
        if (oben != null)
        {
            vspeed=0;
            fall();
        }
        else
        {

        }

        if (richtung==0)
        {
            if (counter % 4 == 0)
            {
                Animationlinks();
            }
        }
        if (richtung==1)
        {
            if (counter % 4 == 0)
            {
                Animationrechts();
            }
        }    

    } 
}
An Image:
danpost danpost

2019/5/15

#
Your unten method, as well as oben in your act method, only looks for an object the is not touching the vertical through the center of your actor. So, as long as an object does not pass at least half way across the actor, vertical motion is permitted. A better way to code collision detection is to do an unrestricted move, then check for any intersecting obstacles (using getIntersectingObjects). If one is found, just move back off it. The end result will be what is shown on the next frame greenfoot displays.
EMRX EMRX

4 days ago

#
Thank you, I solved it by creating transparent pixels there and when intersecting, it moves back off it.
You need to login to post a reply.