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

2012/5/29

Object Chain Movement

1
2
3
kartikitrak kartikitrak

2012/5/30

#
Thanks MatheMagician. I'd assume making curves would be very hard. If I were to try making curves, what steps would I need to take.
erdelf erdelf

2012/5/30

#
nice, look at world2. the chain turns direction
MatheMagician MatheMagician

2012/5/30

#
Yes Erdelf, that is because the line is two pixels in width not just one. I noticed it but thought it actually looked pretty cool and kept it. kartikitrak, instead of this:
if(getWorld().getColorAt(getX()+1, getY()).getRed() < 100&&getWorld().getColorAt(getX()+1, getY()).getGreen() < 100&& x !=-1)        
            {        
                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)        
            {        
                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)        
            {        
                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)        
            {        
                setLocation(getX(), getY()-1);     
                x = 0;  
                y = -1;  
            }  
We need this:
if(getWorld().getColorAt(getX()+1, getY()).getRed() < 100&&getWorld().getColorAt(getX()+1, getY()).getGreen() < 100&& x !=-1)      
            {      
                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)      
            {      
                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)      
            {      
                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)      
            {      
                setLocation(getX(), getY()-1);   
                x = 0;
                y = -1;
            }
            else if(getWorld().getColorAt(getX()+1,getY()+1).getRed()< 100&&getWorld().getColorAt(getX()+1,getY()+1).getGreen()<100&&x!=-1&&y!=-1)      
            {      
                setLocation(getX() + 1,getY()+1);    
                y = 1;
                x = 1;
            }      
            else if(getWorld().getColorAt(getX()-1, getY()-1).getRed() < 100&& getWorld().getColorAt(getX()-1, getY()-1).getGreen()<100&&x!=1&&y!=1)      
            {      
                setLocation(getX()-1, getY()-1);      
                y = -1;
                x = -1;
            } 
           
            else if(getWorld().getColorAt(getX()-1, getY()+1).getRed() < 100&& getWorld().getColorAt(getX()-1, getY()+1).getGreen() < 100 &&y!=-1&&x!=1)      
            {      
                setLocation(getX()-1, getY()+1);      
                x = -1;
                y = 1;
            }
            else if(getWorld().getColorAt(getX(), getY()-1).getRed() < 100&&getWorld().getColorAt(getX(), getY()-1).getGreen() < 100&&y !=1&&x !=-1)      
            {      
                setLocation(getX()+1, getY()-1);   
                x = 1;
                y = -1;
            }
This new code also detects the diagonals.
kartikitrak kartikitrak

2012/5/30

#
THANKS A BUNCH! I WILL MAKE SURE I CREDIT YOU IN THE PROJECT I'M WORKING ON! ALSO, WHAT DOES IT MEAN WHEN THE ORB JUST STOPS HALF WAY THROUGH A CURVE? Is it because there is more than one pixel, no pixel, or another reason.
MatheMagician MatheMagician

2012/5/30

#
Your welcome! And the reason the body stops during the curve is because my x,y parameters are too stiff. However, once you fix them (which I have done below), the angle of the pixels cannot be more than 90 degrees. That is where the parts are being hung up. So you must be very careful while creating your pictures.
if(getWorld().getColorAt(getX()+1, getY()).getRed() < 100&&getWorld().getColorAt(getX()+1, getY()).getGreen() < 100&& x !=-1)      
            {      
                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)      
            {      
                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)      
            {      
                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)      
            {      
                setLocation(getX(), getY()-1);   
                x = 0;
                y = -1;
            }
            else if(getWorld().getColorAt(getX()+1,getY()+1).getRed()< 100&&getWorld().getColorAt(getX()+1,getY()+1).getGreen()<100&&(x!=-1||y!=-1))      
            {      
                setLocation(getX() + 1,getY()+1);    
                y = 1;
                x = 1;
            }      
            else if(getWorld().getColorAt(getX()-1,getY()-1).getRed() < 100&& getWorld().getColorAt(getX()-1, getY()-1).getGreen()<100&&(x!=1||y!=1))      
            {      
                setLocation(getX()-1, getY()-1);      
                y = -1;
                x = -1;
            } 
            else if(getWorld().getColorAt(getX()-1,getY()+1).getRed()<100&&getWorld().getColorAt(getX()-1,getY()+1).getGreen()<100 &&(y!=-1||x!=1))      
            {      
                setLocation(getX()-1, getY()+1);      
                x = -1;
                y = 1;
            }
            else if(getWorld().getColorAt(getX(), getY()-1).getRed() < 100&&getWorld().getColorAt(getX(), getY()-1).getGreen() < 100&&(y !=1||x !=-1))      
            {      
                setLocation(getX()+1, getY()-1);   
                x = 1;
                y = -1;
            }
kartikitrak kartikitrak

2012/5/30

#
If the world is black, the orbs just moves horizontally across the screen. Also if I were to change the line colour how would I adjust the code. I know right now you're checking the red value and the green value but what about the blue and can I modify the code to use the Color Blue = new Color(0,0,255,255); and simply check if the ColorAt is equal to blue?
MatheMagician MatheMagician

2012/5/30

#
Well you see if you use Color Blue = new Color(0,0,255,255), the color must be EXACTLY 255 blue. However, when you draw images, this doesn't always work. So I used this command
color.getRed();
This code gets the RGB value of the red. You can do the same with blue and green and possibly the alpha value, though I am not sure about that one. You can modify the statement:
getWorld().getColorAt(getX(), getY()-1).getRed() < 100
To
getWorld().getColorAt(getX(), getY()-1).getRed() > 100
All depending on what you want. You can to the same with getGreen(). If you are writing everything on a black background, you probably want to make the above transformation.
kartikitrak kartikitrak

2012/5/31

#
So, I've made the code allow the line to be on a black background by making the colours it requires to be more specific however I don't see why the body doesn't not go up curves. It goes down the curves perfectly fine but I've traced through the code and I don't know why it's not letting the body go up curves. Up curves would be: http://i47.tinypic.com/ildjmh.png
erdelf erdelf

2012/5/31

#
he said it. it can't be higher then 90 degrees until you changes the code
kartikitrak kartikitrak

2012/5/31

#
Yes but even smaller includes, just one pixel above just stop the movement. That's what I'm confused about.
erdelf erdelf

2012/5/31

#
ok post me your code. I will change it. if you make it in the next 10 minutes I will make it now. if later you have to wait 16 hours
kartikitrak kartikitrak

2012/5/31

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class Enemy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Enemy extends Actor
{
    private Color Blue = new Color(0,0,255,255);  
    private int x,y=0;

    public void act() 
    {
        // Add your action code here.
    }    

    public void movement()
    {
        //if(x == 1)
        if(getWorld().getColorAt(getX()+1, getY()).getRed() < 100&&getWorld().getColorAt(getX()+1, getY()).getGreen() < 100 &&getWorld().getColorAt(getX()+1, getY()).getBlue() >250 && x !=-1)      
        {      
            setLocation(getX() + 1,getY());    
            y = 0;
            x = 1;
        }      
        else if(getWorld().getColorAt(getX()-1, getY()).getRed() < 100&& getWorld().getColorAt(getX()-1, getY()).getGreen() < 100&& getWorld().getColorAt(getX()-1, getY()).getBlue() > 250&& x !=1)      
        {      
            setLocation(getX()-1, getY());      
            y = 0;
            x = -1;
        } 

        else if(getWorld().getColorAt(getX(), getY()+1).getRed() < 100&& getWorld().getColorAt(getX(), getY()+1).getGreen() < 100&& getWorld().getColorAt(getX(), getY()+1).getBlue() > 250 &&y != -1)      
        {      
            setLocation(getX(), getY()+1);      
            x = 0;
            y = 1;
        }
        else if(getWorld().getColorAt(getX(), getY()-1).getRed() < 100&&getWorld().getColorAt(getX(), getY()-1).getGreen() < 100&&getWorld().getColorAt(getX(), getY()-1).getBlue() > 250&&y !=1)      
        {      
            setLocation(getX(), getY()-1);   
            x = 0;
            y = -1;
        }
        else if(getWorld().getColorAt(getX()+1,getY()+1).getRed()< 100&&getWorld().getColorAt(getX()+1,getY()+1).getGreen()<100&&getWorld().getColorAt(getX()+1,getY()+1).getBlue() > 250&&x!=-1&&y!=-1)      
        {      
            setLocation(getX() + 1,getY()+1);    
            y = 1;
            x = 1;
        }      
        else if(getWorld().getColorAt(getX()-1, getY()-1).getRed() < 100&& getWorld().getColorAt(getX()-1, getY()-1).getGreen()<100&& getWorld().getColorAt(getX()-1, getY()-1).getBlue() > 250&&x!=1&&y!=1)      
        {      
            setLocation(getX()-1, getY()-1);      
            y = -1;
            x = -1;
        } 

        else if(getWorld().getColorAt(getX()-1, getY()+1).getRed() < 100&& getWorld().getColorAt(getX()-1, getY()+1).getGreen() < 100&& getWorld().getColorAt(getX()-1, getY()+1).getBlue() > 250 &&y!=-1&&x!=1)      
        {      
            setLocation(getX()-1, getY()+1);      
            x = -1;
            y = 1;
        }
        else if(getWorld().getColorAt(getX(), getY()-1).getRed() < 100&&getWorld().getColorAt(getX(), getY()-1).getGreen() < 100&&getWorld().getColorAt(getX(), getY()-1).getBlue() > 250&&y !=1&&x !=-1)      
        {      
            setLocation(getX()+1, getY()-1);   
            x = 1;
            y = -1;
        }
    }
}
erdelf erdelf

2012/5/31

#
needs a bit time, give a few minutes
erdelf erdelf

2012/5/31

#
hm, not so easy as I thought. I will look if one can break the cells
kartikitrak kartikitrak

2012/5/31

#
Ok. Do you know why it is not allowing the 90 degrees raises. If my tracing is correct, it allows it.
There are more replies on the next page.
1
2
3