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

2013/12/11

change the order of acting objects

Jtlie Jtlie

2013/12/11

#
Hi, I think I have a little problem with the order my Objects are acting. I want the location of 2 objects that are next to each other to move when I press "right" or "left". So I set the location X + or - 100. When it collides, I want it to stay at the same spot so I put in the opposite ( -100 for right & + 100 for left). My problem is that when I press right, only the one on the right side moves. I think its because its checks the conditions of the left object first which makes it collide with the right object. Anyone got a idea how to fix this? This is my code:
public class Order extends World
{
    boolean spawning = false;
    
    public Order()
    {    
        
        super(760, 760, 1); 
        prepare();
        plaats();
        Greenfoot.setSpeed(40);   
    }

    private void prepare()
    {
        Geel geel = new Geel();
        addObject(geel, 130, 745);
        Blauw blauw = new Blauw();
        addObject(blauw, 230, 745);
        Rood rood = new Rood();
        addObject(rood,330, 745);
        Groen groen = new Groen();
        addObject(groen, 430, 745);
        Paars paars = new Paars();
        addObject(paars, 530, 745);
        Grijs grijs = new Grijs();
        addObject(grijs, 630, 745);
        Muur muur = new Muur();
        addObject(muur, 720, 380);
        Muur muur2 = new Muur();
        addObject(muur2, 40, 380);
    }
    public void plaats()
    {
        Container_Blauw container_blauw1 = new Container_Blauw();
        Container_Geel container_geel1 = new Container_Geel();
        Container_Grijs container_grijs1 = new Container_Grijs();
        Container_Groen container_groen1 = new Container_Groen();
        Container_Rood container_rood1 = new Container_Rood();
        Container_Paars container_paars1 = new Container_Paars();
        
        Container_Blauw container_blauw2 = new Container_Blauw();
        Container_Geel container_geel2 = new Container_Geel();
        Container_Grijs container_grijs2 = new Container_Grijs();
        Container_Groen container_groen2 = new Container_Groen();
        Container_Rood container_rood2 = new Container_Rood();
        Container_Paars container_paars2 = new Container_Paars();
        
            
            int random1 = Greenfoot.getRandomNumber(5);
            int random2 = Greenfoot.getRandomNumber(5) + 1;
            while (random1 == random2)
            {
                random1 = Greenfoot.getRandomNumber(5);
                random2 = Greenfoot.getRandomNumber(5) + 1;
            }
            
            switch(random1)
            {
                case 0: addObject(container_geel1, 330, 50); break;
                case 1: addObject(container_blauw1, 330, 50);break;
                case 2: addObject(container_paars1, 330, 50); break;
                case 3: addObject(container_groen1, 330, 50); break;
                case 4: addObject(container_rood1, 330, 50);break;
            }
            switch(random2)
            {
                case 1: addObject(container_blauw2, 430, 50); break;
                case 2: addObject(container_paars2, 430, 50); break;
                case 3: addObject(container_groen2, 430, 50); break;
                case 4: addObject(container_rood2, 430, 50);break;
                case 5: addObject(container_grijs2, 430, 50); break;
            }
    }
}

And the code I use for the Actors:
public class Container_Paars extends Containers
{
    int speed = 5;
    boolean stil = false;

    
    public void act() 
    {   
        setLocation(getX(), getY() + speed);
        if(Greenfoot.isKeyDown("right")&& stil == false)
        {
            setLocation(getX() + 100, getY());
        }
        if(Greenfoot.isKeyDown("left") && stil == false)
        {
            setLocation(getX() - 100, getY());
           
        }
        if(Greenfoot.isKeyDown("down") && stil == false)
        {
            speed = 20;
        }
        else
        {
            speed = 5;
        }
        if(Greenfoot.isKeyDown("space") && getX() == 530 && stil == true)
        {
           ((Order)getWorld()).removeObject(this);
           return;
        }
        stilstand();
    }    
    
    public void stilstand()
    {
           Actor collide = getOneIntersectingObject(Muur.class);
           if (collide != null)
           {
               if(Greenfoot.isKeyDown("right")&& stil == false)
               {
                   setLocation(getX() - 100, getY());
               }
               if(Greenfoot.isKeyDown("left")&& stil == false)
               {
                   setLocation(getX() + 100, getY());
               }
           }
           Actor collide1 = getOneIntersectingObject(Containers.class);
           if (collide1 != null)
           {
               if(Greenfoot.isKeyDown("right")&& stil == false)
               {
                   setLocation(getX() - 100, getY());
               }
               else if(Greenfoot.isKeyDown("left")&& stil == false)
               {
                   setLocation(getX() + 100, getY());
               }
               else
               {
                   speed = 5;
                   setLocation(getX(), getY()-5);

                   if(stil == false)
                   {
                       stil = true;
                       if(getWorld().getObjectsAt(330, 50, null).isEmpty() && getWorld().getObjectsAt(430, 50, null).isEmpty() && ((Order)getWorld()).spawning == true)  
                       {
                            ((Order)getWorld()).spawning = false;
                            ((Order)getWorld()).plaats();
                       }
                       else
                       {
                           ((Order)getWorld()).spawning = true;
                       }
                   }
               }
           }
           Actor collide2 = getOneIntersectingObject(Plaatsen.class);
           if (collide2 != null)
           {
               speed = 0;
               setLocation(getX(), getY() - 5);
               if(stil == false)
               {
                   stil = true;
                   if(getWorld().getObjectsAt(330, 50, null).isEmpty() && getWorld().getObjectsAt(430, 50, null).isEmpty() && ((Order)getWorld()).spawning == true)  
                   {
                       ((Order)getWorld()).plaats();
                       ((Order)getWorld()).spawning = false;
                   }
                   else
                   {
                       ((Order)getWorld()).spawning = true;
                   }
               }
           }
    }
}
danpost danpost

2013/12/11

#
You would have to link the pairs so that one 'knows' about the other. By their x locations and the direction of user input, you can determine which to move first. Move both at the same time (do not have both moved individually by their own act method calls).
// instance field
Actor pair; // to be set in the constructor --
// or by calling a 'set' method after creation
// in act or method it calls
if (pair.getX() < getX() && Greenfoot.isKeyDown("right") && stil == false)
{
    // check and move 'this' first
    // if 'this' moved, move 'pair'
}
if (pair.getX() > getX() && Greenfoot.isKeyDown("left") && stil == false)
{
    // check and move 'pair' first
    // if 'pair' moved, move 'this'
}
I hope you can understand what would be happening here. Also, when you change the value of 'stil' for one, you should also change the value of 'stil' for its 'pair'.
You need to login to post a reply.