简体   繁体   中英

Lib gdx and custom actors in scene2d

I am new to libgdx and am enjoying the learning that it is providing me with.

I am currently creating some custom actors (that extend the Actor class) and am moving them around the screen.

My question is that when moving the actors by using, setX(), setY(), how do I properly scale this with the passed in float delta which is provided as a parameter to the act method?

Example,

@Override
public void act(float delta) {

    if (wasTouched) {

        setY(getY() - .5f);
    }
}

How can I properly scale the movement with the delta? Is there a built in function for this within the libgdx Actor class?

There is no built-in function. I don't know what your update method looks like so I don't know if wasTouched will be true every frame that the finger is held down on it.

But if you want to move an object with a certain speed while the finger is held down on it, then first you need to have a desired speed. It could be constant like

private static final float SPEED_WHEN_TOUCHED = 5.0f; //in viewport units per second

or it could be some variable that changes over time, but either way, it should be in viewport (world) units per second.

Then when you want to move something by that speed, you multiply the speed by the delta time (which is in seconds) to get the amount of motion you want.

@Override
public void act(float delta) {
    if (wasTouched) {
        y += SPEED_WHEN_TOUCHED * delta;
        positionChanged();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM