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

2012/4/20

ArrayList help

2patrickMurphy 2patrickMurphy

2012/4/20

#
I understand how arraylists work and how they are used. (most of them are lists but i will convert all of them to arraylist afterwards.) This is my question in my program my worm which is an arraylist will move faster when it hits a powerup. But only the front link moves faster the rest of the link stays still. Ill show you the code used to make my worm move and ive tried many solutions and got 0 results.
 public void act() 
    { 
        if( powerup == true)
        {
            powerUp(1);   
        }else{
            move(2);
            PowerUp powerUp = (PowerUp)getOneIntersectingObject(PowerUp.class);
            if(powerUp != null)
            {
                powerup();
                t0 = System.currentTimeMillis();
            }
        }
        if((index() == 0)) {
            controls();
            bodyCrash();
        }
        else if(acts%1 == 0) {
            slowDown();
            followTheLeader();            
        }
        acts++;
    }    

 public void powerUp(int variable)
    {        
        if( System.currentTimeMillis() < (t0 + 5000))
        {
            switch(variable)
            {
                case 1: move(5);
                break;
                case 2: move(-1);
                break;
                default: move(2);
                break;
            }
        }else{
            powerup();
        }
    }

    public boolean detectFront() {
        int x =  1*(int)Math.cos(getRotation());
        int y = -1*(int)Math.sin(getRotation());
        if((getOneObjectAtOffset(x, y, Worm.class)) != null)
        {
            return true;
        }else{ 
            return false;
        }
    }

    public void followTheLeader() {
        List<Worm> wormList = getWorld().getObjects(Worm.class);
        int x = wormList.get(index()-1).getX() - getX();
        int y = (wormList.get(index()-1).getY() - getY());
        setRotation((int) (180 * Math.atan2(y, x) / Math.PI));
    }


the powerup() method is same thing as powerup= !powerup; Some of these things will be changed later and some wont so i just need to know whats wrong here. Why isnt my act method working for all links of my array list. Btw if i start at high speed it follows appropriately it only problem when i change speeds.
danpost danpost

2012/4/20

#
I do not know if this will help, but on line 19 you have 'else if (acts%1 == 0)' which is the same as 'else if(true)' (any integer divided by one (1) will have zero (0) remainder).
2patrickMurphy 2patrickMurphy

2012/4/21

#
Oh and a new problem is this program has crashed my labtop and my dads desktop
2patrickMurphy 2patrickMurphy

2012/4/21

#
everything fixed. I needed to iterate through each element of my arraylist and apply the timeframe and the boolean variable
You need to login to post a reply.