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

2017/2/12

Placing Objects

BrownDwarf BrownDwarf

2017/2/12

#
In my tower defense game, I want to make it so that the user cannot place towers on the path of the balloons, but anywhere else. is the only way to achieve this through the use of invisible objects telling the tower where the path is? if so, what is the most efficient way of achieving this? Thanks
danpost danpost

2017/2/12

#
BrownDwarf wrote...
In my tower defense game, I want to make it so that the user cannot place towers on the path of the balloons, but anywhere else. is the only way to achieve this through the use of invisible objects telling the tower where the path is? if so, what is the most efficient way of achieving this? Thanks
Would need to see (at least) the codes which move the towers to be placed.
BrownDwarf BrownDwarf

2017/2/12

#
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
37
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Dart_Button here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Dart_Button extends Actor
{
    /**
     * Act - do whatever the Dart_Button wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Dart_Button()
    {
        GreenfootImage image = getImage();
        image.scale(image.getWidth() - 20, image.getHeight() - 20);
        setImage(image);
    }
    public void act()
    {
        if (Greenfoot.mouseClicked(this))
       {
           MouseInfo mouse = Greenfoot.getMouseInfo();
          getWorld().addObject(new DartMonkey(), mouse.getX(), mouse.getY());
           
        }
        if (Greenfoot.mouseDragged(this))
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            setLocation (mouse.getX(), mouse.getY());
            getWorld().addObject(new Dart_Button(), 504, 102);
        }
         
    }   
}
Tower itself is below:
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Write a description of class DartMonkey here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class DartMonkey extends Actor
{
    /**
     * Act - do whatever the DartMonkey wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public boolean inRange;
    public boolean isShoot;
    private int timer = 60;
    public void act()
    {
        lookAt();
        shoot();
    }   
 
    public void shoot()
    {
        if (timer > 0 && inRange)
        {
            timer --;
            if (timer == 0 && inRange)
            {
                getWorld().addObject(new Bullet(), getX(), getY());
                timer = 60;
            }
            else if (getWorld().getObjects(RedBloon.class).size() == 0)
            {
                int x = 0;
            }
        }
    }
 
    public void lookAt()
 
    {
      List   c=   getObjectsInRange(200, RedBloon.class);
        if(!c.isEmpty() )
        {
            //Change so that looks at closest using greenfoot solution
            Actor b = (Actor)c.get(0);
             
            turnTowards(b.getX(), b.getY());
            setRotation(getRotation()+90);
            inRange = true;
 
        }
 
         
        else {
            inRange = false;
        }
 
 
    }
    public int getRotations()
    {
        return getRotation();
    }
}
danpost danpost

2017/2/12

#
I am finding it difficult to come up with a simple quick fix here. The fact that you are dragging the button is a bit of a wrench in the works. Maybe it would be best to use an invisible object. Have the buttons always maintain one on them. That will be what is dragged. It can take on the image of the tower that will be placed. You can use 'isTouching(Path.class)' to refuse placement of the tower. Only when the location of placement is validated will the tower be placed into the world and the dragged object can be moved back to the button.
BrownDwarf BrownDwarf

2017/2/12

#
The problem is I don't hava a path.class. I am using an image from google as my background, and this image has a path on it. So would I need to have an invisible object all along the 'path' *that we see), so it cold sense where it is? Or is there an easier way?
danpost danpost

2017/2/12

#
BrownDwarf wrote...
The problem is I don't hava a path.class. I am using an image from google as my background, and this image has a path on it. So would I need to have an invisible object all along the 'path' *that we see), so it cold sense where it is? Or is there an easier way?
That would seem to be the easiest way.
BrownDwarf BrownDwarf

2017/2/12

#
Ok thanks. I'll get on that! :)
BrownDwarf BrownDwarf

2017/2/12

#
Sorry I have a follow up question, not sure if I should start a new thread but... How do I add an object to a list?
danpost danpost

2017/2/12

#
BrownDwarf wrote...
How do I add an object to a list?
See here.
You need to login to post a reply.