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

2013/9/21

Help with decimal movement

wabuilderman wabuilderman

2013/9/21

#
i have a terraria game (see scenario or "help with dynamic grid lighting) but i want my player to move un-accordingly to the current grid size.. is it possible to move only a fraction? such as "setLocation(0.05f , 0.7f);" i know that that code there dosen't work.
Gevater_Tod4711 Gevater_Tod4711

2013/9/21

#
Therefore you need to override some methods and fields. You first need location variables that are doubles and not ints and you need to override the setLocation method:
protected double exactX;
protected double exactY;

public void setLocation(double x, double y) {
    this.exactX = x;
    this.exactY = y;
    super.setLocation((int) x, (int) y);
}

public double getExactX() {
    return exactX;
}
public double getExactY() {
    return exactY;
}
Now you can move your actor for parts of a grid size. But your actor's image still can only move full grids. So your location is saved as the exact values but your actor will still only move full grids. To make your actor move only half grids is unfortunately not possible.
wabuilderman wabuilderman

2013/9/21

#
what about something like this:
tempImg = getImage()
getImage().clear()
getImage().drawImage(tempImg, offsetX, offsetY)
that way you can simply offset the image from the nearest cell
Gevater_Tod4711 Gevater_Tod4711

2013/9/21

#
Yes that might work. But you need to change it a bit:
GreenfootImage tempImg = new GreenfootImage(getImage());
getImage().clear();
getImage().scale(tempImg.getWidth() + offsetX, tempImg.getHeight() + offsetY);
getImage().drawImage(tempImg, offsetX, offsetY);
I'm not shure if this will work. You'll have to try.
wabuilderman wabuilderman

2013/9/21

#
the question is.. where do i get the offsetX and offsetY from... it would have to do something like this:
offsetX = ((int) getExactX()) - getX();
wabuilderman wabuilderman

2013/9/21

#
never mind... that would probably wouldn't work
wabuilderman wabuilderman

2013/9/21

#
still... how to find out how many pixels to offset it to... maybe rounding it to the nearest hundredth?
wabuilderman wabuilderman

2013/9/21

#
this is something that the developers should look into integrating into the greenfoot API.
danpost danpost

2013/9/21

#
The smaller you make your world cellsize the smoother your actors will move. When setting the cellsize of your world to 10, your actors can only move by increments of 10 pixels (that is, a change of one in the value of getX() or getY() will offset your actor 10 pixels from its original location. The smoothest movement you can achieve is when the cellsize of your world is one (1). To improve beyond that, you would have to incorporate a method similar to what Gevator_Tod4711 suggested. Offsetting the image from the center of the cell is possible, but would be a lot of extra, and not so easy, coding. Best would be to set the cellsize to 1, and adjust all the location coordinates to compensate. Mostly, just multiplying by 10 and adding 5.
wabuilderman wabuilderman

2013/9/21

#
the reason i suggested offseting the image is because i only really need to do it once. but i guess in the long run, a smaller cell size would be best. thanks danpost
danpost danpost

2013/9/21

#
danpost wrote...
Mostly, just multiplying by 10 and adding 5.
Actually, what this means is 'multiplying by blocksize and adding blocksize/2'.
You need to login to post a reply.