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

2021/2/14

Can you switch the bigger class that a smaller class extends?

BogdanMicu BogdanMicu

2021/2/14

#
I have an object which, while it displays a certain animation, should change its superior class to another one. It initially extends Surface and I want to switch it to extend Misc while that animation is running and then return to Surface when done. Is there a command to change the superior class?
RcCookie RcCookie

2021/2/14

#
Something like that is not possible in Java, not even close. You will have to use multiple objects from different classes.
BogdanMicu BogdanMicu

2021/2/14

#
Ok, thanks!
danpost danpost

2021/2/14

#
You can replace a Surface object with a Misc type object which then runs the animation and then restores the original object.
BogdanMicu BogdanMicu

2021/2/14

#
Yeah, I did just that. I have another question tho. How can I keep track of an object that I spawned through another object? I have a button which, when touched by a bullet, spawns a platform. How can I keep track of that platform to modify it or eventually remove it? This is the code I spawn the platform with:
getWorld().addObject(new Platform(), x, y);
, where the x and y are specified through the parameters of public button.
public button(int x1, int y1)
    {
        x=x1;
        y=y1;
    }
danpost danpost

2021/2/14

#
BogdanMicu wrote...
How can I keep track of that platform to modify it or eventually remove it?
When might it get modified and when might it get removed?
BogdanMicu BogdanMicu

2021/2/14

#
By modifiy I mean change its image or call a method for it. It should get modified when it gets spawned. Regarding the remove function, I want to time it to stay for 5 seconds in the world, for example, and then get deleted.
BogdanMicu BogdanMicu

2021/2/14

#
The problem is that I don't know how to make a reference for it.
danpost danpost

2021/2/14

#
BogdanMicu wrote...
By modifiy I mean change its image or call a method for it. It should get modified when it gets spawned. Regarding the remove function, I want to time it to stay for 5 seconds in the world, for example, and then get deleted.
The Misc type can do both -- supply the new image and run the timer (and replace the Surface object)..
BogdanMicu BogdanMicu

2021/2/14

#
I didn't express myself correctly, sorry. The second question is separate from the first one. I solved that. For this one, I programmed a button which spawns a certain amount of platforms at given locations. The question was: how can I modify those platforms that are newly spawned? This is the complete button class so you can have a clearer view:
import greenfoot.*;
public class button extends Surface
{
    public int x, y, count;
    public button(int x1, int y1, int z1)
    {
        x = x1;
        y = y1;
        count = z1;
    }
    public void act() 
    {
        if(isTouching(Bullet.class))
        {
            getWorld().removeObject(getOneIntersectingObject(Bullet.class));
            if(count==3)
            {
                getWorld().addObject(new Platform(), x, y);
                getWorld().addObject(new Platform(), x-170, y);
                getWorld().addObject(new Platform(), x-340, y);
            }
            if(count==1)getWorld().addObject(new Platform(), x, y);
        }
    } 
}
I was asking how I can modify these newly created objects
getWorld().addObject(new Platform(), x, y);
RcCookie RcCookie

2021/2/14

#
Just save the new instance to a variable:
Platform p = new Platform();
getWorld().addObject(p, x, y);
p.performSomeModification();
You need to login to post a reply.