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

2013/3/29

moving and turning objects

1
2
3
4
Gingervitis Gingervitis

2013/3/29

#
How can I make an object move and turn around another object while the other object moves? I can get the power-up to move and turn in a nice circle, but I don't know how to get it to follow my actor.
danpost danpost

2013/3/29

#
Keep a reference to your actor that is to be followed in the powerup object or get one each act cycle. Use 'turnTowards' to rotate the powerup (or the object it is revolving around -- it was unclear as to exactly what you wanted it to do) to face the location of the player, then move the object. If 'Player' was the name of the class that the actor to follow was created from, then the following should work (getting a reference each act cycle). Place the code in the act method or a method it calls.
1
2
3
Player player = (Player) getWorld().getObjects(Player.class).get(0);
turnTowards(player.getX(), player.getY());
move(3);
danpost danpost

2013/3/29

#
From what you initially wrote, it appears you were referring to three objects. The first was something that moves. The second was the powerup that turns around the first object. The third is the actor which the system made up of the first two are to follow. If this is not the case and you meant that there were only two objects involved, the actor to follow and the powerup, then place the code in the powerup class act method or a method it calls. Otherwise, if there are three objects involved, place the code in the class of the object that the powerup turns around.
Gingervitis Gingervitis

2013/3/29

#
I did exactly what you told me and that got it to follow my actor, but now how can I get to rotate around my actor? (right now it spins in the center of my actor)
Gingervitis Gingervitis

2013/3/29

#
There were only two actors involved by the way, but I did use a third actor to create the power-up, but that actor is not needed for anything related to this.
danpost danpost

2013/3/29

#
Oh, maybe you meant that you are just having trouble with keeping the powerup to stay with the moving object that it turns around. If this is the case, then keep a reference to the powerup in the object that it turns around and let the object it turns around control the movement of the powerup. I will call the object is turn around of type 'Core' and the powerup of type 'PowerUp'. The Core class code should look something like the following:
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
import greenfoot.*
 
public class Core extends Actor
{
    private PowerUp powerUp;
    private int angle;
 
    public Core()
    {
        // create image of core
    }
 
    public void act()
    {
        // put movement code for core here
        if (powerUp == null) return;
        if (powerUp.getWorld() == null)
        {
            powerUp = null;
            return;
        }
        angle = (angle + 1)%360;
        powerUp.setLocation(getX(), getY());
        powerUp.setRotation(angle);
        powerUp.move(20);
        powerUp.setRotation(0);
    }
 
    public void setPowerUp(Powerup pu)
    {
        powerUp = pu;
    }
}
You can use the 'setPowerUp' method to put the powerup in play.
Gingervitis Gingervitis

2013/3/29

#
That did not change anything..... would it help if I published my un-finished game so you can see what I mean? If you say yes, should I include the source code?
danpost danpost

2013/3/29

#
Including the source code would help me in determining exactly what you are trying to do.
Gingervitis Gingervitis

2013/3/29

#
I did not publish it yet, but when you have the source code, let me know so I can remove it from the website.
danpost danpost

2013/3/29

#
Sure thing!
danpost danpost

2013/3/29

#
Got it!
Gingervitis Gingervitis

2013/3/29

#
Just so you know, I have it set so the power-ups appear in level 1 as a test.... later they will be put in later levels.....
danpost danpost

2013/3/29

#
OK. Remove any movement code from the DominatorPower1 class. remove the code I provided from the act method of the Crab class. Then change the 'ifCanSeeDP' method to the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void ifCanSeeDP()
{
    if (canSee(DominatorPower.class))
    {
        eat(DominatorPower.class);
        timer1 = 500;
        if (powerUp != null && powerUp.getWorld() != null) return;
        powerUp = new DominatorPower1();
        getWorld().addObject(powerUp, getX(), getY());
    }
    if (powerUp == null || powerUp.getWorld() == null) return;
    angle = (angle+5)%360
    powerUp.setLocation(getX(), getY()); 
    powerUp.setRotation(angle); 
    powerUp.move(40);
    powerUp.turn(90);
    timer1--;
    if (timer1 == 0) getWorld().removeObject(powerUp);
}
Gingervitis Gingervitis

2013/3/29

#
It's perfect! Thank you so much! I have one small question about the code you provided. In line 12 of your code, you include a percent symbol. What does that do?
danpost danpost

2013/3/29

#
The percent symbol takes what is on the left of it, divides that by what is on the right of it, and returns the remainder. For example, when you divide 7 by 5, you get 1 with a remainder of 2; the remainder (2) is what is returned. This keeps the value within a range. In the code, we want the angle to stay between 0 and 359 inclusive. So, after it reaches 359, the next value it becomes will be 0, starting the sequence over again.
There are more replies on the next page.
1
2
3
4