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

2015/5/11

Double actor trouble

1
2
Corrupted Corrupted

2015/5/14

#
So I want to put
public Arm arm;

public Arm(Actor arm)
    {
        arm = arm;
    }
In my Arm method
public Arm arm;
In my Body Method and
arm.setLocation(getX(), getY());
In my Body act method?
danpost danpost

2015/5/14

#
Corrupted wrote...
So I want to put
public Arm arm;

public Arm(Actor arm)
    {
        arm = arm;
    }
In my Arm method
Passing an arm object to an Arm object constructor does not make any sense. Nor does keeping a reference to an Arm object in the Arm class. Remove the above from the Arm class.
public Arm arm;
In my Body Method
First, I think you mean the Body constructor; however, the line should be outside any constructor or method (but still inside the Body class).
and
arm.setLocation(getX(), getY());
In my Body act method?
Yes; preferably at the end of the method.
Corrupted Corrupted

2015/5/14

#
Yeah, sorry I phrased the second one wrong, I keep getting the same error though. Where would I want to put the setter then?
Super_Hippo Super_Hippo

2015/5/14

#
In the Body class:
public setArm(Arm a)
{
    arm = a;
}
To call the method, you can either pass the Body object to the Arm object...
//when building up the world
Body body = new Body();
addObject(body, /**x, y*/);
addObject(new Arm(body), /**x, y*/);
...and use this in the Arm class...
public Arm(Body body)
{
    //...
    body.setArm(this);
}
...or you just use this in the arm class.
protected void addedToWorld(World w)
{
    ((Body)(w.getObjects(Body.class).get(0))).setArm(this);
}
(Add the arm after adding the body.)
Corrupted Corrupted

2015/5/15

#
I tried to add the first bit to the Body method and all I get is a "invalid method declaration; return type required."
danpost danpost

2015/5/15

#
Corrupted wrote...
I tried to add the first bit to the Body method and all I get is a "invalid method declaration; return type required."
It should be:
 public void setArm(Arm a)
with 'void' in there for the return type. I wrote a general class for a double actor. The only excess stuff is the movement code for the main actor and the turning code for the secondary actor. I tried to document it so that some understanding of what I did may come through, Peruse over it; re-create it if you can (all the code for all classes is actually given; the only thing you would need to do is import the images, which are provided by greenfoot); hope you can come to some realization of what it is actually doing. Here it is:
import greenfoot.*;

/**
 * This class shows the basic set-up for an actor with a secondary part.  This could be a
 * tank with a turret, a body with an arm, a ship with a forcefield, or any similar pairing
 * of objects where one 'belongs' to the other.  This is done by use of an inner class; a
 * class within a class.  Any object created from an inner class 'belongs' to the object that
 * created it.  With the following class, multiple Owner objects could be created and each
 * will recieve its own object of the inner class.  Interestingly enough, the Owned objects
 * automatically know which object created it; but, the creators must keep references to the
 * objects they own.
 * 
 *  WHAT I DID...
 *  1) Just to show there is no relevant code elsewhere, the only other code in the project
 *  was in the world class.  Its entire code was the following (notice it only creates and
 *  adds one Owner object into the world):
 *  
 *  import greenfoot.*;
 *  
 *  public class Domain extends World
 *  {
 *      public Domain()
 *      {
 *          super(800, 600, 1);
 *          addObject(new Owner(), 400, 300);
 *      }
 *  }
 *  
 *  2) I set the default image of this class to "button-green.png"
 *     {provided by greenfoot in the 'objects' folder)
 *  3) I gave this actor some "random moving/turning" behavior (see 'move()' method)
 *  4) I gave the Owned actor "turn toward mouse when mouse is moved" behavior
 *     {in 'act' method of inner Owned class)
 *  
 *  All comment below with the words 'owned actor' are at critical code points.  These include:
 *  a) referencing the owned object in a field
 *  b) adding the owned object into the world in the 'addedToWorld' method
 *  c) maintaining the owned object's location in the Owner object's 'act' method
 *  d) removing the owned object when the owner is removed in the Owned object's 'act' method
 *  
 *  Code to set the image of the Owned object should be provided in its constructor; however,
 *  I did not consider that to be critical as the general code is complete without it.
 *  As you can see, all code related to the owned object is completely contained in this class.
 */
public class Owner extends Actor
{
    private Owned owned = new Owned(); // owner-s reference field for owned actor 
    
    protected void addedToWorld(World world)
    {
        world.addObject(owned, getX(), getY()); // add owned actor into world at owner's locaation
    }
    
    public void act()
    {
        move(); // owner moves
        owned.setLocation(getX(), getY()); // maintain position of owned actor at owner's location
    }
    
    private void move()
    {
        turn(Greenfoot.getRandomNumber(31)-15);
        move(2);
    }
    
    public class Owned extends Actor
    {
        public Owned()
        {
            // set image (the default image of an inner class actor cannot be changed;
            // so we need to change the image from the default greenfoot icon)
            setImage("castle.png");
            getImage().scale(30, 30);
        }
        
        public void act()
        {
            // detect and turn toward any mouse movement
            if (Greenfoot.mouseMoved(null) || Greenfoot.mouseDragged(null))
            {
                MouseInfo mouse = Greenfoot.getMouseInfo();
                turnTowards(mouse.getX(), mouse.getY());
                turn(90);
            }
            // remove owned actor (this) if owner is not in world
            if (Owner.this.getWorld() == null) getWorld().removeObject(this);
        }
    }
}
Corrupted Corrupted

2015/5/15

#
Thank you, if works perfectly! I get an error at the end when the player dies, but I'll be able to fix that myself. Once again, thank you!
You need to login to post a reply.
1
2