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

2014/2/25

Greenfoot, simple moving actor

blorange2 blorange2

2014/2/25

#
So i have this project in which an actor is moving using move() and all i need them to do for the timebeing is stop at a given co-ordinate. Any ideas?
danpost danpost

2014/2/25

#
Sounds like you just need to put a condition on the 'move()' statement:
if (/* some condition is true */) move();
the condition could be as simple as 'getX()< targetX'.
blorange2 blorange2

2014/2/25

#
So i could literally just use *if it is where i want, do this, else do whatever* I'll give it a go. I also tried a counter that increments and when it reaches a certain number, an action is performed.
danpost danpost

2014/2/25

#
blorange2 wrote...
I also tried a counter that increments and when it reaches a certain number, an action is performed.
I do not think that a counter would be a good way to go, as if you change the starting location, speed, or some other pre-determined state, it will probably have to be adjusted. I am not saying you should, but best might be to add a 'target' actor (whether invisible or it being actually something where your actor needs to stop at) that intersection detection can be used on as the condition. Obviously, if there is already an Actor object located at the coordinates of where you want your moving actor to stop, this would probably be the way to go.
blorange2 blorange2

2014/2/25

#
I tried this.... but it never reaches the third part?
    public void go(){
        go++;
        if(go < 50){
            move(4);
        } else if(go > 60){
            move(2);
        }else if(go > 70){
            move(0);
        }
    }
danpost danpost

2014/2/25

#
Lines 5 and 7 are using '>', where they should be using '<'. Although, you can remove the second 'else' clause:
public void go(){
    go++;
    if(go < 50)}
        move(4);
    } else if (go < 60){
        move(2);
    }
}
blorange2 blorange2

2014/2/25

#
Also if i have a moving actor, is there a way to stop an actor with a method or can i just use move(0) as in theory this is stationary.
danpost danpost

2014/2/25

#
There is no sense in using 'move(0)' as it is basically just wastefully calling a method. On the other hand, if you were using a field, call it 'speed' to regulate how far it moves each act cycle, then 'move(speed)' would accommodate any value of speed (including zero); but to hard-code a zero as an argument in the 'move' method is pointless as you do not have to call the method if your actor is not to move. As an example:
public void go(){
    go++;
    int speed = 4;
    if (go > 50) speed = 2;
    if (go >= 60) speed = 1;
    if (go >= 70) speed = 0;
    move(speed);
}
would be feasible, but still, the following would be equivalent:
public void go(){
    go++;
    int speed = 4;
    if (go >= 50) speed = 2;
    if (go >= 60) speed = 1;
    if (go >= 70) return;
    move(speed);
}
Although the check for >= 70 could be the first statement in the block to avoid doing anything else for no cause. In fact, it could be moved to your act method using 'if (go < 70) go();'.
blorange2 blorange2

2014/2/27

#
Also is there a way to check for collisions and set an object to stop a certain distance before hitting another?
You need to login to post a reply.