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

2013/5/1

An object which spans across two cells

rowanto rowanto

2013/5/1

#
Does anybody here know how to create an object which spans across two cells? Just image I want to create a rope object (a line) which binds two actors of other type together. I can't seem to figure it out. If I create a constructor which take in two actors as parameter, after clicking new Object(), the dialog window can't find the existing actors. And if I were to create a normal constructor, then try to put it on the map then, I will actually need to click at two points, which is not supported? and btw, is it possible to draw across cells? Sorry I think my question is kind of noobish, but if anyone could kindly explain or just give me some example scenario I could look at, it would be very helpful. Thanks in advance!
danpost danpost

2013/5/1

#
// with two actors
Actor actor1;
Actor actor2;
// create rope as follows
int ropeLength = (int) Math.hypot(actor2.getX()-actor1.getX(), actor2.getY()-actor1.getY());
Rope rope = new Rope(ropeLength);
addObject(rope, (actor1.getX()+actor2.getX())/2, (actor1.getY()+actor2.getY())/2);
rope.turnTowards(actor2.getX(), actor2.getY());
In your Rope class constructor, create a horizontally long object of given length using a set height (thickness of the rope). Fill it with a color or texture. You may want to have your actors always painted on top of the rope. Use the World class method 'setPaintOrder' method to do that.
rowanto rowanto

2013/5/1

#
the problem is how do I assign the two actor to the ropes? That's what I still can't figure out. I mean, if I put the assignment in the contructor as variable, it can't find the actors after I right clicked and click new Rope. The user should to be able to create as many ropes as he want and bind those with the actors. Is something possible? Like clicking new Rope then just click on two actors on the grid.
danpost danpost

2013/5/1

#
Use this for you Rope class
import greenfoot.*;
import java.awt.Color;

public class Rope extends Actor
{

    Actor actorOne;
    Actor actorTwo;
    
    public Rope(Actor actor1, Actor actor2)
    {
        actorOne = actor1;
        actorTwo = actor2;
    }
    
    public void addedToWorld(World world)
    {
        updateImage();
    }
    
    private void updateImage()
    {
        int ropeLength = (int) Math.hypot(actorTwo.getX()-actorOne.getX(), actorTwo.getY()-actorOne.getY());
        GreenfootImage image = new GreenfootImage(ropeLength, 5);
        image.setColor(new Color(96, 24, 24));
        image.fill();
        setImage(image);
        setLocation((actorOne.getX()+actorTwo.getX())/2, (actorOne.getY()+actorTwo.getY())/2);
        turnTowards(actorTwo.getX(), actorTwo.getY());        
    }
    
    public void act()
    {
        updateImage();
    }
}
After selecting the first actor by clicking on it, either press the 'tab' key or click on the next input box before clicking on the other actor; then press 'enter' or click on the 'OK' button.
rowanto rowanto

2013/5/1

#
so it's selected through clicking. Thanks a lot!You've been very helpful!
You need to login to post a reply.