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

2014/10/27

Create an exception to some classes

MySelfLuls MySelfLuls

2014/10/27

#
I need to make my class (bluePearl) so you can walk through it. Since you need to comsume it. Maybe making so you can just eat it from the cell beside it is easier? I defintly need to know.
public class Player extends Actor
{
    boolean aboutFace;
    int ySpeed = 0, xSpeed = 0;
    final int jSpeed = 20;   


 private void move()
    {
        ySpeed++; // adds gravity
        if (xSpeed != 0 && onGround) xSpeed +=aboutFace?2:-2; // add friction
        setLocation(getX()+xSpeed/10, getY()+ySpeed/2);
        // check for change in horizontal direction
        if((xSpeed>0 && aboutFace) || (xSpeed<0 && !aboutFace)) 
        {
            getImage().mirrorHorizontally();
            aboutFace = !aboutFace;
        }

        // check for obstacles
        onGround=false; // initialize value
        // check below the actor
        while(getOneObjectAtOffset(0, getImage().getHeight()/2+1, null)!=null)
        {
            setLocation(getX(), getY()-1); 
            onGround=true; 
            ySpeed=0;
        }
        // check above the actor
        while(getOneObjectAtOffset(0, -getImage().getHeight()/2-1, null)!=null) 
        {
            setLocation(getX(), getY()+1);
            ySpeed = 0;
        }
        // check to right of actor
        while(getOneObjectAtOffset(getImage().getWidth()/2+1, 0, null)!=null)
        {
            setLocation(getX()-1, getY());
            xSpeed = 0;
        }
        // check to left of actor
        while(getOneObjectAtOffset(-getImage().getWidth()/2-1, 0, null)!=null)
        {
            setLocation(getX()+1, getY());
            xSpeed = 0;
        }
Super_Hippo Super_Hippo

2014/10/27

#
// check below the actor 
Actor a = getOneObjectAtOffset(0, getImage().getHeight()/2+1, null);
while (a!=null && !(a instanceof BluePearl))
{
    setLocation(getX(), getY()-1); 
    onGround=true; 
    ySpeed=0;
    a = getOneObjectAtOffset(0, getImage().getHeight()/2+1, null);
}
//...
Either something like this or you could change the first 'null' to the type of object which should be checked instead. If this is more than one class, you could make them all to a subclass of one class which you could check then. But as I don't know what different classes should be checked, I can't say if this would be a good idea.
MySelfLuls MySelfLuls

2014/10/27

#
Could you show me how to do it with all subclasses of one class?
Super_Hippo Super_Hippo

2014/10/27

#
while(getOneObjectAtOffset(0, getImage().getHeight()/2+1, Classname.class)!=null)
This would be line 23. Instead of 'Classname', write the name of the class. The class replaces the 'null' which checks for every class if used like you did.
danpost danpost

2014/10/27

#
You could also do something like this:
for (Object obj : getObjectsAtOffset(0, getImage().getHeight()/2+1, null).removeAll(getObjectsAtOffset(0, getImage().getHeight()/2+1, Pearl.class)))
{
    Actor obstacle = (Actor)obj;
    while(intersects(obstacle)) setLocation(getX(), getY()-1);
}
Something like this will actually be necessary because if the one object you keep getting is a pearl, then you will not detect the objects that are actually obstacles.
MySelfLuls MySelfLuls

2014/10/29

#
danpost wrote...
You could also do something like this:
for (Object obj : getObjectsAtOffset(0, getImage().getHeight()/2+1, null).removeAll(getObjectsAtOffset(0, getImage().getHeight()/2+1, Pearl.class)))
{
    Actor obstacle = (Actor)obj;
    while(intersects(obstacle)) setLocation(getX(), getY()-1);
}
Something like this will actually be necessary because if the one object you keep getting is a pearl, then you will not detect the objects that are actually obstacles.
Could you add an explanation to what you actually did for each line? I can't seem to understand the code, especially not where am I supposed to put it in my code either? ;/
danpost danpost

2014/10/29

#
Line 1: * the 'enhanced for' (or 'for each') loop -- see lower half of this page * the 'getObjectsAtOffset' method-- see Actor class documentation * the 'removeAll' method -- see List class documentation Line 3 assigns the object from the resultant list to an Actor field. This is so the object will be looked upon as an Actor object, not just an Object object (necessary because the 'intersects' method requires an Actor object). Line 4 moves the player up until it no longer intersects the actor from the list. In total, the player will be moved up until it no longer intersects any and all objects that are not Pearl objects.
You need to login to post a reply.