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

2012/4/22

Someone help me fix this problem

2patrickMurphy 2patrickMurphy

2012/4/22

#
Someone else in my group programmed this part and i dont understand how it works. I have an error in one worm that isnt in the other for some strange reason when i copy pasted the code (its pretty much identical). If you can find the error and help me solve it woud be alot of help. Ill email you the program. But basically you have a worm represented with an arraylist. And another. One is moving left to right on the screen the other is moving right to left. They have the exact same methods and everything with very minor differences. Its 4/21 our project is due very shortly so if its after monday dont even bother helping i would of already fixed the problem
IsVarious IsVarious

2012/4/22

#
Can you paste the code you're having an issue with?
2patrickMurphy 2patrickMurphy

2012/4/22

#
sure ill do that too. Ill show you the one that works then the one that doesnt.
This is my act() method {

       if( powerup == true)  
        {  
            powerUp(1);     
        }else{  
            move(-2);
            PowerUp powerUp = (PowerUp)getOneIntersectingObject(PowerUp.class);  
            if(powerUp != null && powerup == false)  
            {                  
                ArrayList<Player2> P2 = (ArrayList<Player2>)getWorld().getObjects(Player2.class);
                int size = P2.size();
                for(int i =0;i<size;i++)
                {
                    Player2 temp = P2.get(i);
                    temp.powerup();
                    temp.time();
                    P2.set(i,temp);
                }                  
            }       
 if((index() == 0)) {  
            controls();  
            bodyCrash();  
        }  
        else if(acts%1 == 0) {  
            slowDown();  
            followTheLeader();              
        }  
        acts++;
}


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

    public void followTheLeader() {
        List<Worm> wormList = getWorld().getObjects(Player2.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 only difference between this code and the working code is all the adding is subtracting and all the move() values are positive. What happens is the Player2 worm links dont follow the front one they just run off in a diangal tangent across the screen while the front worm moves. But if the front worm changes from moving backwards to forwards then they begin to folllow. I need them to follow it as it moves backwords and forwards for my program to work. Thanks for the help i think this is all you should need cuz im fairly certain everything else works.
danpost danpost

2012/4/22

#
Are you going to post the code that does NOT work? It would make sense to do so, as that would be the one that needs fixing. Also, it would be best to post the whole class, and specify whether it is the one going left to right, or right to left. When you post bits and pieces, it is much harder to follow, and it is possible that something there causes a change that affects what is happening (or is NOT happening). The help would be more specific and correct for your needs, also.
2patrickMurphy 2patrickMurphy

2012/4/22

#
i posted the one that doesnt work and it moves from right ot left. And thats why i wanted to email it cuz then i would have to post the WHOLE project in this thread it would be much easier if you guys looked at it and tried to debugg. The problem i fixed when i set the rotation using atan2(y,x) y is 0 and x is -20 so it returns PI then it makes it rotate 180 degrees when it should rotate 0 and follow the leader. But after i fixed that a bunch of other errors occured that i dont even understand or know why its happenening. Like i said it would be 100x more easier if i just email it to somebody and they take a look at it from there.
danpost danpost

2012/4/22

#
I did not ask for the whole project. I only asked for the class. You had said "Ill show you the one that works then the one that doesn't". So, I had assumed what I was looking at was the one that worked.
danpost danpost

2012/4/23

#
OK: Player1 class code is good. Player2 class code should be exactly the same except for references to the player (P1 to P2, Player1 to Player2, and player1 to player2) and the keys (arrows to "wasd") with one important difference. Add this to the Player2 code
public Player2()
{
    setRotation(180);
}
BTW: there were 2 differences that there should not have been between the two classes: 1) Player1 had Math.abs in posFollowTheLeader and Player2 did not (add them in Player2); and 2) Player1 had move(2) in one of the methods near the end of the code and Player2 had move(3) (change Player2 class code move(3) to move(2)). Found one other thing: the angles for the arrow keys are incorrect (right = 0, down = 90, left = 180, and up = 270).
2patrickMurphy 2patrickMurphy

2012/4/23

#
ok thanks thats a much easier fix and ill make all nessciary corrections later when we combine our pieces.
You need to login to post a reply.