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

2022/3/22

Changing Images and Properties of an Object

Mauckenweg Mauckenweg

2022/3/22

#
I wanted to have a Sponge for Game which inflates (changes Images) when hit. Also I wanted the player to be able to climb up only the inflated sponge. For the 2. thing i have no idea how to do it...
danpost danpost

2022/4/22

#
You can use the size (width or height, whichever you desire) to determine if the player can climb the sponge. Maybe something like this:
Actor sponge = getOneIntersectingObject(Sponge.class);
if (sponge != null
 && sponge.getImage().getWidth() >= 50
 && Greenfoot.isKeyDown("up"))
{
    climb();
}
Mauckenweg Mauckenweg

2022/4/28

#
Thx for the replie! i wanted to have an entirely different image for the sponge not a bigger version though. How could that be adjusted in the Code?
danpost danpost

2022/4/28

#
Mauckenweg wrote...
i wanted to have an entirely different image for the sponge not a bigger version though. How could that be adjusted in the Code?
How many images do you have for the sponge from not inflated to inflated? What code do you have for inflating the sponge? Show Sponge class codes.
Mauckenweg Mauckenweg

2022/5/5

#
import greenfoot.*;  


public class sponge extends object
{
    public GreenfootImage small, big;
    public sponge()
    {
        small = new GreenfootImage("spongesmol.png");
        big = new GreenfootImage("spongeBEEG.png");
    }
    public void act()
    {
        if (isTouching(inklingshot.class)) 
        {
            setImage(big);
            removeTouching(inklingshot.class);
        }
    }
}
danpost danpost

2022/5/7

#
Mauckenweg wrote...
<< Code Omitted >>
After line 10 (sponge class above), insert the following line:
setImage(small);
In the class of the player, use the following as conditions to climb::
sponge hitSponge = (Sponge)getOneIntersectingObject(sponge.class);
if (hitSponge != null && hitSponge.getImage() == hitSponge.big)
{
    // climbing code here
}
Mauckenweg Mauckenweg

2022/5/12

#
Thank you SO much it works perfectly
You need to login to post a reply.