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

2014/3/27

Controlling the movement of 2 objects with 1 class

patrick26 patrick26

2014/3/27

#
Hi, is there a way to control the movement of two objects using 1 class? With the code below, when I press the up arrow key both of the paddles move so is it possible for the up and down arrow key to control one of the objects and the 'w' and 's' keys to control the movement of the other object using the same class I have below? public class Paddle extends Actor { public Paddle() { GreenfootImage pad = new GreenfootImage(10, 100); pad.fill(); setImage(pad); } */ public void act() { if(Greenfoot.isKeyDown("up")) { setLocation(getX(),getY()-5); } if(Greenfoot.isKeyDown("down")) { setLocation(getX(),getY()+5); } } }
danpost danpost

2014/3/27

#
Yes, using your class; but with slight modifications. You just need to refer to the keys using String fields and assign them differently for each paddle. This would probably require you to pass the keys to the paddles when you create them using the constructor signature of 'Paddle(String, String)'. The constructor would store the keys in the fields and the movement code would check for the keys with those values to be down.
patrick26 patrick26

2014/3/27

#
Can you give me a quick start as to how to do this?
danpost danpost

2014/3/27

#
I added some more to the post. Did you see that?
patrick26 patrick26

2014/3/27

#
Yes, but I am still quite confused. Can you give me an example as to how to refer to the keys using String fields?
danpost danpost

2014/3/27

#
"up" and "down" are String literals. You can assign them to variables or fields using
1
2
String upKey = "up";
String downKey = "down";
You could assign "w" and "s" just as well. However, these literals are not something that should be in the Paddle class. They should come from your world constructor when you create the Paddle objects.
1
2
// only as example -- in world constructor
addObject(new Paddle("w", "s"), 10, getHeight()/2);
This could be used to pass the literal keys to the new Paddle object. The Paddle class constructor will then need to have these values saved in instance fields (fields that this new Paddle object will use). To give objects created from a class an instance field, you simply declare the field (not as 'static' -- ignore if you do not understand) within the class, but outside any methods.
1
2
3
4
5
6
7
8
9
public class Paddle extends Actor
{
    private String upKey, downKey; // instance fields
 
    public Paddle(String up, String down) // arguments added to bring in the key designations
    { // saving the values in the instance fields
        upKey = up;
        downKey = down;
        // etc (create and set image -- what you already had in your constructor)
Now, each paddle will know what keys to use for movement
1
2
if (Greenfoot.isKeyDown(upKey)) setLocation(getX(), getY()-5);
// etc.
patrick26 patrick26

2014/3/27

#
This is what I have so far and it isn't working for me. public class PongWorld extends World { String upKey = "up"; String downKey = "down"; /** * Constructor for objects of class PongWorld. * */ public PongWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(400, 700, 1); addObject(new Ball(5),getWidth()/2,getHeight()/2); addObject(new Paddle("up","down"),20,getHeight()/2); addObject(new Paddle(),380,getHeight()/2); } public class Paddle extends Actor { private String upKey, downKey; public Paddle() { (String up, String down) upKey = up; downKey = down; GreenfootImage pad = new GreenfootImage(10, 100); pad.fill(); setImage(pad); } /** * Act - do whatever the Paddle wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(Greenfoot.isKeyDown("up")) { setLocation(getX(),getY()-5); } if(Greenfoot.isKeyDown("down")) { setLocation(getX(),getY()+5); } if(Greenfoot.isKeyDown("w")) { setLocation(getX(),getY()-5); } if(Greenfoot.isKeyDown("s")) { setLocation(getX(),getY()+5); } } }
danpost danpost

2014/3/27

#
Remove this from your PongWorld class:
1
2
String upKey = "up"
String downKey = "down";
Your two 'addObject' lines for adding the paddles in the PongWorld constructor should look like this:
1
2
addObject(new Paddle("w", "s"), 20, getHeight()/2);
addObject(new Paddle("up", "down"), 380, getHeight()/2);
In your Paddle class:
1
2
3
4
5
6
7
// change this:
public Paddle()
{
    (String up, String down)
// to this:
public Paddle(String up, String down)
{
Also, in the Paddle class, change your act method to this:
1
2
3
4
5
public void act()
{
    if (Greenfoot.isKeyDown(upKey)) setLocation(getX(), getY()-5);
    if (Greenfoot.isKeyDown(downKey)) setLocation(etX(), getY()+5);
}
You need to login to post a reply.