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

2016/6/9

Issue with object going backwards

CuddlySpartan CuddlySpartan

2016/6/9

#
So I have a tower defense type game where you defend against Octopuses. I added some CamoOctopuses that when they are killed they spawn a regular Octopus in the same spot that they are in. However, if the CamoOctopus is moving left or up when it is hit, the Octopus that is spawned will move backwards instead of the direction it is supposed to be going. Here is my code for the CamoOctopus' movement:
public void followLine() 
    {
            Color Blue = new Color(0,0,255,255);    
            if(getWorld().getColorAt(getX()+1, getY()).getRed() < 100&&getWorld().getColorAt(getX()+1, getY()).getGreen() < 100&& x !=-1)      
            {      
                //right
                setLocation(getX() + 1,getY());
                y = 0;
                x = 1;
            }      
            else if(getWorld().getColorAt(getX()-1, getY()).getRed() < 100&& getWorld().getColorAt(getX()-1, getY()).getGreen() < 100&& x !=1)      
            {      
                //left
                setLocation(getX()-1, getY());
                y = 0;
                x = -1;
            } 
            else if(getWorld().getColorAt(getX(), getY()+1).getRed() < 100&& getWorld().getColorAt(getX(), getY()+1).getGreen() < 100 &&y != -1)      
            {      
                //down
                setLocation(getX(), getY()+1);
                x = 0;
                y = 1;
            }
            else if(getWorld().getColorAt(getX(), getY()-1).getRed() < 100&&getWorld().getColorAt(getX(), getY()-1).getGreen() < 100&&y !=1)      
            {      
                //up
                setLocation(getX(), getY()-1);
                x = 0;
                y = -1;
            }
    } 
Here is code for the bullet that hits the CamoOctopus: public void death() { Actor o = getOneIntersectingObject(CamoOctopus.class); if (o!=null) { Money.money+=50; getWorld().addObject(new Octopus(),o.getX(),o.getY()); getWorld().removeObject(o); getWorld().removeObject(this); } } And the octopus moves using the same code as the camoOctopus. Thanks!
danpost danpost

2016/6/9

#
It sounds like you need a field to track the direction of the actor (forward or backward). A boolean type field would be sufficient; but, an 'int' type field would be easier to work with as you can use '1' or '-1' as its value and multiply the 'x' and 'y' values by its value to have the direction reverse.
CuddlySpartan CuddlySpartan

2016/6/9

#
Okay but once I am tracking the direction how would I be able to use that to change the direction of the new Octopus? Because I know how I could do that if I was using regular movement but because I'm using the line following movement I'm not too sure how to.
danpost danpost

2016/6/9

#
I might have been slightly over-thinking it myself. You may be able to just multiply both 'x' and 'y' by '-1' after creating one that tends to move in reverse:
Octopus octopus = new Octopus();
if (x+y < 0) // up or left
{
    octopus.x *= -1;
    octopus.y *= -1;
}
getWorld().addObject(octopus, getX(), getY());
More to the point might be to unconditionally set them to that of the actor it is replacing:
Octopus octopus = new Octopus();
octopus.x = x;
octopus.y = y;
getWorld().addObject(octopus, getX(), getY());
CuddlySpartan CuddlySpartan

2016/6/10

#
Okay so I tried that but it didn't quite work. When the bullet hit the camooctopus, the octopus would spawn not on the line so it wouldn't be able to move. It spawned below where the original camooctopus was.
danpost danpost

2016/6/10

#
CuddlySpartan wrote...
Okay so I tried that but it didn't quite work. When the bullet hit the camooctopus, the octopus would spawn not on the line so it wouldn't be able to move. It spawned below where the original camooctopus was.
Sorry -- I wrote that out a bit quickly. Line 4 above should be:
getWorld().addObject(octopus, o.getX(), o.getY());
You need to login to post a reply.