I'm using LibGDX and I want to know if it is possible to do a speech system where the text gets drawn letter by letter, or slowly, like a person speaking, instead of just appearing. Is this possible? Do I need to make a function to do it or does LibGDX or java have it built in??
Thanks,
Luke
I would recommend something similar to Sameera's comment, although a wait generally isn't a good idea for a game as it stops everything else, unless you do it in a separate thread.
Instead of waiting, perhaps use your delta times:
private float timeSinceLastLetter = 0f;
private static final float TIME_PER_LETTER = 100f;
public void render(float deltaTime) {
// do your other rendering
if(timeSinceLastLetter > TIME_PER_LETTER) {
timeSinceLastLetter = 0f;
// render your next letter here
} else {
timeSinceLastLetter += deltaTime;
}
}
There's plenty more details that need filling in, but that should give a rough idea
Related
I am beginner when it comes to java and I have come across with a problem that I haven't found a solution to just yet. The thing is that I have working methods for drawing an letter and also for rotating it - when I set rotation it works the way it should. However I would like to make this slightly more interactive, at school we were given the basic framework for that - I was able to create a button that when you click on it the letter's angle changes and the letter is redrawed correctly. But I would like to make it an animation, for example you click on the button and for 10 seconds (or until you press the button again) the letter will be rotating.
On the internet I found a way to perform an action after certain period of time, and I thought I will use this. I wanted to add an angle and redraw an image, after let's say 1 second, then it would repeat - I thought this would make it look like it is animated. But I was wrong. I tried so many ways to do this, the best thing was that after few seconds that I set I want the animation to go for, it changed an angle and redraw, unfortunately it was the final state and it didn't draw states in between to create an animation. And this latest code doesn't even do that, the program just freezes.
int animation = 0;
int steps = 0;
public void G_draw() {
graphic.clear();
if (animace==1)
{
animation();
}
letter('a', G_Color.G_cBlack, 2, 2);//drawing an letter
}
public void G_mousePressed(G_Button button, int x, int y) {
if (button.equals(G_Button.B_LEFT)&&x>700&&x<750&&y>500&&y<520){
animation=1;
G_draw();
}
}
public void animation() {
long start = System.currentTimeMillis();
long end = start + 2 * 1000;
while (System.currentTimeMillis() < end) {
}
langle+=30; // adding an angle
steps++;
G_repaint();
G_draw();
if (steps<4) animace();
}
instead of this
long start = System.currentTimeMillis();
long end = start + 2 * 1000;
while (System.currentTimeMillis() < end) {
}
use
Thread.sleep(# in milliseconds);
We can't see where you use langle
This line if (steps<4) animace(); only runs when animace<>1 otherwise it's skipped because animate()calls G_draw() calls animate and so on.
I am currently working on a 2d game in which a player sprite pushes other sprites around on the screen.
My current code (within subclass):
//x and y being the co-ords i want this object to move to (e.g 50 pixels
right of its starting point etc.)
public Boolean move(float x, float y, int delta) {
this.setx(x);
}
How do i make the object move say 50 pixels every 1 second? or alternatively every x frames.
I've tried using delta but that results in smooth motion which is much harder to control for my particular needs.
Any help would be much appreciated
Your approach to accomplish it with the deltas is right. Assuming you have your move method inside your update method and call it in there (or implementing it in a similar way). One way you could achieve these would be the following:
class YourGameStateWithUpdateRenderInit extends BasicGameOrWhatever{
//Global variables for updating movement eacht second.
float myDelta = 0; // your current counter
float deltaMax = 1000; // 1 second, determines how often your object should move
public void update(...){
objectToMove.move(50,50,delta); //The object which contains the move method and you move it by 50 x/y per second.
}
}
Inside your objectToMove class you have your move method:
public Boolean move(float x, float y, float pDelta) {
myDelta += pDelta;
if(myDelta >= deltaMax){
this.setx(x);
myDelta = 0;
}
}
This should work for an update every second. However this implementation is not really good or precise since as you stated you probably have that move method in a sub class or something similar. So you need to adapt it to your needs, but i hope you get the idea behind it. I think it demonstrates the purpose of counting an class attribute up by the delta values until a certain value (e.g. 1000 for 1 second) and after that set it back to zero.
I have a game that is almost done but I forgot about gravity and impulse, i did not want to implement box2d and i got the gravity to work, but can some body explain how to apply an impulse without box2d?
You can simulate an impulse just by adding a velocity to the objects velocity. Like that:
class GameObject {
private Vector2 velocity = new Vector2();
public void applyImpulse(Vector2 impulse) {
velocity.add(impulse);
}
public void update(float deltaTime) {
float dampingPerSecond = 0.5f;
float dampingFactor = Math.max(0f, 1 - deltaTime*dampingPerSecond);
velocity.scl(dampingFactor);
}
}
Now you might use it like this: object.applyImpulse(new Vector2(3, 2));. In case you update all your GameObjects in every frame, your object will slow down after an impulse was given. Which is what you would expect to happen, if no further impulses hit your object. Doing this in a realistic way though is difficult and might only be achieved by using box2d. You might however tweak that and hope that it will work in your game. Remember to always apply your gravity after that though.
I'm creating java game (I'm a beginner with this for now) and I'd like to start with some kind of platform game.
I'd like to know how I can make the player jump (I know how to move him up and down), but I don't know how how to make him go back down after going up.
Here is my code:
public void keyPress() {
if (listener.arrowUp) {
Jump();
}
}
private void Jump() {
if(player.get(1).getPosY() > maxJump) {
player.get(1).moveY(-10);
} else if(player.get(1).getPosY() == maxJump) {
player.get(1).moveY(85);
}
}
So.. the player moves -10px upwards as long as i press 'w' and when he hits maxJump (which is 375 and players position at the start is 465) he "teleports" back to 465 instead of sliding back down like he does when going up.. It's really hard to explain this without a video, but i hope somebody understands and can help me with this.
This question gives a basic answer to yours. Now in your jump function, you have to set the vertical_speed only once and only call fall() every frame and add some moveY.
Here are two alternatives:
Option 1. Simulating some very simple physics. You add forces to your player, so pressing the up arrow will add a force that makes the player move upwards. Gravity is constantly applied and thus it will gradually cancel out the force you applied when the up arrow was pressed. Perhaps you only want to use forces in the vertical direction you do something like this:
// Each frame
if(notOnTheGround){
verticalVelocity -= GRAVITATIONAL_CONSTANT;
}
// When user jumps
vertivalVelocity += JUMP_FORCE;
Option 2. An alternative is to kind of animate the jumps using projectile motion.
// Each frame
if(isJumping){
verticalVelocity = initialVelocity*sin(angle) - g*animationTime;
animationTime += TIME_STEP;
}
// When user jumps
isJumping = true;
animationTime = 0;
initialVelocity = ... // calculate initial velocity
I am trying to make a onTouchEvent to create a missile that will launch from my character sprite and forward.
I have this working using
if (missdraw = true){
canvas.drawBitmap(missile,missilex,missileY,null);
missilex = missilex + 14;
missdraw = false;
}
in my onDraw method, but the problem is it will only create one at a time.
I tried to create a class to deal with this, but this just causes an error and crashes when i try to fire.
here is what i use for the class: (this is in the ondraw in my gameview)
for (Batcher missile : missiles ){
missile.onDraw(canvas);
}
this is in the class
public Batcher(List<Batcher> temps, ScreenActivity newView, float x,
float y, Bitmap missile){
this.x = 1;
this.y = 2;
this.missile = missile;
}
public void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
canvas.drawBitmap(missile, x,y, null);
}
I would appreciate any help, but also if you could explain how it would work, instead of just code, as im quite new to programming, and really need to understand what im doing and why im doing it, rather than just copying peoples code because it works.
Cheers Phil.
Your concepts are pretty good, actually. There are a few things I don't quite understand about the code samples you posted up, I'll try to translate into what I'd do and you can tell me if I'm doing it wrong :)
in your game class you need (and it looks like you have) a list of Missiles:
LinkedList<Batcher> missiles;
In your onTouch(), however a missile is created -
missiles.add(new Batcher(missilex, missiley, missile));
you now have a collection of missiles. Note that I didn't include the list in the constructor of your batcher, because an object should never need to know that it's a part of a collection. All it needs to know is how to draw itself and where. Since I assume that all of your missiles will be added to or removed from the screen frequently, while only having a few on screen at a time, I've used a LinkedList, which is fast for adding and removing, but slow for accessing a specific missile. If you needed to access specific items in the collection and the collection didn't change very much, you would use an ArrayList instead. on to onDraw - as you have it the missile draws itself, which is fine, but I prefer to let the View do the drawing, with the missile telling it where it should be drawn -
for (Batcher missile : missiles ){
missile.setX(missile.getX() + 14); // to make it move
if (missile.getX() > canvas.gedWidth()) { //check if it's left the screen
missiles.remove(missile); // Remove it
}
else { //perform drawing
canvas.drawBitmap(missile.getBitmap(), missile.getX(), missile.getY(), null);
}
}
Hopefully that'll do it for you, but feel free to let me know if there's anything you'd like me to explain more!