android Libgdx particle editor special line - libgdx

As we all know that the particle editor give us a explosive particle line . However , what I need is a particle move from around point to the point I clicked . How can I realize this?

Just take a "Point particle" and change the position over time to the clicked point while the ParticleEffectis running. Thats all you need todo. A simple approach would be to create a class which holds the component ParticleEffect, an target position and extends Actor. Inside of the act(float delta) you update the position of the Actor into the direction of the target by an speedvalue multiplied with the delta time. Don't forget to update the postion of the ParticleEffect. Something like this:
#Override
public void act2(float delta) {
direction = this.pos - this.target; //both are vector2
direction.nor();
this.setPosition(direction.x*delta*velocity, direction.y*delta*velocity);
}
#Override
public void draw2(Batch batch, float alpha) {
emitter.setPosition(getX(), getY());
emitter.draw(batch);
}

Related

Confused about Inputlistener LIBGDX

I have
Main Test Class creating a Stage, Adding an Actor to the stage and setting the Inputprocessor to the stage
extended Group Class with several Actors added. In the Constructor of the Group I have added an InputListener.
The InputListener is not fired . Can someone tell why not and how to do ?
public class Test extends ApplicationAdapter implements ApplicationListener {
public void create() {
stage = new Stage(new ScreenViewport());
specialScene = new SpecialScene();
stage.addActor(specialScene);
Gdx.input.setInputProcessor(stage);
}
}
public class SpecialScene extends com.badlogic.gdx.scenes.scene2d.Group {
public SpecialScene {
<add some actors ...>
addListener(specialListener);
}
private static InputListener specialListener = new InputListener() {
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
return true; //or false
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
super.touchUp(event, x, y, pointer, button);
}
#Override
public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
super.enter(event, x, y, pointer, fromActor);
}
};
}
* UPDATE *
I found the problem. The Listener did not find any region of my Actors.
I have to set explicitely the Region with setBounds().
My Problem is solved, but I am still confused. Why do I have to set the bounds myself. I will forget this with every Actor in the future I am sure, because it is unlogical to me. Is this the way I have to, or do I understand the concept wrong ?
Group is a skeleton class that can be used to develop your own functionality, so it does not presume anything, even the way its child actors contribute to its bounds. (For example, you might have some actors that you don't want to contribute because they are a visual flourish, like particles.) You can extend Group to create your own base class to fit your needs.
So why doesn't LibGDX already include a class like that? In LibGDX, Stage is used primarily for the UI system. Although it was designed to be extensible to all kinds of purposes, it only includes a framework for you to do that, unless you are using the fully baked UI implementation that is based on it. That UI implementation does include a subclass of Group called WidgetGroup, which does what you'd expect with the bounds.
IIRC, the author of Stage wrote a blog post a few years ago on libgdx.com discussing how he tried using Stage for the gameplay of a simple game jam game, and basically concluded that it caused his game to be more convoluted, or at least more time-consuming to code.
I have personally used it for a turn-based game jam game, and it was good for that. I used the Actions system to have nice animated transitions of the game pieces. But I think it would make a real-time game more convoluted than creating your own organization structure that is tailored to your particular game. If you are creating a more complicated game, you might check out the Ashley plugin for LibGDX.
In either case, you definitely should use it for GUI stuff because that is all fully implemented and a huge time-saver.

LibGDX Particles not the same as in Editor

I've created a "snow" particle effect using LibGDX's ParticleEditor, and exported the effect to a snow.p file.
When I load the file in my game it looks completely different.
As seen in the editor:
As seen in game:
As you can see the editor shows the snow falling throughout the space. Can't tell from the image, but it's falling downwards.
In the game, the snow "falls" along a line and to the right. I opened the p file in a text editor, and it looks fine so I'm not sure what is wrong.
In case it's needed, here is how I load and use the particle effect.
ParticleEffect p = new ParticleEffect();
p.load(Gdx.files.internal("snow.p"), Gdx.files.internal(""));
p.setPosition(0, DonutsGame.WORLD_HEIGHT / 2);
addActor(new ParticleEffectActor(p));
The actor class:
public class ParticleEffectActor extends Actor {
ParticleEffect effect;
public ParticleEffectActor(ParticleEffect effect) {
this.effect = effect;
effect.start();
}
public void draw(SpriteBatch batch, float parentAlpha) {
effect.draw(batch); //define behavior when stage calls Actor.draw()
}
public void act(float delta) {
super.act(delta);
effect.update(delta); //update it
}
}
I can post the snow.p file, but it's a bit large, if there's a particular section that would be helpful let me know and I will post it.

Post overriding the paint method of the components in java

In java awt or swing when you want to change painting of some component you usually have to override the method paint(Graphics g) (in awt) or paintComponent(Graphics g) (in swing).
This is usually (maybe allways - I'm not sure) done when you are creating the component for example:
JPanel jPanel = new JPanel() {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
//... my implementation of paint, some transfromations, rotation, etc
}
};
Imagine that you have container of components which could for example consists of some JLabels, some JTextFields, some image. Which will be all put on one component.
By container I mean you have some list or map with ids or some similar structure in which are all components you will put on one JFrame.
The question is if I can change the painting method after creating with all of the components which are in this list in the moment when all of them are already created. For example I want do the rotation action (rotate), which is defined in Graphisc2D, with all of them.
So basicaly what I want is that I throught the list of componets I have and say:
"All of you (components) which are in the list will be rotated by some angle". Is that possible? If yes how?
Edit:
This is my not correctly working solution:
graphicalDisplayPanel = new JPanel() {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g2d = (Graphics2D) g;
g2d.rotate(Math.PI, anchorx, anchory);
}
#Override
public void paintChildren(Graphics g) {
super.paintChildren(g);
Graphics2D g2d2 = (Graphics2D) g;
g2d2.rotate(Math.PI, anchorx, anchory);
}
};
JFrame jFrame = JFrame();
// ... setting dimension, position, visible etc for JFrame, it works correctly nonrotated
jFrame.setContentPane(graphicalDisplayPanel);
I have not tested this, but it seems like it would work. A JComponent's paint() method calls:
paintComponent(co);
paintBorder(co);
paintChildren(co);
where co is a Graphics object. In theory you create an image, retrieve the graphics object and then pass that into paintChildren(). you will have to call paintComponent() and paintBorder() yourself, if you do this. Then, just rotate the image and draw it into your component. You may have to crop the image or resize your component accordingly for this to work. It might look something like this:
BufferedImage myImage;
#Override
public void paint(Graphics g){
myImage = new BufferedImage(getWidth(), getHeight(), BufferedImage.TRANSLUCENT);
//using a transparent BufferedImage might not be efficient in your case
Graphics myGraphics = myImage.getGraphics();
super.paintComponent(g);
super.paintBorder(g);
super.paintChildren(myGraphics);
//rotation code here
// ...
//draw children onto your component
g.drawImage(myImage, 0, 0,getWidth(), getHeight(), null);
}
I hope I didn't make any mistakes, please let me know if this works.
So basicaly what I want is that I throught the list of componets I have and say: "All of you (components) which are in the list will be rotated by some angle".
If you want to rotate panel and therefore all the components on the panel as a single using then you need to do the custom painting in the paintComponent() method.
If you want to rotate, for example, individual images that each have a different angle of rotation then you can again do this in the paintComponent(...) method and change the angle for each component.
Or, in this second case you can use the Rotated Icon class. In this case the Icon is just added to a JLabel. Then you can change the degrees of rotation and repaint the label, so there is no custom painting (except in the Icon itself).

LibGDX Scene2D: Actions won't work at all for actors inside groups

I am trying to fade an Actor out using the Action FadeOut. However, I've discovered that the no actions work at all for my objects. The hierarchy of my classes are like this:
Actor -> MoveableObject -> Knight
Stage -> KnightGroup (Group) -> Knight
Actions for my Knight actors do not work at all. However, actions for my KnightGroup group works. Here is the code for my Knight:
public class Knight extends Players {
public Knight() {
setWidth(96);
setHeight(96);
setPosition(100, 90);
//Doesn't work
AlphaAction action = new AlphaAction();
action.setAlpha(0f);
action.setDuration(1f);
addAction(action);
//Doesn't work
addAction(fadeOut(1f));
addAction(Actions.scaleBy(1f, 1f));
}
#Override
public void act(float delta){
super.act(delta);
}
#Override
public void draw(Batch batch, float parentAlpha) {
batch.setColor(getColor().r, getColor().g, getColor().b, getColor().a);
batch.draw(animation[currentState], getX(), getY(), getWidth(), getHeight());
}
}
I can't for the life of me figure out what the problem is. Actions in MoveableObject (the parent of Knight) doesn't work either. My best guess is that wrapping actors in a Group will render the actions of those actors invalid. The KnightGroup is a pretty crucial part of my code though and I would have to do a lot of refactoring to take it out. Can someone else shed some light on this issue?
In the Actor class the method act(float delta) calls act(delta) for all registered Actions of this Actor. So you have to make sure, that you call super.act(delta) in every subclass of Actor, so that the act(delta) method in Actor gets called.

How to finish animation with libgdx

I am trying to implement a simple animation with libGDX and I am currently stuck on one thing. Lets say I have a bunch of sprites which take some time to finish. For example, around 30 sprites like this link: https://github.com/libgdx/libgdx/wiki/2D-Animation
However, before the animation completes some key is pressed. For smooth animation I want the 30 frames to be completed before I start the next set of animation, to prevent an abrupt stop.
So my question is how do I achieve it this in libGDX? My current idea is to extend the Animation class, which would keep track of how frames I have and how many have been rendered and then display the rest. Or use the isAnimationFinished(float stateTime) function (though I haven't had luck using that).
The examples I have seen like superjumper have very few animations and don't really change that much.
Also, is there a way to hold the list of sprites from a TextureAtlas.createSprites method and use those with the Animation class? If not, whats the purpose of providing this function?
Thanks
You can use
animation.isAnimationFinished(stateTime);
To see if your animation is finished.
For the sprites : personnaly I use TextureRegion from a TextureAtlas and I store them in an array for my animation
I create a class AnimatedImage that extends Image to automate spriting in Image. My code will be like this:
public class AnimatedImage extends Image{
private Array<Array<Sprite>> spriteCollection;
private TextureRegionDrawable drawableSprite;
private Animation _animation;
private boolean isLooping;
private float stateTime;
private float currentTime;
public AnimatedImage(Array<Array<Sprite>> _sprites, float animTime, boolean _looping){
// set the first sprite as the initial drawable
super(_sprites.first().first());
spriteCollection = _sprites;
// set first collection of sprite to be the animation
stateTime = animTime;
currentTime = 0;
_animation = new Animation(stateTime, spriteCollection.first());
// set if the anmation needs looping
isLooping = _looping;
drawableSprite = new TextureRegionDrawable(_animation.getKeyFrame(currentTime));
this.setDrawable(drawableSprite);
}
public void update(float delta){
currentTime += delta;
TextureRegion currentSprite = _animation.getKeyFrame(currentTime, isLooping);
drawableSprite.setRegion(currentSprite);
}
public void changeToSequence(int seq){
// reset current animation time
resetTime();
_animation = new Animation(stateTime, spriteCollection.get(seq));
}
public void changeToSequence(float newseqTime, int seq){
_animation = new Animation(newseqTime, spriteCollection.get(seq));
}
public void setRepeated(boolean _repeat){
isLooping = _repeat;
}
public boolean isAnimationFinished(){
return _animation.isAnimationFinished(currentTime);
}
public void resetTime(){
currentTime = 0;
}
}
changetosequence method will make new Animation that will be used to update the current TextureRegionDrawable at the update method. resetTime will reset the total time for the animation when you call changeToSequence. You could add the event listener to call changeToSequence method.
Here is the example:
private AnimatedImage _img;
then I add the InputListener like this:
_img.addListener(new InputListener(){
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button){
_img.changeToSequence(1);
return true;
}
});
Hope it helps.
Use tween engine for this kind of animation. Its well documented and libgdx supports it .. Google about it , and you can find bunch of examples using libgdx .. Hope it will help you !!