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

2012/11/11

Intersecting object

1
2
kevv kevv

2012/11/11

#
Hi, does anyone have idea how to improve my code where the arrow hits the dragon. It only works when hitting the exact middle of the dragon image.
1
2
3
4
5
6
7
8
public void kill()
{
    Actor arrow;
    arrow = getOneObjectAtOffset(0, 0, Arrow.class);
    if (arrow != null)
    {
        setImage("dead.png");
        pause();
http://www.greenfoot.org/scenarios/6523 here is the scenario, don't currently want to make the code public.
erdelf erdelf

2012/11/11

#
change line 4 to this:
1
arrow = getOneIntersectingObject(Arrow.class);
kevv kevv

2012/11/11

#
Thanks mate. Now I got a new problem. My score counter code:
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)
import java.awt.Color;
 
/**
 * Write a description of class Counter here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
    private int score;
     
    public Counter()
    {
        score = 0;
        setImage (new GreenfootImage(200, 30));
        update();
    }
     
    public void addScore()
    {
        score++;
        update();
    }
     
    public void update()
    {
        GreenfootImage img = getImage();
        img.clear();
        img.setColor(Color.WHITE);
        img.drawString("Dragons killed: " + score, 20, 30);
    }
}
The part from the dragon class when it should add score:
1
2
3
4
5
6
7
8
9
public void kill()
{
    Actor arrow;
    arrow = getOneIntersectingObject(Arrow.class); 
    if (arrow != null)
    {
        setImage("dead.png");
        pause();
        counter.addScore();
In the world:
1
2
3
4
5
        Dragon dragon = new Dragon(counter);
 
and in the top:
 
    private Counter counter;
Error I get:
1
2
3
4
5
6
7
java.lang.NullPointerException
    at Dragon.kill(Dragon.java:104)
    at Dragon.act(Dragon.java:38)
    at greenfoot.core.Simulation.actActor(Simulation.java:565)
    at greenfoot.core.Simulation.runOneLoop(Simulation.java:523)
    at greenfoot.core.Simulation.runContent(Simulation.java:213)
    at greenfoot.core.Simulation.run(Simulation.java:203)
What am doing wrong? :)
SPower SPower

2012/11/11

#
This is probably wrong:
1
counter.addScore();
you got a null pointer exception:
1
java.lang.NullPointerException
which means you're using an object which is null. For more about exceptions, follow this course.
kevv kevv

2012/11/11

#
Sorry, i'm sure i should actually follow your course for my own gain, but too tired and have to get things done. Have added a counter like that before in another game, can see what is different this time it does not work. Would be very grateful for some help as if you talked to a very stupid person. ;) (maybe I am)
SPower SPower

2012/11/11

#
Add this code before using the counter (before the addScore method)
1
2
3
if (counter == null)
    counter = getWorld().getObjects(Counter.class).get(0);
// now you can use the counter!
kevv kevv

2012/11/11

#
1
2
3
if (counter == null
counter = getWorld().getObjects(Counter.class).get(0); 
counter.addScore();
highlights the 0 in the end and says incompatible types
erdelf erdelf

2012/11/11

#
change the line to:
1
counter = (Counter)getWorld().getObjects(Counter.class).get(0);
kevv kevv

2012/11/11

#
Ok now it works but the counter keeps on increasing. But I wanna figure this one out myself. :) Thank you very much erdelf! // sorry forgot to thank SPower for his help!
kevv kevv

2012/11/11

#
If I would like to use getObjectsInRange instead of getOneIntersectingObject(Arrow.class); how should it look like? Greenfoot summary says: getObjectsInRange(int radius, java.lang.Class cls) Return all objects within range 'radius' around this object. So I tried: arrow = getObjectsInRange(100, Arrow.class); It says incompatible types. Reason Why i want to change it is that the gif is bigger than the actual dragon.
danpost danpost

2012/11/11

#
The method 'getObjectsInRange' does not return an Object (by itself), but a List object that may or may not contain any objects. Since you cannot set a List object to a field that is declared to hold an Arrow object, you get your error. You can use
1
2
3
4
if (!getObjectsInRange(100, Arrow.class).isEmpty())
{
    arrow = (Arrow) getObjectsInRange(100, Arrow.class).get(0);
    // etc.
to get a reference to the arrow object.
kevv kevv

2012/11/12

#
1
2
3
4
5
6
7
8
9
private void newDragon()
{
    if (alive == false)
    {
        alive = true;
        getWorld().addObject (new Dragon (counter), 0, 400); 
         
        }
    }
http://www.greenfoot.org/scenarios/6523 Tried so many different ways, cannot figure out why one dragon won't be created. : /
kevv kevv

2012/11/12

#
1
2
3
4
5
6
7
8
9
private void newDragon()
{
    if (alive == false && dragonCreated == false)
    {
        alive = true;
        getWorld().addObject (new Dragon (counter), 0, 400); 
        dragonCreated = true;
        }
    }
This keeps it kinda under control but still not the way I need it. One dies another appears.
danpost danpost

2012/11/12

#
How about something like
1
if (getWorld().getObjects(Dragon.class).isEmpty())
OH, and forget about all the booleans.
kevv kevv

2012/11/12

#
1
2
3
4
5
6
7
private void newDragon()
{
    if (getWorld().getObjects(Dragon.class).isEmpty()) 
    {
        getWorld().addObject (new Dragon (counter), 0, 400); 
        }
    }
Like this nothing happens. :)
There are more replies on the next page.
1
2