This site requires JavaScript, please enable it in your browser!
Greenfoot back
vtn2@calvin.edu
vtn2@calvin.edu wrote ...

2015/6/8

Clone of an Actor

vtn2@calvin.edu vtn2@calvin.edu

2015/6/8

#
Is there any (easy) way to make a complete clone of a subclass of an Actor? (Actually, it would need to be a clone of a subclass of a subclass of an Actor.) What I think I would like is the equivalent of public Actor(Actor other) -- i.e., a copy constructor. Do you know if the clone() method inherited from Object would work? Thanks. Vic Norman
danpost danpost

2015/6/8

#
I do not think the using the Object class 'clone' method is possible because the Actor class does not support its use. You could, like you said, create a cloning constructor; or alternatively a cloning system of your own similar to the one of the Object class (only it starts in your first subclass of Actor).
vtn2@calvin.edu vtn2@calvin.edu

2015/6/8

#
Thanks. That's the conclusion I came to, too. Too bad there isn't a copy constructor for the Actor. When i create a clone, I'll have to ask "old" actor for its x, y, direction, size, image, etc., etc. and recreate those in my new copy... I hope I remember everything! :-)
danpost danpost

2015/6/8

#
Using a method, you would probably do something like this:
1
2
3
4
5
6
7
8
9
10
11
public ActorClassName cloneMe()
{
    ActorClassName acn = new ActorClassName();
    acn.setImage(getImage());
    acn.setRotation(getRotation());
    if (getWorld()) != null)
    {
        getWorld().addObject(acn, getX(), getY());
    }
    return acn;
}
This, however, is only good enough to copy the basic actor. Any fields you add in the subclasses of Actor leading up to the class of your actor must be dealt with as well to make it a true copy. For what purpose do you require an exact copy of the actor?
vtn2@calvin.edu vtn2@calvin.edu

2015/6/9

#
I am working on a layer below Actor that provides similar functionality as exists in Scratch. Scratch has a clone() function, which duplicates a sprite completely, so I'm trying to do the same thing in Greenfoot. I have it working now, with some code similar to what you suggested above. Thanks.
danpost danpost

2015/6/9

#
I was mistaken about the usability of 'clone' and came up with the following general layout for cloning an object of the actor class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import greenfoot.*;
 
public class Any extends Actor implements Cloneable // must be implemented
{
    // the clone method
    public Object clone()
    {
        try
        {
            Actor clone = (Actor)super.clone();
            // in the case that the actor cloned was in the world, the 'world' field of the
            // clone needs to be reset to 'null' before adding clone into the world;
            // one not only cannot add an object into a world it is already in, but
            // cannot even add one into a world that it only appears to be in
            if (clone.getWorld() != null)
            {
                clone.getWorld().removeObject(clone);
                getWorld().addObject(clone, getX(), getY());
            }
            return clone;
        }
        catch (CloneNotSupportedException cnse) // must be caught
        {
            System.out.println("Clone not supported");
        }
        return null;
    }
     
    // sample use
    public void act()
    {
        if (Greenfoot.mouseClicked(this))
        {
            Any clone = (Any)clone();
            if (clone != null) clone.move(50);
        }
    }
}
danpost danpost

2015/6/9

#
Well -- the code above seems to work fine in the initial subclass of Actor (the actor and its clone seem to be totally independent of each other); but, I am finding it very difficult to make a subclass of that class clone properly. An object of a subclass of a subclass of Actor appears to clone okay; but, there still seems to be some dependency between the actor and its clone. Still working on it, though.
You need to login to post a reply.