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

2013/3/29

how to define a bloc physically

welleid welleid

2013/3/29

#
Hi, My problem is that I must remove an object if another touches it. The object to remove is a bloc, and the object that attacks is a ball. When the ball hit the bloc, I want it to be removed. Thus I crated a function :
    public void destroyBloc() 
    {
        int bordBlocX = 50;
        int bordBlocY = 25;
        
        if(contactObjet(Bloc.class, bordBlocX, -bordBlocY))
        {
            
            destroy(Bloc.class, bordBlocX, -bordBlocY);
            setRotation(360 - getRotation());
            
        }
    }
The width of my bloc is 100 and the height is 50. I also use two function named contactObjet and destroy :
    public boolean contactObjet(Class clss, int bordObjetX, int bordObjetY)
    {
        Actor actor = getOneObjectAtOffset(bordObjetX, bordObjetY, clss);
        
        return actor != null;        
    }
And destroy
    public void destroy(Class clss, int bordObjetX, int bordObjetY)
    {
        Actor actor = getOneObjectAtOffset(bordObjetX, bordObjetY, clss);
        
        if(actor != null) 
        {
            getWorld().removeObject(actor);
        }
    }
My problem is that if the ball comes from under the bloc orfrom the left, no problem, the bloc is destroyed when the ball hits the LIMIT of the bloc. But from the right or the top, ball must hit the middle of the bloc, and it shouldnt. I tryied to put another condition like
if(contactObjet(Bloc.class, bordBlocX, -bordBlocY) || contactObjet(Bloc.class, - (bordBlocX), -(bordBlocY)) )
or something more like
        if(contactObjet(Bloc.class, bordBlocX, -bordBlocY) )
        {
            
            destroy(Bloc.class, bordBlocX, -bordBlocY);
            setRotation(360 - getRotation());
            
        }

        if(contactObjet(Bloc.class, -bordBlocX, bordBlocY) )
        {
            
            destroy(Bloc.class, -bordBlocX, bordBlocY);
            setRotation(360 - getRotation());
            
        }
I'm actually running out of idea, even if i'm sure it's shouldn't be so hard to fix^^ Thanks a lot, Welleid
Gevater_Tod4711 Gevater_Tod4711

2013/3/29

#
The easiest way would be to use the method getOneIntersectingObject of the Greenfoot API. This method checks whether your ball intersects the block. If it returns true you destroy the block.
welleid welleid

2013/3/29

#
It gave me an error message saying "unexpected type". My Ball is a class, my bloc is a class too. All this code takes place in the ball's one
danpost danpost

2013/3/29

#
What code was highlighted when you got that error message?
welleid welleid

2013/3/29

#
the Bloc.class was in red. In this case the error message is : unexpected type. required variable, found value, but as I did an error as it isnt the boolean I deleted the "true" If I delete the "= true", then the error message becomes "unexpected type", but it's the same that is highlighted
Gevater_Tod4711 Gevater_Tod4711

2013/3/29

#
I think that's my fail. You missunderstood what I ment in the post above. The right code would be:
Block block = getOneIntersectingObject(Block.class);
if (block != null) {
    getWorld().removeObject(block);
}
welleid welleid

2013/3/29

#
Actually, I still get the error message "unexpected type" (I just corrected the block by bloc, it's not supposed to change anything right ?^^)
danpost danpost

2013/3/29

#
Initially, the problem was you were trying to set (=), instead of compare (==), an Object (that which is returned from 'getOneIntersectingObject') with a boolean (true), which you cannot do. Then you are trying to remove a Class from the world, which you cannot do (you can only remove actor objects from the world). A class is something that can create objects that can be placed and removed from the world, but cannot in itself be placed or removed from the world. Better would be the following
Bloc bloc = (Bloc)getOneIntersectingObject(Bloc.class);
if (bloc != null) getWorld().removeObject(bloc);
The (Bloc) is needed to have the variable 'bloc' accept it as an object of type Bloc.
welleid welleid

2013/3/29

#
thanks guy it's perfect now !
You need to login to post a reply.