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

2013/6/16

Access Method from another Class

Barador Barador

2013/6/16

#
Hi! I've got a problem in the game I'm working on. I have a class named Bomb and another one named Players. In Bomb I want to access a mehod bombExploded(int player) from Players. I have this in Bomb:
Players players = new Players();
           if (player == 1)
           {
               players.bombExploded(1);
           }
And this in Players:
public void bombExploded(int player)
    {
        if (player == 1)
        {
            bombsLaid1--;
        }
    }
Nohing happens when the Code is executed. Any suggestions? Greetings, Leon
Gevater_Tod4711 Gevater_Tod4711

2013/6/16

#
I think the problem is that you create a new instance of the class Players and execute the method bombExploded(int player) for this instance and this instance is never added to the world. So the method get's executed but you just don't see it. You probably want to execute the method bombExploded(int player) for an Players object that is in the world. Therefore you need the reference to the object in the world. Have a look at this tutorial. That may help you.
Barador Barador

2013/6/16

#
Thanks. I've done it following the Tutorial Steps and Bomb accesses the Method like it's supposed to do. But now my Player1, 2 etc. don't do anything. I changed their code like this:
BomberWorld bomberWorld = (BomberWorld) getWorld();  // get a reference to the world
        Players players = bomberWorld.getPlayers();
        players.control(1);
But they don't react to the input like they should.
Gevater_Tod4711 Gevater_Tod4711

2013/6/16

#
Can you post the rest of your code? Without knowing what this methods do (or should do) I can't help you.
danpost danpost

2013/6/16

#
I do not understand how you are keeping track of which player is which and which one is detonating the bombs. Please explain how you are doing this.
Barador Barador

2013/6/17

#
I have a Class Players, which has 4 Subclasses Named Player1, Player2,etc.. These only access one method from Players (control(int player)) which does all the control and other stuff connected to the players. Should I put all these methods from Players in the Player classes and access them to manipulate the integer bombsLaid?
danpost danpost

2013/6/17

#
What is the code for the method 'control(int)'.
Barador Barador

2013/6/17

#
public void control(int player)
    {
        if (player==1)
        {
            if(Greenfoot.isKeyDown("d"))
            {
                move(1);
                animate(player,"R");
            }
            if(Greenfoot.isKeyDown("s"))
            {
                move(2);    
                animate(player,"F");
            }
            if(Greenfoot.isKeyDown("a"))
            {
                move(3);    
                animate(player,"L");
            }
            if(Greenfoot.isKeyDown("w"))
            {
                move(4);  
                animate(player,"B");
            }
            if(Greenfoot.isKeyDown("shift"))
            {
                dropBomb(radius1,1);
            }
            pickItem(1);
        }
        if (player==2)
        {
            if(Greenfoot.isKeyDown("j"))
            {
                move(1);  
                animate(player,"R");
            }
            if(Greenfoot.isKeyDown("h"))
            {
                move(2);   
                animate(player,"F");
            }
            if(Greenfoot.isKeyDown("g"))
            {
                move(3);    
                animate(player,"L");
            }
            if(Greenfoot.isKeyDown("z"))
            {
                move(4);    
                animate(player,"B");
            }
            if(Greenfoot.isKeyDown("v"))
            {
                dropBomb(radius2,2);
            }
            pickItem(2);
        } 
        if (player==3)
        {
            if(Greenfoot.isKeyDown("right"))
            {
                move(1);    
                animate(player,"R");
            }
            if(Greenfoot.isKeyDown("down"))
            {
                move(2);    
                animate(player,"F");
            }
            if(Greenfoot.isKeyDown("left"))
            {
                move(3);    
                animate(player,"L");
            }
            if(Greenfoot.isKeyDown("up"))
            {
                move(4);    
                animate(player,"B");
            }
            if(Greenfoot.isKeyDown("control"))
            {
                dropBomb(radius3,3);
            }
            pickItem(3);
        } 
        if (player==4)
        {
            if(Greenfoot.isKeyDown("6"))
            {
                move(1);    
                animate(player,"R");
            }
            if(Greenfoot.isKeyDown("2"))
            {
                move(2);    
                animate(player,"F");
            }
            if(Greenfoot.isKeyDown("4"))
            {
                move(3);    
                animate(player,"L");
            }
            if(Greenfoot.isKeyDown("8"))
            {
                move(4);    
                animate(player,"B");
            }
            if(Greenfoot.isKeyDown("enter"))
            {
                dropBomb(radius4,4);
            }
            pickItem(4);
        }
    }
Barador Barador

2013/6/17

#
Ok I did it on my own. I copied all the general code with this "int player" in it, into the Player1, etc. classes and accessed them from Bomb the way it's described in the tutorial which Gevater_Tod posted. Thanks anyways for the help.
You need to login to post a reply.