Sequence Action only first action is working in LIBGDX - android

I am trying to add a sequence action. The problem is that only the first added action taken effect. For example in below code if i add mta first, I can only see mta action (the second action doesn't work). If I reverse then I see only the mtabite take effect.
Please help
MoveToAction mta = new MoveToAction();
mta.setPosition(x, y);
mta.setDuration(4f);
MoveToAction mtaBite = new MoveToAction();
mtaBite.setPosition(xFinal, yFinal);
mtaBite.setDuration(4f);
SequenceAction sequence = new SequenceAction();
sequence.addAction(mta);
sequence.addAction(mtaBite);
this.addAction(sequence);

Try something like this instead see if it works better:
actor.addAction(Actions.sequence(mta, mtaBite));
Also make sure your Actions are setup correctly.
mta.reset();
mta.setTime(0);
mta.setPosition(x, y);
mta.setInterpolation(Interpolation.fade);
mta.setDuration(4f);

It is best to use Tween Engine in order to animate in he libgdx.
Tween.to(myobject, Type.POSITION, 1.0f)
.targetRelative(10, -20) // the target can be relative to the current values
.delay(2.5f) // a delay can be specified
.ease(Quad.OUT) // the easing function can be modified
.repeat(2, 0.5f) // repetitions are possible
.repeatYoyo(2, 0.5f) // yoyo repetitions too (one play forward, the other backward, etc)
.setUserData(obj) // custom objects can be attached
.setCallback(cb) // callbacks can be specified to get notified of the completion
.setCallbackTriggers(...) // callbacks can be launched on many events, not just completion
.start(myManager);
http://www.aurelienribon.com/blog/projects/universal-tween-engine/

Related

Libgdx Stage.setActionsRequestRendering(true) not working as expected

I'm trying to disable continues frame rendering in LibGDX.
As stated in libgdx documentation here, it can be done by calling setContinuousRendering(false) and which worked well.
Other thing i want to do is when action is applied on some actor , every frame must be rendered until action has not finished.
for that i called the method from stage setActionsRequestRendering(true) which changes nothing. frames are dropped and not rendered.
Applied actions on the actors are working but it is applied directly without any effects.
What I'm missing here?
Update :
I have an Actor which is image and applying action to the actor like this.
img.addAction(Actions.moveTo(x, y, 0.5f, Interpolation.swingIn));
This actions skips first few frames. also fps logger displays random number of frames between 1-20 when action is active. There is no other issue which may affect frame rates.
Also this same action works as expected if this action is applied after dragging actor. but does not work if applied after clicking.
Action applied after drag.
img.addAction(Actions.sequence(
Actions.moveTo(x, y, 0.5f , Interpolation.exp5Out),
Actions.run(new Runnable() {
#Override
public void run() {
dragged = true;
}
})));
At first here is the wiki entry for continous rendering.
To anwser your question: You are missing to call
Gdx.graphics.requestRendering();
on your own. Many default actions as mentioned in the wiki entry are calling this method, so they request a render call. For your own custom actions you have to do this manualy.

Libgdx for loop lag

I have problems with my game. The game runs ok until the time where I have to reposition the objects, which happens every frame. For example here I have to reposition some parked cars (around 6 cars on each side of the road), the cars are constantly moving down in the screen, then they reset and reposition at the top again. I have used Poolable to save resources. However, the game is still very jumpy when it comes to running these lines of code. Is there any way I could improve the code to prevent lag? Maybe a new thread? If so how would be the right way of creating a new thread and updating it every frame.
private void updateParkedVehicles(float delta){
for (int i = 0; i < parkedCarLeft1Array.size; i++){
parkedCarLeft1Array.get(i).update(delta);
for (int c = 0; c < parkedCarLeft1Array.size; c++){
if (c != i){
if (Intersector.overlaps(parkedCarLeft1Array.get(i).bounds, parkedCarLeft1Array.get(c).bounds)){
parkedCarLeft1Array.get(i).reset();
}
}
}
}
for (int i = 0; i < parkedCarRight1Array.size; i++){
parkedCarRight1Array.get(i).update(delta);
for (int c = 0; c < parkedCarRight1Array.size; c++){
if (c != i){
if (Intersector.overlaps(parkedCarRight1Array.get(i).bounds, parkedCarRight1Array.get(c).bounds)){parkedCarRight1Array.get(i).reset();
}
}
}
}
}
One way to handle items in your game is through the use of the Scene2d.
Scene2D allows you to step out from having to move yourself the items. Instead you give them instructions (as a director would) if and when you need to move them instead of having to handle yourselve every frame update.
in a nutshell, to add an Action you would do this:
if(SomeCondition){
MoveToAction moveCar = new MoveToAction();
moveCar.setPosition(anX,anY);
moveCar.setDuration(aDuration);
myCarActor.addAction(moveCar);
}
That way, the only thing you need to check for is the occurence of the event and you call your function to move the car only once (when the event occurs).
LibGdx will take care of updating your actor each frame.
The only thing you will need to do in your loop is to check the collision. If they occur then you will assign a new Action to your actor.
Another thing to keep in Mind, if you use Scene2D is that you can use the Camera. This means that if you need to scroll, instead of moving each item, you simply move the camera (which has all the convenience method you need to zoom etc).
For more information on Scene2D check out: https://github.com/libgdx/libgdx/wiki/scene2d
For more information specifically on Actions check this out:
https://github.com/libgdx/libgdx/wiki/Scene2d#actions
Hope it helps.
It seems to me that the problem is with double looping through arrays --> O(n^2) complexity!!
It's not clear for me why you need to check overlapping of each car with each car except this car if "the cars are constantly moving down in the screen, then they reset and reposition at the top again". For the described behaviour the simplest way would be to check is the current y position is less that 0 and rest position if so:
for (Car car : parkedCarLeft1Array){
car.update(delta);
if (car.getY <= 0) {
car.reset();
}
}
I assume that you update car position in update() method.

How to make actors do the specific action one after another in Libgdx

I've got three questions.>.<
I have 2 actors(actor1,actor2), and an action(eg. ScaleTo). My willing is to make actor1 do the scaleTo firstly,and then(maybe after two seconds) actor2. Or randomly choose the actor to behave the action, and repeat the process n times.
Are there any methods like squenceAction, but could be like this "sequence(Actor1.action,delay,Actor2.action,......)"
Or is there be the timeline API which i can put the action of actor on several specific time points?
not if be the more correct way, but it occurs to me that if I understand your question, you can put in your listener for example this:
Actor1.addAction(Actions.sequence(Actions.delay(0.1f),
Actions.parallel(
Actions.moveBy(0f, 600, 1f),
Actions.fadeOut(0.8f))));
and in your render this one:
if (Actor1.getActions().size == 0) {
Actor2.addAction(Actions.sequence(Actions.delay(0.2f),
Actions.parallel(
Actions.moveBy(0f, 600, 1f),
Actions.fadeOut(0.8f))));
//Actor1.addAction(Actions......add actions to the actor one again or
// whatever you can think is that it's not what you really want to do,
// but you can handle yourself with the method called from the if
}
depends what you want to do, I think it would be better that worked it how long the first actor to finish the action, before the 2 second for example, put it in the second actor two second delay, for start amimacion in second actor.
test: 0.2f + 1.8, not + fadeOut becouse is parallel
Actor1.addAction(Actions.sequence(Actions.delay(0.2f),
Actions.parallel(
Actions.moveBy(0f, 600, 1.8f),
Actions.fadeOut(0.8f))));
add delay; 2.1f
Actor2.addAction(Actions.sequence(Actions.delay(2.1f),
Actions.parallel(
Actions.moveBy(0f, 600, 1f),
Actions.fadeOut(0.8f))));
P.S: I hope you can understand what I say.

Unity2D Android Touch misbehaving

I am attempting to translate an object depending on the touch position of the user.
The problem with it is, when I test it out, the object disappears as soon as I drag my finger on my phone screen. I am not entirely sure what's going on with it?
If somebody can guide me that would be great :)
Thanks
This is the Code:
#pragma strict
function Update () {
for (var touch : Touch in Input.touches)
{
if (touch.phase == TouchPhase.Moved) {
transform.Translate(0, touch.position.y, 0);
}
}
}
The problem is that you're moving the object by touch.position.y. This isn't a point inworld, it's a point on the touch screen. What you'll want to do is probably Camera.main.ScreenToWorldPoint(touch.position).y which will give you the position inworld for wherever you've touched.
Of course, Translate takes a vector indicating distance, not final destination, so simply sticking the above in it still won't work as you're intending.
Instead maybe try this:
Vector3 EndPos = Camera.main.ScreenToWorldPoint(touch.position);
float speed = 1f;
transform.position = Vector3.Lerp(transform.position, EndPos, speed * Time.deltaTime);
which should move the object towards your finger while at the same time keeping its movements smooth looking.
You'll want to ask this question at Unity's dedicated Questions/Answers site: http://answers.unity3d.com/index.html
There are very few people that come to stackoverflow for Unity specific question, unless they relate to Android/iOS specific features.
As for the cause of your problem, touch.position.y is define in screen space (pixels) where as transform.Translate is expecting world units (meters). You can convert between the two using the Camera.ScreenToWorldPoint() method, then creating a vector out of the camera position and screen world point. With this vector you can then either intersect some geometry in the scene or simply use it as a point in front of the camera.
http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenToWorldPoint.html

Cocos2d-x: Create simple progress bar

I'm newbie in Cocos2d-x.
I want to create simple progress/update bar for my game.
When this progress bar is full, we will move to next level.
How can i create that bar.
Thanks for all your helps.
See this - How to use a progress bar in cocos2d-x and C++
Basically, create two sprites one for the progress bar's border and one for the loading bar itself.
CCPointer fuelBarBorder;
fuelBarBorder =
CCSprite::createWithSpriteFrameName ("bt_progressbarborder.png" );
fuelBarBorder->setPosition(ccp(100,100));
this->addChild(fuelBarBorder,1);
// CCProgresstimer object (smart pointer)
CCPointer fuelBar;
fuelBar = CCProgressTimer::create(
CCSprite::createWithSpriteFrameName ("bt_progressbar.png" ));
Set the loading bar sprite's type to CCProgressTimerType.
// Set this progress bar object as kCCProgressTimerTypeBar (%)
fuelBar->setType(CCProgressTimerType::kCCProgressTimerTypeBar);
// Set anchor point in 0,0 and add it as a child to our border sprite
fuelBar->setAnchorPoint(ccp(0,0));
fuelBar->setBarChangeRate(ccp(1,0)); // To make width 100% always
fuelBar->setTag(1); // Tag our object for easy access
fuelBarBorder->addChild(fuelBar,50); // Add it inside the border sprite
In your update method, change its percentage to reflect loading percentage.
fuelBar->setPercentage(80); // Value between 0-100
I wrote the original post (link above).
Thanks to this post, I found out that WordPress played me ... when saving the code:(.
There is some corrections to be made:
CCPointer <CCSprite> fuelBarBorder;
fuelBarBorder =
CCSprite::createWithSpriteFrameName ("bt_progressbarborder.png" );
fuelBarBorder->setPosition(ccp(100,100));
this->addChild(fuelBarBorder,1);
This for the first set, as you can see the only change is at the first line:
CCPointer <CCSprite> fuelBarBorder;
If you don't have this cocos2d-x extension, just use the following:
CCSprite * fuelBarBorder;
Same on the second set of code, the correct one is:
CCPointer <CCProgressTimer> fuelBar;
fuelBar = CCProgressTimer::create(
CCSprite::createWithSpriteFrameName ("bt_progressbar.png" ));
// Set this progress bar object as kCCProgressTimerTypeBar (%)
fuelBar->setType(CCProgressTimerType::kCCProgressTimerTypeBar);
// Set anchor point in 0,0 and add it as a child to our border sprite
fuelBar->setAnchorPoint(ccp(0,0));
fuelBar->setBarChangeRate(ccp(1,0)); // To make width 100% always
fuelBar->setTag(1); // Tag our object for easy access
fuelBarBorder->addChild(fuelBar,50); // Add it inside the border sprite
Same thing is using an CCPointer (smart pointer implementation), if you don't have it in your project, just change the following line:
CCPointer <CCProgressTimer> fuelBar;
by this one:
CCProgressTimer fuelBar;
An this should make the code works, I hope this helps !!!!

Categories

Resources