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

2012/12/11

Creating Objects

Johnathon Johnathon

2012/12/11

#
So I've been working on a game in greenfoot for my java class and I've come to a new crossroad that I cannot understand what to do with. I have an actor named Teemo. Teemo has a subclass named Dart. My goal is to make it so when q is pressed on the keyboard A dart is created at teemo's position and flies the direction he is looking to the edge of the screen. Within the class teemo I have this:
    public void animate()
    {
            if (Greenfoot.isKeyDown("q"))
            {
                firedart();
            }
and this:
    public void firedart()
    {
        int x = getX();
        int y = getY();
        addObject(new dart(x,y));   
    }
What I 'thought' this would do would create actor Dart on the position of teemo. I believe I know how to make the dart move to the edge and remove itself but I cannot get it to be created. Thanks ahead of time.
davemib123 davemib123

2012/12/11

#
you could try something: this will be property:
private int DartLimiter = 0;
this will be method for class
if (Greenfoot.isKeyDown("space") && DartLimiter > 8){

            DartLimiter = 0;
            DartThrow fire = new DartThrow();
            getWorld().addObject(fire, getX() + xPosition, getY() + yPosition);
        }
Zamoht Zamoht

2012/12/11

#
You might want to add an if statement that added 1 to DartLimiter if DartLimiter is lower or equal 8.
Johnathon Johnathon

2012/12/13

#
So I continued this code, I didn't really need the delimiter thing, however I did need this line here "getWorld().addObject(fire, getX() + xPosition, getY() + yPosition); " as that was what I was messing up. Thanks for that! My next problem is the fact that with the current code, the darts will change direction so long as teemo does, which is problematic. here is the current code for within the dart. The call for the variable within class Teemo is simply just if he last move left it is true, if not it is false.
    public void act() 
    {
        int direction = 1;
        World world;
        world = getWorld();
        if (Teemo.facingLeft){
            direction = -1;
            getImage().mirrorHorizontally();
        }
        if (!Teemo.facingLeft){
            direction = 1;
        }
        if (getX() <= 5)  
        {  
            world.removeObject(this);  
        }  
        else if (getY() <= 5)  
        {  
            world.removeObject(this);  
        }  
        else if (getX() >= getWorld().getWidth() - 5)  
        {  
            world.removeObject(this); 
        }  
        else if (getY() >= getWorld().getHeight() - 5)  
        {  
            world.removeObject(this);  
        }
        else move((direction * 5));
    }    
}
Zamoht Zamoht

2012/12/13

#
One thing. Change getImage().mirrorHorizontally(); to setImage(getImage().mirrorHorizontally()); This line is not needed since the direction is already equal to 1. if (!Teemo.facingLeft){ direction = 1; } As well I don't get how this works Teemo.facingLeft if you're not ending it with () like Teemo.facingLeft(). I don't know if this helps, but well maybe is does.
danpost danpost

2012/12/13

#
To get the dart to face the right direction initially, just change your 'fire' metho to:
public void firedart()
{
    int x = getX();
    int y = getY();
    dart newDart = new dart();
    getWorld().addObject(newDart,x,y);
    newDart.setRotation(getRotation());
}
Then you will not need a 'direction' variable or image altering in the dart class. In fact, the dart 'act' method can simply be:
public void act()
{
    move(5);
    if(getX()<5 || getY()<5 || getX()>getWidth()-5 || getY()>getHeight()-5) getWorld().removeObject(this);
}
Zamoht Zamoht

2012/12/13

#
Oh of course. To be honest I didn't understand the question, but danpost's code is definitely an easier way to approach this problem.
You need to login to post a reply.