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

2019/12/18

getOneIntersectingObject problem

Fuph Fuph

2019/12/18

#
Hey guys, I have a problem with this method. I created a little scenario about the peanuts and Snoopy can shoot little Woodstocks and if one Woodstock hits a Red Baron the Red Baron dies. but in my case if I shoot all Red barons disappear instantly without getting hit. I dont know why this happens. this is in my RedBaron class.
 public void checkCollision()
    {
        Actor meinWoodstock;
        meinWoodstock = this.getOneIntersectingObject(Woodstock.class);
        
        if(meinWoodstock != null)
        {
            this.getWorld().removeObject(this);
        }
    }
thanks for your help, if you need anything just ask.
danpost danpost

2019/12/18

#
Fuph wrote...
if you need anything just ask.
Well, the provided code was insufficient to determine the cause of your issue. Provide the entire class code (for starters).
Fuph Fuph

2019/12/18

#
this is my RedBaron class
public void act() 
    {
        movement();
        this.checkCollision();
    }    
    
     public void movement()
    {
        if(Greenfoot.getRandomNumber(100)<10)
        {
            turn(Greenfoot.getRandomNumber(45));
        }
        
        move();
    }  
    
    public void checkCollision()
    {
        Actor meinWoodstock;
        meinWoodstock = this.getOneIntersectingObject(Woodstock.class);
        
        if(meinWoodstock != null)
        {
            this.getWorld().removeObject(this);
        }
    }
Woodstock
public void act() 
    {
        this.move(10);
        if(this.isAtEdge())
        {
            this.getWorld().removeObject(this);
        }    
    }
and Snoopy
 public void act() 
    {
        checkKeypress();
    {
        if(Greenfoot.isKeyDown("space"))
        {
            Woodstock meinWoodstock = new Woodstock();
            this.getWorld().addObject(meinWoodstock, this.getX(), this.getY());
            meinWoodstock.setRotation(getRotation());
            
        }
    }
    }    
    
    public void checkKeypress()
    {
        if(Greenfoot.isKeyDown("up"))
        {
            setRotation(270);
            move();
        }
        if(Greenfoot.isKeyDown("left"))
        {
            setRotation(180);
            move();
        }
        if(Greenfoot.isKeyDown("right"))
        {
            setRotation(0);
            move();
        }
        if(Greenfoot.isKeyDown("down"))
        {
            setRotation(90);
            move();
        }
    }
I hope this helps
danpost danpost

2019/12/18

#
Still do not see anything that might cause it. What are the dimensions of the images used for Red Baron and Woodstock?
Fuph Fuph

2019/12/18

#
Ah maybe thats the reason. For RedBaron the image is 1754x1227 and Woodstock is 225x225. but my World is only 640x480. Could this be the problem and if, how can I fix it? simply scale the pictures smaller?
danpost danpost

2019/12/18

#
Fuph wrote...
For RedBaron the image is 1754x1227 and Woodstock is 225x225. but my World is only 640x480.
Definitely a problem. You probably have a lot of transparency that can be removed (complete rows or columns of unused image along the outer edges).
Fuph Fuph

2019/12/18

#
Ok thank you, I will try it.
Fuph Fuph

2019/12/19

#
I just tried it and it worked! Thank you very much!
You need to login to post a reply.