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

2015/1/6

Perfect pixel collision

xFabi xFabi

2015/1/6

#
Hi, me again Q.Q I got a BUTTLOAD of errors, when i tried to use the ppc, instead of intersect() this is what i wrote:
public void act() 
    {
        int x = getX();
        int y = getY();
        move(30.0);
        if(list() != null)
        {
            if( hit < 5)
            {
                hit++;
                getWorld().removeObject(this);
            }
            else
            {
                getWorld().removeObject(list());
                getWorld().removeObject(this);
            }
        }
        if(getWorld().getObjects(fb.class) != null)
        {
            if(edge())
            {
                getWorld().removeObject(this);
            }
        }
    }

    private boolean edge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
            return true;
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
            return true;
        else
            return false;
    }

    public Actor list()
    {
        List<Actor> list =
            getWorld().getObjects(sm.class),
        list2 = new ArrayList();
        for(Actor A : list)
            if(intersects(A)&&touch(A))
                return A;
        return null;
    }
    }
It says Nullpointer exception at line 10 and 23 And also, actor not in world at line 22. How can this be? I made the requirement to only call edge() when getObjects() isn't null, so why does it still call the method? Regarding hit++; , the hit value is used by my sm class. and this class extends it Edit: Actually, ive got a lot more nullpointer exceptions, shall i post them all, or can you already see, what i did wrong? Edit2: Come to think about it, since i wrote if( list() != null), how comes i get error, before my bullet even touches anything?
JetLennit JetLennit

2015/1/6

#
Would you show where you are creating the variable hit?
xFabi xFabi

2015/1/6

#
 public int hit = 0;
    public int pause = 15;
    public int x = Greenfoot.getRandomNumber(4);
    /**
     * Act - do whatever the sm wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        int x = getX();
        int y = getY();
                if(edge())
        { 
            setRotation(getRotation() - 180);
        }
        rm();
        rot();
        //intersect(); ~~~~~~ Ersetzt durch Perfect Pixel Collision ~~~~~~
    }
fb extends sm
danpost danpost

2015/1/6

#
actor not in world at line 22. How can this be? ...
The requirement you gave was that any fb object be in the world, not that this particular one be in the world.
since I wrote if( list() != null)...
I am not exactly sure how 'intersects' nor 'touch' works when you give them the actor that is itself; but, I suspect that 'list()' will never return 'null'. I think you need to make line 19 above an 'else if' line. That should help as far as null pointers are concerned.
xFabi xFabi

2015/1/6

#
regarding the fb object, i cant really understand what you mean. i Gave the requirement to any fb object to exist, this is true but whats wrong about that? Regarding list() i thought about that aswell but "unfortunately" it does
 public Actor list()
    {
        List<Actor> list =
            getWorld().getObjects(sm.class),
        list2 = new ArrayList();
        for(Actor A : list)
            if(intersects(A)&&touch(A))
                return A;
        return null;
    }
EDIT!: Your Line 19 correction solved it, im not getting any errors anymore, but probably because my bullet instantly disappears(which ofc i dont want) and i get damaged instead of the snowman.(For whatever reason, those are different methods in different classes..) Im gonna look into this and post my ongoing errors in a couple of minutes :)
danpost danpost

2015/1/6

#
One thing that strikes funny is lines 10 and 11 in your original post:
hit++;
getWorld().removeObject(this);
the 'hit' field belongs to the object being removed from the world and once there is no reference to the actor, it and its fields are destined for garbage collection. In other words, a bullet will never 'hit' more than one object before its removal; so, the 'hit' field of any bullet can never be more than one.
xFabi xFabi

2015/1/6

#
^^^^^^ this is intended, a bullet may only hit one object before disappearing,so you cant shoot multiple enemies at once(my "bullet" is a fire ball, so that would be kinda illogical) So my new problem is, that my bullet instantly disappears when created, this is the whole code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.util.ArrayList;
/**
 * Write a description of class fb here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class fb extends sm
{

    /**
     * Act - do whatever the fb wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        int x = getX();
        int y = getY();
        move(30.0);
        if(list() != null)
        {
            if( hit < 5)
            {
                hit++;
                getWorld().removeObject(this);
            }
            else
            {
                getWorld().removeObject(list());
                getWorld().removeObject(this);
            }
        }
        else if(getWorld().getObjects(fb.class) != null)
        {
            if(edge())
            {
                getWorld().removeObject(this);
            }
        }
    }

    private boolean edge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
            return true;
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
            return true;
        else
            return false;
    }

    public Actor list()
    {
        List<Actor> list =
            getWorld().getObjects(sm.class),
        list2 = new ArrayList();
        for(Actor A : list)
            if(intersects(A)&&touch(A))
                return A;
        return null;
    }
}
danpost danpost

2015/1/6

#
xFabi wrote...
regarding the fb object, i cant really understand what you mean. i Gave the requirement to any fb object to exist, this is true but whats wrong about that?
What is wrong about that is that you are saying this: if any fb object is in the world, then call the 'edge' method. The 'edge' method then proceeds to call 'getX' on this particular fb object which may or may not be the one found in the world. If this fb object is not in the world, 'getX' will fail.
xFabi xFabi

2015/1/6

#
I dont know if i get this right.. "hit" is a value of sm(The class being shot on) which counts how often my bullet hit it already Can you give me a clue on how to solve the problem about edge? Also, i used this to decrease my life in the character's class
Actor b = this.getOneIntersectingObject(sm.class);
So if fb extends sm, and my character intersects fb, its not regarded as intersecting sm.class, is it? Because my hp gets decreased, although only my bullet touches the character I just found out that
getWorld().removeObject(list());
is the reason for my bullet to get removed. So obviously the 'A" returned by this:
    public Actor list()
    {
        List<Actor> list =
            getWorld().getObjects(sm.class),
        list2 = new ArrayList();
        for(Actor A : list)
            if(intersects(A)&&touch(A))
                return A;
        return null;
    }
is my bullet. Do you know how to change it to return the snowman thats being intersected?
danpost danpost

2015/1/6

#
Yes, since fb extends sm, any fb object is also considered an sm object. And from your last post, I am guessing that this is a major problem in your coding. A fireball is NOT a snowman; therefore, fb must not extend sm (if 'fb' is for fireball and 'sm' is for snowman -- I thought that maybe 'sm' was for 'smooth mover' before). 'hit' is an instance field. Each object created from the class it is declared in and each object created from any of its subclasses will get its own 'hit' field for it to use. It is not a single field that all your objects share. If you want all your fb objects to share a single field, then you need to either move the field elsewhere (like either in the world or in the player that shoots the fb objects) or make the field a 'static' field (which gives the field to the class and not to the objects created from it). Things will be different once you adjust what extends what.
xFabi xFabi

2015/1/6

#
I made fb extend sm, so it could use the hit value from sm, huge fault i see :) Can i make Fb able to acess hit from sm? Ill just cross the idea of needing to get 5hits, its just a project anyway, will probably makes things easier ._. Btw ignore the Actor A stuff in my previous post, i just got kinda confused, its wrong and i cant edit anymore ^-^ So back to my edge() method, can you tell me how to solve that problem? I tried getObjects(this) because of despair and ofc it didnt work hahaha
danpost danpost

2015/1/6

#
Can I make Fb able to acess hit from sm?
Yes. First, do you only have one sm object in the world throughout? Then, do you have a field in your World subclass to hold the sm object in the world? Is it possible that the sm object in the would might be removed for any reason, even at the end?
xFabi xFabi

2015/1/6

#
Thanks, but i just crossed that idea of needing to get 5hits^^, things are a lot easier without it i guess, i might try somewhen else. Still, i got it working with perfect pixel detecion, and the edge() method works as intended again aswell. Sorry for bombarding you with errors, my head is kinda exploding, so i guess i'll stop for now. As i said, thanks for your help once again, but i (unfortunately) think this wont be the last time ^_^ Cya, and have a good night
danpost danpost

2015/1/6

#
xFabi wrote...
So back to my edge() method, can you tell me how to solve that problem?
Remove lines 35, 36 and 41 (from your last class posting) and change line 37 from 'if' to 'else if'.
You need to login to post a reply.