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

2013/11/22

[Beginner] How to change speed/transparency with obj. collision

eladage eladage

2013/11/22

#
Ok so I'm trying to get an object to slow down and change transparency when it hits another object and can't quite figure it out. So far I've got public void smellFlowers() { if (canSee(Flower.class)) { move=move()-1; GreenfootImage.setTransparency(-10); } } Both of these come up with errors so I know I'm not doing it right, honestly I'm probably not even close...
danpost danpost

2013/11/22

#
From 'move=move()-1;', you would need (1) an instance field 'move' declared in the class and (2) a 'move' method that returns an int value within the class (these could also be located in a superclass). In the next line, you are trying to set the transparency value of the class 'GreenfootImage'. The class does not have transparency value, only objects of the class (the images create from that class) will have a transparency value. Also, the transparency value must be between 0 and 255 inclusive (it cannot be negative). If you are trying to decrease the value of transparency, you will need to add an instance field to track the transparency value of the image. You probably meant 'getImage()', which refers to the image of the actor, instead of 'GreenfootImage', which is the class it was created from. Just like the transparency of the image of the actor, you would need an instance field to track the speed of the actor..
eladage eladage

2013/11/22

#
Sorry, I feel really stupid but I'm still blanking on getting the transparency to continuously go down each time. I figured out the syntax for changing the transparency but it's a one time thing when the objects collide getImage().setTransparency(100); I don't know where to subtract from the transparency...
danpost danpost

2013/11/22

#
You need to add an instance field to the class
private int transValue = 255;
then, when collision occurs change the value and reset the transparency of the image
transValue -= 10;
if (transValue < 25)  getWorld().removeObject(this);
else getImage().setTransparency(transValue);
EDIT: It would not take long before the value of the field reaches the lower limit and remove the object (maybe about half a second time of interaction). Decreasing the fields value by one (instead of ten) would allow somewhere around 5 to 6 seconds of interaction time.
danpost danpost

2013/11/22

#
If you do not want the object to be removed from the world, change the 3-line code above to:
if (transValue > 30)
{
    transValue -= 10;
    getImage().setTransparency(transValue)
}
You need to login to post a reply.