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

2017/2/3

Moving along a Path

BrownDwarf BrownDwarf

2017/2/3

#
Hi, I've started a new game, it is kind of like Bloons Tower Defense. The movement of the balloons occurs along a pth on a map. I used an image as the map, and the balloon actor needs to move along the path. There are two ways I see of doing this. One is coding movement very precisely to go along the path (Like move(x), and if getx is this, then move(another direction).) This would tak emuch to long, so I thought maybe add an object at the sides of the path on the map, and if the ballon is touching this object move in the direction I want to. The problem is I don't see the practicality of this as I can only think of doing this like the following: if balloon istouching objectx at right and top, then move down. If balloon is touching objectx at bottom and right, move left. This would work in theory but I'm not sure how practical this really is. Do you see any other way of accomplishing this, or a way to improve my method. Thanks BTW I don't actually have code for this yet iwas just planning so I can't post code right now.
danpost danpost

2017/2/4

#
BrownDwarf wrote...
Do you see any other way of accomplishing this, or a way to improve my method.
The actor could be a small transparent object in the path of the balloons. It could hold a value that determines which way to proceed from that point. The value could be a simple degrees of rotation to set at that point.
BrownDwarf BrownDwarf

2017/2/4

#
Oh ok I think I get what you are saying. You mean I should add tis object at every point where the balloon needs to turn, and in fact hold 4 of these objects each with four different direction instructions. That's how I understand it. Also just for future reference, if there was a very curvy and windy path, where simply turning would be very repetitive(as it would be many turns to complete the arch like path), how would I go about this same problem in that situation. Thanks
danpost danpost

2017/2/4

#
BrownDwarf wrote...
Oh ok I think I get what you are saying. You mean I should add tis object at every point where the balloon needs to turn, and in fact hold 4 of these objects each with four different direction instructions
Actually, only one type object is needed. You can create it while passing the directional value to it (via a parameter in the constructor call).
if there was a very curvy and windy path, where simply turning would be very repetitive (as it would be many turns to complete the arch like path), how would I go about this same problem in that situation.
You can pass and have it hold another value for turn rate. Please refer to my Racetrack Demo scenario, which controls the car(s) around a track with straight-ways and rounded corners.
BrownDwarf BrownDwarf

2017/2/4

#
Hi, Sorry I tried implementing the code with 4 different direction instructions and it doesn't work.
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class RedBloon here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class RedBloon extends Actor
{
    /**
     * Act - do whatever the RedBloon wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public int turnD = 90;
 
    public void act()
    {
      movement();
    }   
     
    public void movement()
    {
        //setRotation(turnD);
        move(1);
        //setRotation(0);
    }
    public void setTurn(int x)
    {
        turnD = x;
    }
    public int getTurn()
    {
        return turnD;
    }
}
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Marker here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class DownMarker extends Actor
{
    /**
     * Act - do whatever the Marker wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public DownMarker()
    {
        GreenfootImage image = getImage();
        image.scale(image.getWidth() - 493, image.getHeight() - 493);
        setImage(image);
    }
    public void act()
    {
        touching();
    }  
    public void touching()
    {
        if(getOneObjectAtOffset (10, 10, RedBloon.class) != null)
        {
           RedBloon bloon = new RedBloon();
         bloon.setTurn(90);
          
        }
    }
}
I think it has something to do with the fact that I make a new balloon to change the direction of, but I don't know how to change the direction of the CURRENT balloon on the screen. How do I access that to change its turn variable. If you need any more code just tell me. thanks
BrownDwarf BrownDwarf

2017/2/4

#
Never mind, I got it to work by changing the turn variable into a static variable, and also he setter method to static. However, I don't understand why this worked. If you can explain thanks.
danpost danpost

2017/2/4

#
For one thing, the image of the marker should be (1) transparent and (2) single-cell sized. For another, the balloons should be looking for the markers -- not the other way around. For example, if you had a simple Marker class as such:
1
2
3
4
5
6
7
8
9
10
11
12
13
import greenfoot.*;
 
public class Marker extends Actor
{
    int x, y;
 
    public Marker(int x, int y)
    {
        setImage(new GreenfootImage(1, 1)); // single-celled transparent actor
        this.x = x;
        this.y = y;
    }
}
Then, you can create and place objects of this class in the path of the balloons:
1
2
3
4
5
6
7
8
addObject(new Balloon(), 100, 100);
addObject(new Marker(1, 0), 100, 100); // right from here (balloon placed here also)
addObject(new Marker(0, 1), 400, 100); // down from here
addObject(new Marker(-1, 0), 400, 200); // left from here
addObject(new Marker(0, 1), 100, 200); // down from here
addObject(new Marker(1, 0), 100, 300); // right from here
addObject(new Marker(0, -1), 500, 300); // up from here
addObject(new Marker(0, 0), 500, 100); // end of path
Now, the balloon, with the following fields:
1
int x, y;
can follow the instructions with this:
1
2
3
4
5
6
7
8
9
10
11
12
13
public void act()
{
    // look for marker
    Marker marker = (Marker)getOneObjectAtOffset(0, 0, Marker.class);
    if (marker != null && marker.getX() == getX() && marker.getY() == getY()) // at marker location
    {
        // get marker directions
        x = marker.x;
        y = marker.y;
    }
    // move as instructed
    setLocation(getX()+x, getY()+y);
}
FYI, just like one class can create multiple types of markers, one class can also create the different balloons in your project. Just pass a Color value (or some number that would representing a specific color) to a generic balloon class constructor and set the image in the constructor according to the value sent. That way, you do not need to duplicate (triplicate, quadruplicate) the code and create so many classes.
danpost danpost

2017/2/4

#
BrownDwarf wrote...
Never mind, I got it to work by changing the turn variable into a static variable, and also he setter method to static. However, I don't understand why this worked. If you can explain thanks.
When the field and method were non-static, you were only changing the rotation of the new balloon which is never added into the world. So, all you saw was the original balloon with no changes to it. When you made them static, ALL red balloons take on the same turn value (which may not be the behavior you will want when more than one red balloon is in the world at any time).
You need to login to post a reply.