AndEngine setPosition inside onManagedUpdate - android

I am reading learning andengine book https://play.google.com/store/books/details/Martin_Varga_Learning_AndEngine?id=ww9_BAAAQBAJ.
Try to manually execute setPosition on Player inside onManagedUpdate scene class
Here is my code. For example
player.getY()=100
platform.getFirst().getY()=200
player.getHeight=50
#Override
protected void onManagedUpdate(float pSecondsElapsed) {
// in this position player.getY always 100 evertime onManagedUpdate is called even I already set up player.setPosition like below
if (player.getY() < platforms.getFirst().getY()) {
float x = platforms.getFirst().getX();
float y= platforms.getFirst().getY() + player.getHeight() / 2;
player.setPosition(x, y);
}
}
Unfortunately the player.getY() is back to 100 everytime onManagedUpdate is called even I already set player.setPosition(x,y) inside the managedUpdate
What's wrong ?

In your above assigments, you are using player.getY() = 100; if you want to asign a value to Y coordinate use player.setY(100f); to set a property use set() methods and to get some property value use get() methods.
Change you assigments to this:
player.setY(100f);
platform.getFirst().setY(200f);
player.setHeight(50f);
Use float values.

Related

How to move a view to another view using animation in Android?

I have a circle at the center of the screen inside which there's an ImageView + TextView. I have another two ImageView+TextView, one at the top and another at bottom of the screen.
My requirement is :
I want a copy of the top ImageView+TextView and a copy of the bottom ImageView+TextView to move in animation into the center of the circle, thereby changing the value of the textView inside the circle.
For example:
Say top textView has value 200 and bottom textview has value 300. I want a portion of those values (say 100 or 150) to animate and move into the circle, but the original values 200 and 300 should remain on the same position.
I've tried using TranslateAnimation. However I face issues finding the x and y coordinates of the center circle. It is not exactly going to the center of the circle. Also original view's position is not retained.
TranslateAnimation animation = new
TranslateAnimation(startLayout.getX(),endLayout.getX(),
startLayout.getY(),endLayout.getY);
animation.setDuration(1000);
animation.setFillAfter(false);
startView.startAnimation(animation);
startLayout is the linearlayout in which ImageView and TextView reside.
Please help! Thanks!
I had the same issue and I fixed by using the next code (sorry is in Kotlin, but works the same in Java).Let's say viewFirst wants to reach viewTwo position:
(DON'T USE):
viewFirst.animate()
.translationX(viewSecond.x)
.translationY(viewSecond.y)
.setDuration(1000)
.withEndAction {
//to make sure that it arrives,
//but not needed actually these two lines
viewFirst.x = viewSecond.x
viewFirst.y = viewSecond.y
}
.start()
(USE THIS SOLUTION):
viewFirst.animate()
.x(viewSecond.x)
.y(viewSecond.y)
.setDuration(1000)
.withEndAction {
//to make sure that it arrives,
//but not needed actually these two lines
viewFirst.x = viewSecond.x
viewFirst.y = viewSecond.y
}
.start()
Using the getX() and getY() methods define the position of the view in pixels, but the constructor you use defines Float type values that must be values from 0.0f to 1.0f
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
This is another option using the view`s position in pixels:
viewFirst.animate()
.x(viewSecond.getX())
.y(viewSecond.getY())
.setDuration(1000).withEndAction(new Runnable() {
#Override
public void run() {
viewFirst.setX(tv2.getX());
viewFirst.setY(tv2.getY());
}
}).start();
Try this for accurate coordinates
private fun moveView(viewToBeMoved: View, targetView: View) {
val targetX: Float =
targetView.x + targetView.width / 2 - viewToBeMoved.width / 2
val targetY: Float =
targetView.y + targetView.height / 2 - viewToBeMoved.height / 2
viewToBeMoved.animate()
.x(targetX)
.y(targetY)
.setDuration(2000)
.withEndAction {
targetView.visibility = View.GONE
}
.start()
}

Change image height with delay animation

I'm using LibGDX engine in my game and I started to use Tween for animation:
I have in my game a small 'custom' progress bar. One image is an empty progress bar and the second is full one, they booths have the same width and height.What I do is I update it by setting the position of the full as the same as the empty, and when I want to display progress I use:
private Image fullProgress; //its a libGDX image type
//I have already set the process
float originalHeight = progressEmpty.getHeight();
howMuchProgress = originalHeight * process;
fullProgress.setHeight(howMuchProgress);
Any ideas how can I make it change the height with delay animation, so I can see it for a few seconds? better will be to use the Tween Engine?
Given your code example, delay should already be calculated in the process.
If you need to visualize the delay just for the debugging purposes (for example, for faking the AssetManager's loading progress), I suggest you do something like this:
private static final float LOADING_MIN_TIME= 2.0f; // delay amount in seconds
private float loadingTimer;
#Override
public void render(float delta) {
// ...
loadingTimer += delta;
float loadingTimerScaled = loadingTimer / LOADING_MIN_TIME;
if (loadingTimerScaled >= 1.0f) {
loadingTimerScaled = 1.0f;
}
process = Math.min(process, loadingTimerScaled); // use the slow(fake) value.
}

android: move to touch location after release with libgdx/universal-tween-engine

I'm learning to use libgdx with universal-tween-engine and haven't been able to figure out how to touch (or click on the desktop app) a point on the screen and have a texture move all the way to the touched location without keeping the touch or click active until the end-point is reached.
When the touch event is initiated, the animation begins and the graphic moves towards the location. The graphic will follow the finger/mouse-pointer if a touch and drag is initiated. If I touch a point, the graphic will move towards the point until the touch is released. Then it stops where it was when touch is released.
I'm looking to touch-and-release and have that graphic move to the touched point, and am probably not understanding something about the tween engine implementation. I've pasted the tweening code below.
public void render() {
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(texture.getTexture(), texture.getBoundingBox().x, texture.getBoundingBox().y);
batch.end();
Tween.registerAccessor(Plane.class, new TextureAccessor());
TweenManager planeManager = new TweenManager();
float newX = 0;
float newY = 0;
boolean animateOn = false;
if(Gdx.input.isTouched()) {
newX = Gdx.input.getX();
newY = Gdx.input.getY();
animateOn = true;
}
if (animateOn == true && (texture.getX() != newX || texture.getY() != newY)) {
Tween.to(texture, TextureAccessor.POSITION_XY, 10)
.target(newX, newY)
.ease(TweenEquations.easeNone)
.start(planeManager);
planeManager.update(1);
if (texture.getX() == newX && texture.getY() == newY) {
animateOn = false;
}
}
}
Originally, I had the tweening code inside the conditional for isTouched() and didn't use the newX, newY or animateOn variables. I thought using isTouched() to only set the new coordinates and animation state would then make the loop trigger the tween. The older code looked like this:
if(Gdx.input.isTouched()) {
newX = Gdx.input.getX();
newY = Gdx.input.getY();
Tween.to(texture, TextureAccessor.POSITION_XY, 10)
.target(newX, newY)
.ease(TweenEquations.easeNone)
.start(planeManager);
planeManager.update(1);
}
I've also tried using justTouched(), but the graphic would only move very slightly toward the touched point.
I've been struggling with this for a few hours, I'd really appreciate it if anyone could point me in the right direction.
Thanks.
Tween.registerAccessor(Plane.class, new TextureAccessor());
TweenManager planeManager = new TweenManager();
These two lines should go in the create() method, not the render() one! Here, you're instantiating a new manager on every frame, you only need one manager, that's all, not an army of them!
Also, you need to update the manager on every frame, not just when animateOn is true, else you'll need to keep your finger pressed...
The correct code is as follows, learn from it, you'll get a better understanding of how the Tween Engine works :)
// Only one manager is needed, like a Spritebatch
private TweenManager planeManager;
public void create() {
Tween.registerAccessor(Plane.class, new TextureAccessor());
planeManager = new TweenManager();
}
public void render() {
// The manager needs to be updated on every frame.
planeManager.update(Gdx.graphics.getDeltaTime());
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(texture.getTexture(), texture.getBoundingBox().x, texture.getBoundingBox().y);
batch.end();
// When the user touches the screen, we start an animation.
// The animation is managed by the TweenManager, so there is
// no need to use an "animateOn" boolean.
if (Gdx.input.justTouched()) {
// Bonus: if there is already an animation running,
// we kill it to prevent conflicts with the new animation.
planeManager.killTarget(texture);
// Fire the animation! :D
Tween.to(texture, TextureAccessor.POSITION_XY, 10)
.target(Gdx.input.getX(), Gdx.input.getY())
.ease(TweenEquations.easeNone)
.start(planeManager);
}
}
I was trying to implement this behavior in the wrong way. Instead of using isTouched or justTouched(), I needed to use touchDown() from GestureListener.
I created a class that implemented GestureDetector (call it touchListener())inside of my main class (the one that implements ApplicationLisetener )in the main libgdx project and put the x and y capturing code inside of toucDown (I noticed tap() was also being triggered). I moved the tween functions (the actual tweening, the call to registerAccessor(), and the creation of the new tween manager) into the update() method of touchListener().
I added a call to touchListener()'s update function inside the render() loop of the main libgdx class.
I doubt I did this is the best way, but I hope it's helpful to someone else in the future.

Previous coordinates of a touched object

I'm programming with Processing, what i need is the equivalent of pmouseX/Y but for touch, but I can't use pmouse because I use multi-touch and I need previous coordinates of each touched point. I don't know if I've made myself clear, to do an example I need to know initial and final coordinates of a swipe
I currently use to get initial coordinates:
public boolean surfaceTouchEvent(MotionEvent me) {
float x0=me.getX(0);
float y0=me.getY(0);
....
....
return super.surfaceTouchEvent(me);
}
I'm not sure if I get your right here, since this seems to be very basic programming, but I'll try.
Just use an ArrayList and add each position there. For different touches, you might want to use a HashMap, like this:
HashMap<MotionEvent, ArrayList<Point2D.Float>> touches = new HashMap<MotionEvent, ArrayList<Point2D.Float>>();
public boolean surfaceTouchEvent(MotionEvent me)
{
float x0=me.getX(0);
float y0=me.getY(0);
if (!touches.containsKey(me))
{
touches.add(me, new ArrayList<Point2D.Float>());
}
else
{
// get previous position
Point2D.Float prevpos = touches.get(me).get(touches.get(me).size() - 1);
}
touches.get(me).add(new Point2D.Float(x0, y0));
....
}
Didn't test this, but that's basically how it would work.

Drawing circles at random points

I have got an application where I need to draw random number of points at random locations in the activity. Then I need to move those points at any direction too like steroids. How can I do that? Please see image below.
Well, if I understood you correctly, you wish to make some "asteroids" for your application.
This is not specific to Android, but you probably need to define an asteroid as an entity in your application and when you need the asteroids you just create a random number of them with random positions (you may wish to check if there is already an asteroid or other object in that position to avoid collisions).
Other than that, you just need to give each asteroid a velocity (in a 2D plane, an X and Y velocity) and update that accordingly in a loop as your application progresses.
This is a simple example, but here goes:
//To make things easier, let's assume you have an Entity class, from which every game object is inherited
public abstract class Entity {
// Fields used to know object position
private float x;
private float y;
// Fields used to calculate object motion
private float x_speed;
private float y_speed;
...
// You would probably have a generic method to draw every entity - details are not relevant to your question, but you should draw the object taking it's x and y coordinates into account here
public void draw() { ... }
// Generic function to update the object's position regarding its speed
public void updatePosition() {
this.x += this.x_speed;
this.y += this.y_speed;
}
...
}
//Let's say you have an Asteroid class, which represents each asteroid
public class Asteroid extends Entity {
// Just add a constructor to set it's initial position and speed
public Asteroid(float initial_x, float initial_y, float ini_x_speed, float ini_y_speed) {
this.x = initial_x;
this.y = initial_y;
this.x_speed = ini_x_speed;
this.y_speed = ini_y_speed;
}
}
From here on, you would just have to create a random number of Asteroid objects, with random positions and on your application's main loop call the updatePosition and draw methods for each entity.
EDIT: Oh, and don't forget to "clear" what you've drawn at each loop cycle, so you won't see the already drawn objects in their old positions. :)
Hav look at http://www.droidnova.com/playing-with-graphics-in-android-part-iii,176.html
In onDraw method create a Random object seeding it with width and height of screen and draw points on those points as many time as you want
And onTouchevent() check the method in link change the position of these points

Categories

Resources