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

2016/4/29

Moving an Object When Touching Another

CuddlySpartan CuddlySpartan

2016/4/29

#
So I am still working on my Frogger project and when my bee touches a flower, i want it to move with the flowers (the flowers automatically move on their own). I can't tell what exactly is wrong with my code here. Any help? This code is in the bee class:
public void act() 
    {
        movement();
        hitBird();
        touchFlower1();
    }    
    public void touchFlower1()
    {
        Actor Flower1 = getOneIntersectingObject(flower1.class);
        if (Flower1 != null)
        {
            this.setLocation(Flower1.getX(),Flower1.getY());
        }
    }
danpost danpost

2016/4/29

#
If you want the flower to influence the location of the bee, you should have the flower do the work. Make the bee move in the same direction and speed as the flower moves (from the class of the flower).
CuddlySpartan CuddlySpartan

2016/4/29

#
I now have this in the flower class, but it still won't work. Any ideas?
public void touchBee()
    {
        Actor Bee = getOneIntersectingObject(bee.class);
        if (Bee != null)
        {
            Bee.setLocation(getX(),getY());
        }
    }
danpost danpost

2016/4/29

#
CuddlySpartan wrote...
I now have this in the flower class, but it still won't work. Any ideas? < Code Omitted >
You cannot just call a separate method to move the bee with the flower. You need to know what direction and how far the flower moves to have the bee move with it. That means you need to add code along with the code that moves the flower itself. Show the code in the class of the flower that has the flower move.
CuddlySpartan CuddlySpartan

2016/4/30

#
Here is the flower movement:
int vel = 1;
    int moveCounter=0;
    public void act() 
    {
        //touchBee();
        if (moveCounter > 75 && getX()>=12)
        {
            move(vel);
            moveCounter=0;
            setLocation(0,getY());
        }
        else if (moveCounter > 75)
        {
            move(vel);
            moveCounter=0;
        }
        moveCounter++;
    }
I've been playing around a lot and I can't get anything to work yet. Thanks for helping!
danpost danpost

2016/4/30

#
CuddlySpartan wrote...
Here is the flower movement: < Code Omitted > I've been playing around a lot and I can't get anything to work yet. Thanks for helping!
Well, let's clean the code up first. There is no need to move the flower if it is going to be relocated anyway (see lines 8 and 10). So, we can remove line 8. Now what we have is if the move counter is 75, we reset the counter to zero and either move or relocate depending on the x-coordinate value. This can be written as follows (to replace lines 6 through 16):
if (moveCounter > 75)
{
    if (getX() >= 12)
    {
        setLocation(0, getY());
    }
    else
    {
        move(vel);
    }
    moveCounter = 0;
}
Now there are two places where movement is coded here. So. before line 3 in this revised code (line 3 is where the movement code begins) we need to know if a bee is present:
Actor bee = getOneIntersectingObject(Bee.class);
Then, for the 'true' part we can add this before the 'setLocation' statement:
if (bee != null)
{
    bee.setLocation(bee.getX()-getX(), bee.getY());
}
and for the 'false' part, this before the 'move' statement:
if (bee != null)
{
    bee.setLocation(getX()+vel, getY());
}
Try that out.
CuddlySpartan CuddlySpartan

2016/4/30

#
This is now what I have:
 int vel = 1;
    int moveCounter=0;
    public void act() 
    {
        if (moveCounter > 75)
        {
            Actor Bee = getOneIntersectingObject(bee.class);
            if (getX() >= 12)
            {
                if (Bee !=null)
                {
                    Bee.setLocation(Bee.getX()-getX(), Bee.getY());
                }
                setLocation(0, getY());
            }
            else
            {
                if (Bee !=null)
                {
                    Bee.setLocation(getX()+vel, getY());
                }
                move(vel);
            }
            moveCounter++;
        }
    } 
And now my flower moves once and stops. I'm not positive if I put in the code how you wanted or not but it still isn't working. And just clarifying, I want the flower to constantly be moving across the screen, and if the bee touches it, it should keep moving and the bee should be "stuck" to the flower until you push an arrow key to move the bee off the flower.
danpost danpost

2016/4/30

#
Sorry, I misplaced a line. Lines 24 and 25 need to be switched. No -- well, yes, well. Wait a minute.
CuddlySpartan CuddlySpartan

2016/4/30

#
Okay now the flowers are speeding around my screen super fast? The Bee hasn't touched the flowers yet and they are zooming across.
danpost danpost

2016/4/30

#
Replace the end of the code block starting at line 24 with the following:
        moveCounter = 0;
    }
    moveCounter++;
}
danpost danpost

2016/4/30

#
Make sure to remove any code in your bee class that tries to have it move with the flower.
CuddlySpartan CuddlySpartan

2016/4/30

#
All hail danpost, he's a god. <3 Thanks so much! Now I just need to tweak the code for the other flowers but I don't think that'll be any trouble! Thanks again!
CuddlySpartan CuddlySpartan

2016/4/30

#
Okay, I lied, I have one last question. The Bee sticks to the flower and all that, the code works fine. My only issue is that when the bee touches the flower, the flower image is on top. Is there anyway to put the bee on top? I'm assuming that the flowers are on top because they are spawned into the world after the bee. Anyway to fix this or do I just need to deal with it? Thanks!
danpost danpost

2016/4/30

#
CuddlySpartan wrote...
Okay, I lied, I have one last question. The Bee sticks to the flower and all that, the code works fine. My only issue is that when the bee touches the flower, the flower image is on top. Is there anyway to put the bee on top? I'm assuming that the flowers are on top because they are spawned into the world after the bee. Anyway to fix this or do I just need to deal with it? Thanks!
See the 'setPaintOrder' method documention in the World class API, here.
CuddlySpartan CuddlySpartan

2016/4/30

#
Awesome thanks a ton! Its working perfectly :D
You need to login to post a reply.