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

2016/5/9

HELP, hoe to move an object based on another.

adesh_dooraree adesh_dooraree

2016/5/9

#
My game is to get to the goal before your opponent. when you reach the goal first you get one point. every time you get a point everyone respawns where they started. I'm using:
public void score()
    {
        
        if(isTouching(Goal.class))
        {
            points = points + 1;
            setLocation(1083,20);
        }
    }
to relocate this player, but the other player still stays. HELP.
danpost danpost

2016/5/9

#
adesh_dooraree wrote...
My game is to get to the goal before your opponent. when you reach the goal first you get one point. every time you get a point everyone respawns where they started. I'm using: < Code Omitted >
Please post your current world class code so that it can be confirmed that it is set up properly to do what you are wanting.
adesh_dooraree adesh_dooraree

2016/5/10

#
public class MyWorld extends World
{

    /**
     * Create a series of barriers for the two players to work their way through.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1100, 600, 1);
        wallRect block;
        Goal block2;
        P1 p1 = new P1();
        addObject(fish, 20, 20);
        P2 p2 = new P2();
        addObject(fish2, 1080, 20);
        GreenfootSound music = new GreenfootSound("bgmusic.mp3");
        if(!music.isPlaying())
        {
            music.play();
        }
        for (int i = 0 ; i < 112 ; i++)
        {
            block = new wallRect() ; 
            addObject(block, 10 * i, 1) ;
        }
        for (int i = 0 ; i < 112 ; i++)
        {
            block = new wallRect();
            addObject(block, 10 * i, 599);
        }
        for (int i = 0 ; i < 62 ; i++)
        {
            block = new wallRect();
            addObject(block, 1, 10 * i);
        }
        for (int i = 0 ; i < 62 ; i++)
        {
            block = new wallRect();
            addObject(block, 1099, 10 * i);
        }
        for (int i = 5 ; i < 50; i++)
        {
            block = new wallRect();
            addObject(block, 10 * i, 30);
        }
        for (int i = 61 ; i < 106 ; i++)
        {
            block = new wallRect();
            addObject(block, 10 * i, 30);
        }
        for (int i = 0 ; i < 26 ; i++)
        {
            block = new wallRect();
            addObject(block, 550, 10 * i);
        }
        for (int i = 36 ; i < 62 ; i++)
        {
            block = new wallRect();
            addObject(block, 550, 10 * i);
        }
        for (int i = 50 ; i < 61 ; i++)
        {
            block = new wallRect();
            addObject(block, 10 * i, 360);
        }
}
}
^ here is my world code. What i want is that i can move another player when this player touches the goal.
danpost danpost

2016/5/10

#
You cannot tell me that this class compiles okay. Both 'fish' (on line 15) and 'fish2' (on line 17) are not defined anywhere. Maybe they are supposed to be your P1 and P2 objects (created on lines 14 and 16) which are not added into the world anywhere. Since you are basically doing a reset when a player touches a goal, the code to perform it should go in the MyWorld class. You can add the following method to it:
public void resetPlayers()
{
    P1 player1 = getObjects(Player1.class).get(0);
    player1.setLocation(20, 20);
    P2 player2 = getObjects(Player2.class).get(0);
    player2.setLocation(1080, 20);
}
Now, when a player touches a goal, you can call this 'score' method
public void score()
{
    if (isTouching(Goal.class))
    {
        points = points+1;
        ((MyWorld)getWorld()).resetPlayers();
    }
}
adesh_dooraree adesh_dooraree

2016/5/10

#
here is my whole file: http://jmp.sh/x3IUjtI please check what you need to. and thank you
adesh_dooraree adesh_dooraree

2016/5/10

#
I have entered in the code, hopefully in the right places but there seem to be a few mistakes, here is my code for my player 1:
public class P1 extends Actor
{
    private int points = 0;
    /**
     * Act - do whatever the Fish wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
   public void act() 
    {
        gameOver();
        p2score();
        score();
        resetPlayers();
              if (Greenfoot.isKeyDown("S"))
        {
            setLocation(getX(), getY() + 3);
            Actor aWallCell = getOneIntersectingObject(wallRect.class) ; 
            if (aWallCell != null)
                setLocation (getX(), getY() - 3);
        }
        if (Greenfoot.isKeyDown("W"))
        {
            setLocation(getX(), getY() - 3);
            Actor aWallCell = getOneIntersectingObject(wallRect.class) ; 
            if (aWallCell != null)
                setLocation (getX(), getY() + 3);
        }
        if (Greenfoot.isKeyDown("D"))
        {
            setLocation(getX() + 3, getY());
            Actor aWallCell = getOneIntersectingObject(wallRect.class) ; 
            if (aWallCell != null)
                setLocation (getX() - 3, getY());
        }
        if (Greenfoot.isKeyDown("A"))
        {
            setLocation(getX() - 3, getY());
            Actor aWallCell = getOneIntersectingObject(wallRect.class) ; 
            if (aWallCell != null)
                setLocation (getX() + 3, getY());
        }
    }  
    
    public void resetPlayers()
    {
    P1 player1 = getObjects(P1.class).get(0);
    player1.setLocation(20, 20);
    P2 player2 = getObjects(P2.class).get(0);
    player2.setLocation(1080, 20);
   }
    
        
        public void score()
            {
    if (isTouching(Goal.class))
    {
        points = points+1;
        ((MyWorld)getWorld()).resetPlayers();
    }
   }
    
    
    public void p2score ()
    {
        if (points== 0)
        {
            getWorld().showText("Player 1 Score: 0", 200, 20);
        }
        if (points== 1)
        {
            getWorld().showText("Player 1 Score: 1", 200, 20);
        }
        if (points== 2)
        {
            getWorld().showText("Player 1 Score: 2", 200, 20);
        }
        if (points== 3)
        {
            getWorld().showText("Player 1 Score: 3", 200, 20);
        }
        if (points== 4)
        {
            getWorld().showText("Player 1 Score: 4", 200, 20);
        }
        if (points== 5)
        {
            getWorld().showText("Player 1 Score: 5", 200, 20);
        }
    }
    public void gameOver ()
    {
        if(points == 5)
        {
            getWorld().showText("Player 2 WINS", 550, 300);
            Greenfoot.setWorld(new p1scrn());
        }
    }
}
adesh_dooraree adesh_dooraree

2016/5/10

#
it cannot find symbols of both getObject in resetplayers(line 46 and 48) and cannot find symbol for resetplayers in score (line 58)
danpost danpost

2016/5/11

#
adesh_dooraree wrote...
it cannot find symbols of both getObject in resetplayers(line 46 and 48) and cannot find symbol for resetplayers in score (line 58)
The 'resetPlayers' method is supposed to be in your MyWorld class.
You need to login to post a reply.