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

2015/3/24

Is it possible for an actor to ignore all other actors codes?

Srax Srax

2015/3/24

#
Hello. I downloaded a Side Scroller scenario my friend sent me. Currently, the actor (Wombat) can jump on platforms without it falling through, but when i make a new actor, my Wombat can jump on top of it, even though i haven't added any code. I want to place an image (A painting), which the wombat cannot jump on, like it is in the background. Can anyone help me with it? Is it possible to do as an actor ignores other actors code? Thanks in advantage. Srax.
Srax Srax

2015/3/24

#
Here is the Wombats code:
import greenfoot.*;

/**
 * A user-controlled actor that walks and jumps, and is pulled down by gravity.
 * <l><li>Left arrow moves actor left (toward left scroll limit)</li>
 * <li>Right arrow moves actor right (toward right scroll limit)</li>
 * <li>Up arrow makes the actor jump</li><l>
 */
public class Wombat extends Actor
{
    final int jSpeed = 25; // the initial 'jump' speed
    int ySpeed = 0, xSpeed = 0; // the initial vertical and horizontal speeds
    boolean aboutFace; // the direction (left or right) the actor is facing
    boolean onGround; // the state of the actor being set on an object or not

    /** 
     * Checks for changes in direction and moves the main actor.
     */
    public void act()
    {
        getDirection();
        move();
    }
    
    /**
     * Moves the actor with appropriate image.  Checks for obstacles and adjusts
     * the position of the actor accordingly.
     */
    private void move()
    {
        ySpeed++; // adds gravity
        if (xSpeed != 0 && onGround) xSpeed+=aboutFace?1:-1; // add friction
        setLocation(getX()+xSpeed/20, 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;
        }
    }
    
    /**
     * Determines any changes in horizontal and vertical speeds for the actor.
     */
    private void getDirection()
    {
//         if (!onGround) return; // if not mid-air changes allowed
        // sets requested direction of move, or continues in current direction
        if (Greenfoot.isKeyDown("left") && xSpeed>-50) xSpeed-=2; // check left
        if (Greenfoot.isKeyDown("right") && xSpeed<50) xSpeed+=2; // check right
        if (Greenfoot.isKeyDown("up") && onGround) // check jump
        {
            ySpeed -= jSpeed; // add jump speed
            ((Score) getWorld().getObjects(Score.class).get(0)).add(1); // increment jump counter
        }
    }
}

danpost danpost

2015/3/24

#
The four 'while' loops in the 'move' method is where the collision detection code is at. Currently, if any actor is found at the offset given, it is returned by the 'getOneObjectAtOffset' call and any other actors that might have been found there are ignored. In other words, just saying to ignore any object returned that is of the type to ignore would not be sufficient. So, it would take extensive changes to the code to have the wombat ignore the new actor. Basically, you would have to use 'getObjectsAtOffset' to get a list of all actors at those locations, remove any instances of the type you wish to have the wombat ignore, then if the list is not empty, move back off each remaining one in the list individually -- or something like that. One easier solution might be to have all the obstacle (or solid) type classes subclass a Solid class and use 'Solid.class' instead of 'null' in the 'getOneObjectAtOffset' calls.
Srax Srax

2015/3/25

#
danpost wrote...
The four 'while' loops in the 'move' method is where the collision detection code is at. Currently, if any actor is found at the offset given, it is returned by the 'getOneObjectAtOffset' call and any other actors that might have been found there are ignored. In other words, just saying to ignore any object returned that is of the type to ignore would not be sufficient. So, it would take extensive changes to the code to have the wombat ignore the new actor. Basically, you would have to use 'getObjectsAtOffset' to get a list of all actors at those locations, remove any instances of the type you wish to have the wombat ignore, then if the list is not empty, move back off each remaining one in the list individually -- or something like that. One easier solution might be to have all the obstacle (or solid) type classes subclass a Solid class and use 'Solid.class' instead of 'null' in the 'getOneObjectAtOffset' calls.
Okay, thanks :)
You need to login to post a reply.