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

2015/11/16

Only move one actor

noskillz noskillz

2015/11/16

#
Hey, i'm trying to code a TicTacToe. With "n", you create a new x, with "m" creating a new o. You can move the x with wasd and the o with the arrow keys. That works fine for the first x and the first o but when i create a second o or x and want to move only that, i move all o's/x's. I hope you can help me soon, thanks
danpost danpost

2015/11/16

#
Create them in your world class and keep a reference to the latest one created there. Use the world class act method to control the movement instead of the class of the actors.
noskillz noskillz

2015/11/17

#
What do you mean with keeping a reference to the lastest one?
noskillz noskillz

2015/11/17

#
1
2
3
4
5
6
7
public void left()
    {
         if (Greenfoot.isKeyDown("left"))
        {
            setLocation( getX()-1,getY());
        }
    }
i cant use this getX in a World class.
danpost danpost

2015/11/17

#
noskillz wrote...
What do you mean with keeping a reference to the lastest one?
I mean that you can add an instance field in your world subclass to hold any actors spawned (and control when and which type). Then you could get its location and move one with code like that which follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
// instance field in world subclass
private Actor latestAddition;
//
// setting latestAddition
latestAddition = new X();
addObject(latestAddition, 0, 0);
//
// code to get location if latestAddition is not null
int x = latestAddition.getX();
int y = latestAddition.getY();
//
// code to move latestAddition if not null (right, in this case)
latestAddition.setLocation(x+1, y);
noskillz noskillz

2015/11/17

#
.
noskillz noskillz

2015/11/18

#
Can i contact you via email? Or pn?
danpost danpost

2015/11/18

#
noskillz wrote...
Can i contact you via email? Or pn?
Why? What would this be in reference to?
noskillz noskillz

2015/11/18

#
its to difficult to write it here
danpost danpost

2015/11/18

#
noskillz wrote...
its to difficult to write it here
It could not be any more difficult to write it here.
noskillz noskillz

2015/11/18

#
ok. is there any way to send you my project? you could write it in the project then, because i dont understand what you mean... :/
danpost danpost

2015/11/18

#
It would probably be better for you if you looked over the lesson in the java tutorials on Classes and Objects.
You need to login to post a reply.