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

2016/1/24

problem with ObjectAtOffset

Talrasha Talrasha

2016/1/24

#
Hello I am programming a little Bomberman right now and my character shall put a bomb on he place he stands is allowed to move off it ofcourse but musn't move on it again, i wanted to solve this with getOneObjectAtOffset, but it seems the method doe'snt recognize the Bomb.
if(Greenfoot.isKeyDown("right")) // checks if right key is pressed
       {
            setLocation(getX()+1,getY());
            Actor wand = getOneIntersectingObject(wall.class);
            if (wand != null)
            {
                setLocation(getX()-1,getY());
            }
            Actor bombe = getOneObjectAtOffset(getX()+60,getY(),Bomb.class);
            if (bombe!=null)
            {
                setLocation(getX()-1,getY());
            }
       }
thats the example when i move to the right, but when there is a bomb i move trhough it
danpost danpost

2016/1/25

#
This code snippet does not appear to have any noticeable issues. Perhaps showing the entire class may be more revealing.
Talrasha Talrasha

2016/1/25

#
the class is quite long and don't get distrackted by the german words.
public class Spieler1 extends Spieler
{
    int Bombitemcounter;
    int Radiusitemcounter;
    boolean gedrückt;
    long lastAdded = System.currentTimeMillis();
    int xlegeposition;
    int temp;
    boolean bombground;
    boolean gelegt;
    Bomb letzte_bombe;
    public Spieler1()
    {
        Bombitemcounter=1;
        gedrückt = false ;
        gelegt = false;
        bombground = false;
    }
    
    public void act() 
    {
        Bombe_legen();
        //bombcollider();
        check_keys_spieler_1();
        Itemcollider();
    }    
    
    void check_keys_spieler_1()
    {
       if(Greenfoot.isKeyDown("right")) // checks if right key is pressed
       {
            setLocation(getX()+1,getY());
            Actor wand = getOneIntersectingObject(wall.class);
            if (wand != null)
            {
                setLocation(getX()-1,getY());
            }
            Actor bombe = getOneObjectAtOffset(getX()+60,getY(),Bomb.class);
            if (bombe!=null)
            {
                setLocation(getX()-1,getY());
            }
       }
       
       else if(Greenfoot.isKeyDown("down")) // checks if down key is pressed
        {
            setLocation(getX(),getY()+1);
            Actor wand = getOneIntersectingObject(wall.class);
            if (wand != null)
            {
                setLocation(getX(),getY()-1);
            }
            Actor bombe = getOneObjectAtOffset(getX(),getY()+60,Bomb.class);
            if (bombe!=null)
            {
                setLocation(getX(),getY()-1);
            }
            if(bombground == true&&bombe!=null)
            {
                
            }
        }
        else if(Greenfoot.isKeyDown("left")) // checks if left key is pressed
        {
            setLocation(getX()-1,getY());
            Actor wand = getOneIntersectingObject(wall.class);
            if (wand != null)
            {
                setLocation(getX()+1,getY());
            }
            Actor bombe = getOneObjectAtOffset(getX()-60,getY(),Bomb.class);
            if (bombe!=null)
            {
                setLocation(getX()+1,getY());
            }
            if(bombground == true&&bombe!=null)
            {
                
            }
        }
        else if(Greenfoot.isKeyDown("up")) // checks if up key is pressed
        {
            setLocation(getX(),getY()-1);
            Actor wand = getOneIntersectingObject(wall.class);
            if (wand != null)
            {
                setLocation(getX(),getY()+1);
            }
            Actor bombe = getOneObjectAtOffset(getX(),getY()-60,Bomb.class);
            if (bombe!=null)
            {
                setLocation(getX(),getY()+1);
            }
            if(bombground == true&&bombe!=null)
            {
                
            }
        } 
    }
    
    void Bombe_legen()
    {
        WORLD f = (WORLD)getWorld();
        if (Greenfoot.isKeyDown("space")&& Bombitemcounter > f.Bombenzähler)
        {
            if (gedrückt == false)
            {
                Bomb bomb1 = new Bomb();
                f.Bombenzähler = f.Bombenzähler +1;
                for(int i=1;i<18;i++)
                {
                    if (getX()<60*i)
                    {
                        temp=i;
                        i=18;
                    }
                    xlegeposition=60*temp-30;
                }
                for(int j=1;j<12;j++)
                {
                    if(getY()<60*j)
                    {
                        letzte_bombe = bomb1;
                        getWorld().addObject(bomb1,xlegeposition,60*j-30);
                        gedrückt = true;
                        bombground = true;
                        gelegt = true;
                        j=12;
                    }
                }
            }
        }
        else
        {
            gedrückt = false ;
        }
        
    }
    
    void Itemcollider()
    {
        Bomben_zählen();
        radius_erhöhen();
    }
    
    void Bomben_zählen()
    {
        Actor Bomb_item = getOneIntersectingObject(Bombitem.class);
        if (Bomb_item != null)
        {
            Bombitemcounter = Bombitemcounter+1;
        }
    }
    
    void radius_erhöhen()
    {
        Actor Radius_item = getOneIntersectingObject(radiusitem.class);
        if (Radius_item != null)
        {
            Radiusitemcounter = Radiusitemcounter+1;
        }
    }
    
    void bombcollider()
    {
        if (gelegt == true)
        {
            Actor bombe =getOneIntersectingObject(Bomb.class);
            //if (bombe == null)
            {
                if (bombe !=letzte_bombe)
                {
                    bombground = false;
                    gelegt = false;
                }
            }
        }
    }
    
}
here we go
danpost danpost

2016/1/25

#
It might be better to place the bomb at the exact location of the player. Then, check for intersection with a bomb before and after moving. Allow movement for all situations except when not intersecting prior to and intersecting after moving.
ttg4l ttg4l

2016/1/25

#
I think the bit needs to be removed for it to work
ttg4l ttg4l

2016/1/25

#
ttg4l wrote...
I think the bit needs to be removed for it to work
I wrote that
Talrasha Talrasha

2016/1/25

#
I found my mistake. @ Danpost, i guess you already explained the solution in another discussion. The problem was, that objectatoffset calculates the distance from the player itself to the Object and not from (0;0), thats why i need to change getX() and getY() to certain number, means the Distance to the Bomb from the player. Thank you anyways for help :)
danpost danpost

2016/1/25

#
Talrasha wrote...
I found my mistake. @ Danpost, i guess you already explained the solution in another discussion. The problem was, that objectatoffset calculates the distance from the player itself to the Object and not from (0;0), thats why i need to change getX() and getY() to certain number, means the Distance to the Bomb from the player. Thank you anyways for help :)
Actually, that was something I had overlooked. Glad you found it.
You need to login to post a reply.