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

2017/4/9

How do I reverse an Actor's direction?

1
2
Nosson1459 Nosson1459

2017/4/10

#
ET78759 wrote...
I want to reverse the asteroid's direction when it touches the shield. The first two pieces of code were from rocket's class, the third was from a class that made all movement smoother(it was called SmoothMover) and the fourth was from the class Asteroid.
If the Asteroid is the one that you want to reverse, then I would think it makes sense that you should be showing me the Asteroid's moving code.
Nosson1459 Nosson1459

2017/4/10

#
Show the FULL asteroid class code. Ctrl+A - Ctrl+C.
danpost danpost

2017/4/10

#
Try 'turn(180;' instead of 'setRotatioin(180);' in the given Asteroid class code.
Nosson1459 Nosson1459

2017/4/10

#
danpost wrote...
Try 'turn(180;' instead of 'setRotatioin(180);' in the given Asteroid class code.
Nosson1459 wrote...
To reverse an actors direction you can either turn it 180 degrees, or...If you are using the move method the you'll have to rotate the actor, but....
ET78759 ET78759

2017/4/11

#
Using turn(180) had the same effect if I had used setRotation(180). If you want to see the full asteroid code, here:
import greenfoot.*;

/**
 * 
 * @author Edwin 
 */
public class Asteroid extends SmoothMover
{
    private int size;
    private int stability;
    Space space = (Space) getWorld();

    public Asteroid()
    {
        this(50);
    }


    public Asteroid(int size)
    {
        super(new Vector(Greenfoot.getRandomNumber(360), 2));
        setSize(size);
    }


    public Asteroid(int size, Vector velocity)
    {
        super(velocity);
        setSize(size);
    }

    public void act()
    {         
        move();
        checkShield();
        reverseMovement();
    }


    public void setSize(int size) 
    {
        stability = size;
        this.size = size;
        GreenfootImage image = getImage();
        image.scale(size, size);
    }


    public int getStability() 
    {
        return stability;
    }

    public void hit(int damage) 
    {
        stability = stability - damage;
        if (stability <= 0) 
        {
            breakUp();
        }
    }


    private void breakUp() 
    {
        Greenfoot.playSound("Explosion.wav");
        Space space = (Space) getWorld();
        if (size <= 16) {
            getWorld().removeObject(this);
            space.countScore(space.asteroidScore2);
        }
        else {
            int r = getVelocity().getDirection() + Greenfoot.getRandomNumber(45);
            double l = getVelocity().getLength();
            Vector speed1 = new Vector(r + 60, l * 1.2);
            Vector speed2 = new Vector(r - 60, l * 1.2);        
            Asteroid a1 = new Asteroid(size/2, speed1);
            Asteroid a2 = new Asteroid(size/2, speed2);
            getWorld().addObject(a1, getX(), getY());
            getWorld().addObject(a2, getX(), getY());        
            a1.move();
            a2.move();
            space.countScore(space.asteroidScore1);
            getWorld().removeObject(this);
        }
        int currentLevelFullScore = space.getTotalAsteroids()*space.oneAsteroidsPoints;

        if(space.getValueFromSpace() == currentLevelFullScore)
        {
            space.increaseGameLevel();
            int asteriodsForNextLevel = space.startAsteroids + space.getGameLevel()*space.asteroidsAddedEachLevel;
            space.addAsteroids(asteriodsForNextLevel);                
            space.totalAsteroidsIncrease(asteriodsForNextLevel);
            Greenfoot.playSound("levelUp.wav");
        }

    }



    public void checkShield()
    {
        Actor shield = getOneIntersectingObject(Shield.class);
        if (shield != null)
        {
            getWorld().removeObject(shield);
            turn(180);
        }
    }
}
ET78759 ET78759

2017/4/11

#
I used some of the code from the Greenfoot book.
danpost danpost

2017/4/11

#
You are calling a method called 'reverseMovement' in the act method, but I do not see it in your code and it is not part of the SmoothMover class (at least, not originally). What does that method do? (show code)
ET78759 ET78759

2017/4/11

#
reverseMovement is in the SmoothMover class and is a feeble attempt at getting it to work.
    public void reverseMovement()
    {
        if(deltaY == 5)
        {
            exactY = -exactY;
        }
    }
danpost danpost

2017/4/11

#
That looks like something you yourself added to the class. The class, however, is not meant to be edited. Move that method into the Asteroid class where it belongs. Also anything else you may have put in the SmoothMover class. What do you think that method is going to do (regardless of which class of the two classes it is in)? and what does deltaY represent? and what else did you have in the SmoothMover class?
ET78759 ET78759

2017/4/12

#
I know, I've been experimenting a little bit with my dad and found out that you do have to edit SmoothMover.(The method above was deleted) Here's the code for SmoothMover now...
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * 
 * @author Edwin
 * 
 * @version 1
 */
public abstract class SmoothMover extends Actor
{
    private Vector velocity;
    private boolean shieldReady;
    private boolean moveBackward;
    private int shieldDelayCount = 500;
  
    private double exactX;
    private double exactY;

    public SmoothMover()
    {
        this(new Vector());
    }

    /**
     * Create new Mover initialised with given velocity.
     */
    public SmoothMover(Vector velocity)
    {
        this.velocity = velocity;
    }
    public void move() 
    {
        if (shieldReady == true)
        {
            moveBackward = true;
        }

        if (moveBackward == true)
        {
            exactX = exactX - velocity.getX();
            exactY = exactY - velocity.getY();
            shieldDelayCount--;
        }
        else
        {
            exactX = exactX + velocity.getX();
            exactY = exactY + velocity.getY();
        }
        if(shieldDelayCount == 0)
        {
            moveBackward = false;
            shieldDelayCount = 500;
        }
        if (exactX >= getWorld().getWidth()) {
            exactX = 0;
        }
        if (exactX < 0) {
            exactX = getWorld().getWidth() - 1;
        }
        if (exactY >= getWorld().getHeight()) {
            exactY = 0;
        }
        if (exactY < 0) {
            exactY = getWorld().getHeight() - 1;
        }
        super.setLocation((int) exactX, (int) exactY);
    }

    /**
     * Set the location using exact (double) co-ordinates.
     */
    public void setLocation(double x, double y) 
    {
        exactX = x;
        exactY = y;
        super.setLocation((int) x, (int) y);
    }

    public void setLocation(int x, int y) 
    {
        exactX = x;
        exactY = y;
        super.setLocation(x, y);
    }


    public double getExactX() 
    {
        return exactX;
    }

    public double getExactY() 
    {
        return exactY;
    }


    public void addToVelocity(Vector boost) 
    {
        velocity.add(boost);
    }

    public void accelerate(double factor)
    {
        velocity.scale(factor);
        if (velocity.getLength() < 0.15) 
        {
            velocity.setNeutral();
        }
    }

    /**
     * Return the speed of this actor.
     */
    public double getSpeed()
    {
        return velocity.getLength();
    }


    public void invertHorizontalVelocity()
    {
        velocity.revertHorizontal();
    }

 
    public void invertVerticalVelocity()
    {
        velocity.revertVertical();
    }


    public Vector getVelocity() 
    {
        return velocity.copy();
    }

    public void setShieldReady(boolean newStatus)
    {
        shieldReady = newStatus;

    }
}
ET78759 ET78759

2017/4/12

#
Again, some of the code was from the book
ET78759 ET78759

2017/4/12

#
And here is the newly edited checkShield method of Asteroid class:
    public void checkShield()
    {
        Actor shield = getOneIntersectingObject(Shield.class);
        if (shield != null)
        {
            getWorld().removeObject(shield);
            setShieldReady(true);
        }
        else
        {
            setShieldReady(false);
        }
    }
danpost danpost

2017/4/12

#
ET78759 wrote...
I know, I've been experimenting a little bit with my dad and found out that you do have to edit SmoothMover.(The method above was deleted)
No -- you do not have to edit SmoothMover. However, if you do, it should only be with the generic moving stuff. The shielding capability is specific to the player and anything related to it should be in the Player classs (that would be line 12 though 14, lines 33 through 52 and lines 138 through 142).
You need to login to post a reply.
1
2