I have 3 Translate Animation to be played when the app starts -
Animation animation = new TranslateAnimation(0, 0, -1500, 1500);
animation.setDuration(1000);
animation.setFillAfter(false);
myimage.startAnimation(animation);
animation.setRepeatCount(Animation.INFINITE);
Animation animation2 = new TranslateAnimation(0, 0, -1000, 1000);
animation2.setDuration(1000);
animation2.setFillAfter(false);
myimage2.startAnimation(animation2);
animation2.setRepeatCount(Animation.INFINITE);
Animation animation3 = new TranslateAnimation(0, 0, -500, 500);
animation3.setDuration(1000);
animation3.setFillAfter(false);
myimage3.startAnimation(animation3);
animation3.setRepeatCount(Animation.INFINITE);
I want to start the above 3 in random order, not necessarily the first one first.
One full day gone and I'm still unable to find a solution.
Any pointers on how to achieve it ?
You can generate random number in java and do switch
Random random = new Random();
int num = 3;
switch(random.nextInt(num)) {
case 0:
animateFirst();
break;
case 1:
animateSecond();
break;
case 2:
animateThird();
break;
}
This is one way I got, modifying your code, #Murtaza Hussain
switch(random.nextInt(num)) {
case 0:
animateFirst();
animateSecond();
animateThird();
break;
case 1:
animateSecond();
animateFirst();
animateThird();
break;
case 2:
animateFirst();
animateThird();
animateSecond();
break;
}
Related
Here is my code which works fine
mEffect = effectFactory.createEffect(EffectFactory.EFFECT_BLACKWHITE);
mEffect.setParameter("black", .9f);
mEffect.setParameter("white", .5f);
break;
but now I want two effects on it, what to do ?
mEffect = effectFactory.createEffect(EffectFactory.EFFECT_BLACKWHITE);
mEffect.setParameter("brightness", 2.0f);
I want to marge both effects with "grayscale image"
mEffect = effectFactory.createEffect(EffectFactory.EFFECT_GRAYSCALE);
I also look at this documents but no help [GLSurfaceView][1]
1: https://developer.android.com/reference/android/opengl/GLSurfaceView.html ,
EGLDisplay
1: https://developer.android.com/reference/android/opengl/EGLDisplay.html and Effect factory too
I want to generate/randomize a color and then I want second one that is close to the generated one.
This is how I generate the colors ftm:
Paint colors = new Paint();
int red = ran.nextInt(256-100)+100;
int green = ran.nextInt(256-100)+100;
int blue = ran.nextInt(256-100)+100;
colors.setARGB(255, red, green, blue);
and later the 2nd color I generate like this:
switch (ran.nextInt(3)) {
case 0:
red = red - (40 - level);
break;
case 1:
green = green - (40 - level);
break;
default:
blue = blue - (40-level);
break;
}
The problem is that it works in some cases and sometimes it can give me a 2nd color that is off by miles.
Is there another, better and easier way to generate these colors?
br
You need to create a real random number between 0 and 3:
Random ran = new Random();
int max = 3;
int min = 0;
int randomNum = ran.nextInt((max - min) + 1) + min;
switch (randomNum ) {
case 0:
red = red - (40 - level);
break;
case 1:
green = green - (40 - level);
break;
default:
blue = blue - (40-level);
break;
}
You could use java.awt.Color.brighter() and Color.darker().
I have an explosion animation i need to loop once and then dissapear, but is not working,
This part its supposed to do the trick: birdAnimationHit.setPlayMode(Animation.NORMAL);
But it only shows the last image of the loop. Don't know what is wrong, here is my code
AssetLoader.java
public static void load() {
texture = new Texture(Gdx.files.internal("data/texture.png"));
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
dustHit1 = new TextureRegion(texture, 930, 154, 140, 158);
dustHit1.flip(false, true);
dustHit2 = new TextureRegion(texture, 1079, 154, 187, 158);
dustHit2.flip(false, true);
dustHit3 = new TextureRegion(texture, 1274, 154, 149, 158);
dustHit3.flip(false, true);
dustHit4 = new TextureRegion(texture, 1430, 154, 153, 158);
dustHit4.flip(false, true);
dustHit5 = new TextureRegion(texture, 1590, 154, 155, 158);
dustHit5.flip(false, true);
TextureRegion[] birdsHit = { dustHit1, dustHit2, dustHit3, dustHit4, dustHit5 };
birdAnimationHit = new Animation(0.06f, birdsHit);
birdAnimationHit.setPlayMode(Animation.NORMAL);
}
GameRenderer.java
private void drawBirdHit(float runTime) {
batcher.draw(birdAnimationHit.getKeyFrame(runTime), bird.getX(),
bird.getY(), bird.getWidth() / 2.0f,
bird.getHeight() / 2.0f, bird.getWidth(), bird.getHeight(),
1.4f, 1.4f, bird.getRotation());
}
You need to sum up the runTime inside of that logic. Else you always get the same frame. Take a look at the getKeyFrame method: (Marked the importend part for you)
public TextureRegion getKeyFrame (float stateTime) {
int frameNumber = getKeyFrameIndex(stateTime);
return keyFrames[frameNumber];
}
public int getKeyFrameIndex (float stateTime) {
if (keyFrames.length == 1) return 0;
int frameNumber = (int)(stateTime / frameDuration); //this will always produce the same picture if you dont sum up the time!
switch (playMode) {
case NORMAL:
frameNumber = Math.min(keyFrames.length - 1, frameNumber);
break;
case LOOP:
frameNumber = frameNumber % keyFrames.length;
break;
case LOOP_PINGPONG:
frameNumber = frameNumber % ((keyFrames.length * 2) - 2);
if (frameNumber >= keyFrames.length) frameNumber = keyFrames.length - 2 - (frameNumber - keyFrames.length);
break;
case LOOP_RANDOM:
frameNumber = MathUtils.random(keyFrames.length - 1);
break;
case REVERSED:
frameNumber = Math.max(keyFrames.length - frameNumber - 1, 0);
break;
case LOOP_REVERSED:
frameNumber = frameNumber % keyFrames.length;
frameNumber = keyFrames.length - frameNumber - 1;
break;
}
return frameNumber;
}
So hold somewhere a timer where you sumup the deltatimes
and change your code to something like this:
private void drawBirdHit(float runTime) {
sum += runTime;
batcher.draw(birdAnimationHit.getKeyFrame(sum), bird.getX(),
bird.getY(), bird.getWidth() / 2.0f,
bird.getHeight() / 2.0f, bird.getWidth(), bird.getHeight(),
1.4f, 1.4f, bird.getRotation());
}
The Animation itself does not have a timer for that. You can just request the right keyframe depending on the past time, not on the current deltatime.
What I do is simply include a blank frame as the last frame of any animation; you only need one per sprite sheet that can be used for multiple animations. Then just call the stop/play methods, the animation will run then disappear, ready for the next play on collision or whatever.
I currently have a body created that is Dynamic and moves at a constant speed with a Vector2(). What I want is for when the body leaves the edge of the screen, to come back from that current point back to its original point instantaneously. How do I do this?
a.applyForceToCenter(aMovement, true);
a.applyTorque(3000, true);
FixtureDef fDef = new FixtureDef();
BodyDef ballD = new BodyDef();
ballD.type = BodyType.DynamicBody;
//random location for asteroid
int aLoc = (int) (aLocation * 15);
float x = 300;
switch(aLoc)
{
case 0:
ballD.position.set(x, -105);
break;
case 1:
ballD.position.set(x, -95);
break;
case 2:
ballD.position.set(x, -80);
break;
case 3:
ballD.position.set(x, -65);
break;
case 4:
ballD.position.set(x, -50);
break;
case 5:
ballD.position.set(x, -35);
break;
case 6:
ballD.position.set(x, -20);
break;
case 7:
ballD.position.set(x, -5);
break;
case 8:
ballD.position.set(x, 10);
break;
case 9:
ballD.position.set(x, 25);
break;
case 10:
ballD.position.set(x, 40);
break;
case 11:
ballD.position.set(x, 55);
break;
case 12:
ballD.position.set(x, 70);
break;
case 13:
ballD.position.set(x, 85);
break;
default:
ballD.position.set(x, 0);
}
PolygonShape asteroid = new PolygonShape();
asteroid.setAsBox(12.5f, 12.5f);
//asteroid definition
fDef.shape = asteroid;
fDef.density = .5f;
fDef.friction = .25f;
fDef.restitution = .75f;
a = world.createBody(ballD);
a.createFixture(fDef);
a.setFixedRotation(false);
//asteroid image
aSprite = new Sprite(new Texture("img/asteroid-icon.png"));
aSprite.setSize(12.5f * 4, 12.5f * 4);
aSprite.setOrigin(aSprite.getWidth() / 2, aSprite.getHeight() / 2);
a.setUserData(aSprite);
asteroid.dispose();
You could use Body.setTransform() for that task, but I would not do that. setTransform() causes a lot of trouble in the long run.
For me it lead to weird bugs. For example using setTransform disabled my ContactFilter in random moments, which cost me several days of debugging until I found that.
Furthermore it causes non-physical behaviour, because you basically teleport the Body.
Better would be to destroy the Body completely and recreate a new one at the same initial position of the old one.
You can set the position of your Box2D a body instantly through this method:
a.setTransform(new_x, new_y, new_angle);
With this, you can create a condition that sets the x and y position of the body back to its original position when the x or y value of the body is outside of the screen.
if(outsideBounds()){
a.setTransform(start_x, start_y, start_angle);
}
You can check whether your object is offscreen by either checking its Box2D position and its converted screen coordinates, or by checking the sprite's location.
Once you've received the x and y screen positions, you can compare them to the screen bounds like this:
pos_x>screenWidth||pos_x<0||pos_y>screenHeight||pos_y<0
This can be improved by including the dimensions of the object, depending on when you want the transformation to happen:
(pos_x-objWidth)>screenWidth || (pos_x+objWidth)<0 ||
(pos_y-objHeight)>screenHeight || (pos_y+objHeight)<0
You can use the following:
body.setTransform(newX, newY, newAngle);
body.setAwake(true);
Waking up the body is important.
It might be currently inactive and without waking it up neither gravity nor any immediate collisions will affect it at the new position.
i am new developer on andengine. i am making a game of bike racer, where i want to move my background instead of my bike. so how can i use two 3 background if 1 background is complete then start second one and when it complete then start third? How should i do for it please tell me to solve this problem.
Are you using tiles in your background?
I got this snippet of code from : This very useful tutorial
switch(lDirection) {
case RIGHT:
int RightAnimTiles[] = {WorldActivity.RIGHT_CENTER_TILE - 1,
WorldActivity.RIGHT_CENTER_TILE,
WorldActivity.RIGHT_CENTER_TILE + 1,
WorldActivity.RIGHT_CENTER_TILE};
animate(WorldActivity.WALK_ANIMATE_DURATION, RightAnimTiles, -1);
break;
case DOWN:
int DownAnimTiles[] = {WorldActivity.DOWN_CENTER_TILE - 1,
WorldActivity.DOWN_CENTER_TILE,
WorldActivity.DOWN_CENTER_TILE + 1,
WorldActivity.DOWN_CENTER_TILE};
animate(WorldActivity.WALK_ANIMATE_DURATION, DownAnimTiles, -1);
break;
case LEFT:
int LeftAnimTiles[] = {WorldActivity.LEFT_CENTER_TILE - 1,
WorldActivity.LEFT_CENTER_TILE,
WorldActivity.LEFT_CENTER_TILE + 1,
WorldActivity.LEFT_CENTER_TILE};
animate(WorldActivity.WALK_ANIMATE_DURATION, LeftAnimTiles, -1);
break;
case UP:
int UpAnimTiles[] = {WorldActivity.UP_CENTER_TILE - 1,
WorldActivity.UP_CENTER_TILE,
WorldActivity.UP_CENTER_TILE + 1,
WorldActivity.UP_CENTER_TILE};
animate(WorldActivity.WALK_ANIMATE_DURATION, UpAnimTiles, -1);
break;
default:
break;
}