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

2012/8/4

Enemie code?

1
2
3
4
SPower SPower

2012/8/4

#
That's in the SmoothMover class in my Crabs on the beach scenario:
/**
     * Test if we are close to one of the edges of the world. Return true if we are.
     */
    public boolean atWorldEdge()
    {
        if (getX() <= 1 || getX() >= getWorld().getWidth() - 1) {
            return true;
        }
        if (getY() <= 1 || getY() >= getWorld().getHeight() - 1 ) {
            return true;
        } else {
            return false;
        }
    }
CrazyGamer1122 CrazyGamer1122

2012/8/4

#
i put the smoothmover class into the rocket thing and put enemie as a sub-class but right here---
List<Rocket> ships = getObjectsInRange(100,Rocket.class);
it says "cannot find symbol - class List"
SPower SPower

2012/8/4

#
import List:
import java.util.List;
CrazyGamer1122 CrazyGamer1122

2012/8/4

#
thanks.
CrazyGamer1122 CrazyGamer1122

2012/8/4

#
now it says "cannot find symbol - method moveHorizontally(double)"
moveHorizontally(5.0);
SPower SPower

2012/8/4

#
You probably copied the code of Crab without copying the methods it uses. The moveHorizontally method is in SmoothMover (I think). Copy that, and the error should be gone.
CrazyGamer1122 CrazyGamer1122

2012/8/4

#
it's not in smooth mover and when i try to open crabs on the beach it says "unable to open archive" can you copy and paste the code for me?
SPower SPower

2012/8/4

#
All the code of SmoothMover:
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * A variation of an actor that maintains a precise location (using doubles for the co-ordinates
 * instead of ints).  This allows small precise movements (e.g. movements of 1 pixel or less)
 * that do not lose precision.
 * 
 * @author Poul Henriksen
 * @author Michael Kolling
 * @author Neil Brown
 * @author Sven van Nigtevecht
 * 
 * @version 3.0.1
 */
public abstract class SmoothMover extends Actor
{
    private double exactX;
    private double exactY;
    
    /**
     * Move forward by the specified distance.
     * (Overrides the method in Actor).
     */
    public void moveHorizontally(int distance)
    {
        moveHorizontally((double)distance);
    }
    
    /**
     * Move forward by the specified exact distance.
     */
    public void moveHorizontally(double distance)
    {
        double radians = Math.toRadians(getRotation());
        double dx = Math.cos(radians) * distance;
        double dy = Math.sin(radians) * distance;
        setLocation(exactX + dx, exactY + dy);
    }
    
    /**
     * Move up or down.
     */
    public void moveVertically(int distance)
    {
        moveVertically((double) distance);
    }
    
    /**
     * Move up or down, really precise.
     */
    public void moveVertically(double distance)
    {
        double radians = Math.toRadians(getRotation());
        setLocation(exactX, exactY - distance);
    }
    
    /**
     * Set the location using exact coordinates.
     */
    public void setLocation(double x, double y) 
    {
        exactX = x;
        exactY = y;
        super.setLocation((int) (x + 0.5), (int) (y + 0.5));
    }
    
    /**
     * Set the location using integer coordinates.
     * (Overrides the method in Actor.)
     */
    @Override
    public void setLocation(int x, int y) 
    {
        exactX = x;
        exactY = y;
        super.setLocation(x, y);
    }
    
    /**
     * Return the exact x-coordinate (as a double).
     */
    public double getExactX() 
    {
        return exactX;
    }
    
    /**
     * Return the exact y-coordinate (as a double).
     */
    public double getExactY() 
    {
        return exactY;
    }
    
    /**
     * Return true if we can see an object of class 'clss' right where we are. 
     * False if there is no such object here.
     */
    public boolean canSee(Class clss)
    {
        return (getOneIntersectingObject(clss) != null);
    }
    
    /**
     * Try to eat an object of class 'clss'. This is only successful if there
     * is such an object where we currently are. Otherwise this method does
     * nothing.
     */
    public void eat(Class clss)
    {
        Actor actor = getOneIntersectingObject(clss);
        if(actor == null) return;
        getWorld().removeObject(actor);
    }
    
    /**
     * Test if we are close to one of the edges of the world. Return true is we are.
     */
    public boolean atWorldEdge()
    {
        if (getX() <= 1 || getX() >= getWorld().getWidth() - 1) {
            return true;
        }
        if (getY() <= 1 || getY() >= getWorld().getHeight() - 1 ) {
            return true;
        } else {
            return false;
        }
    }
}
SPower SPower

2012/8/4

#
And about that you're unable to open it: on Windows, you have the problem that when you say 'open' instead of 'save', it saves it in the same folder. When you try to download and open again, Greenfoot sees you already have it in that folder, and Greenfoot won't let you open it. My advice: really save the scenario when you download one, and remove it when you're done with it :)
CrazyGamer1122 CrazyGamer1122

2012/8/4

#
thanks.... the code all compiles now but the enemie instanly transports to the top right corner and gets stuck in it.
SPower SPower

2012/8/4

#
Hmm, can you upload it? Easier to debug for me...
CrazyGamer1122 CrazyGamer1122

2012/8/4

#
"instantly" typo
CrazyGamer1122 CrazyGamer1122

2012/8/4

#
yeah ill upload it.....
CrazyGamer1122 CrazyGamer1122

2012/8/4

#
it's up..... just post the new source here (please comment so i can understand the code)
SPower SPower

2012/8/4

#
The wrong thing is in the Enemie class. You've got this code:
public void moveHorizontally(double distance)  
    {
        double radians = Math.toRadians(getRotation());
        double dx = Math.cos(radians) * distance;
        double dy = Math.sin(radians) * distance;
        setLocation(exactX + dx, exactY + dy);
    }
But you must use the exactX and exactY which SmoothMover gave you, you musn't re-create them in Enemy. Change the method to this:
public void moveHorizontally(double distance)  
    {
        double radians = Math.toRadians(getRotation());
        double dx = Math.cos(radians) * distance;
        double dy = Math.sin(radians) * distance;
        setLocation(getExactX() + dx, getExactY() + dy);
    }
And remove those lines from Enemy:
private double exactX;
private double exactY;
A little tip, copy all the code I gave you of SmoothMover and paste it into the SmoothMover of you, because I added some stuff.
There are more replies on the next page.
1
2
3
4