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;
}
Related
I want to change the bitmap that contains an image whenever user tap on the screen. i.e. the default image is shadow1, now what i want is that when user touched on screen then this image changed to shadow2, then again if user touched then shadow3, then on next touch the image again comes as shadow1 and it goes on and on and on. so basically there are three images and i want that when ever user touched on screen then image changes with each tap.
Following is the code that i tried but it is still not working as expected i.e. the image changes from shadow1 to shadow2 but then not changes to shadow3 or shadow1 even if i touched many times.
public void Touched(float x, float y)
{
boom = false;
try{
switch (bird.GetState()) {
case 0:
distance = 0;
bird.SetState(1);
flapped = true;
Bitmap workingBitmap = BitmapFactory.decodeResource(gameLogic.Resources(), R.drawable.shadow1);
bitmapBird = workingBitmap.copy(Bitmap.Config.ARGB_8888, false);
if (bitmapBird==workingBitmap)
{
}
riseCounter = 0;
pipeValues.clear();
//SoundManager.playSound(2, 1);
break;
case 1:
{
riseCounter = 0;
flapped = true;
t = 3;
Bitmap workingBitmappp = BitmapFactory.decodeResource(gameLogic.Resources(), R.drawable.shadow2);
bitmapBird = workingBitmappp.copy(Bitmap.Config.ARGB_8888, false);
//SoundManager.playSound(2, 1);
}
break;
case 2:
{
riseCounter = 0;
flapped = true;
t = 0;
}
break;
default:
Bitmap workingBitma = BitmapFactory.decodeResource(gameLogic.Resources(), R.drawable.shadow3);
bitmapBird = workingBitma.copy(Bitmap.Config.ARGB_8888, false);
break;
}
} catch(Exception e){}
}
I think there should be a for loop or while loop in 'case 1' and whenever user tap then image changes. Please help me with this.
You can simply use an int value to keep track of the image displayed as;
First Initialize a int at class level;
int num = 0;
then you can use it as;
if(num == 0){
loadFirstImage();
num++;
}
else if(num == 1)
{
loadSecondImage();
num++;
}
else if(num == 2){
loadThirdImage();
num = 0 ;
}
I think that you need to change the state of the bird in your second case statement.
First iteration will set the state to 1, from there, the only case statement you can hit is case 1: because you never change it.
So you need something like
case 1:
bird.SetState(2);
//....
Hope that helps
I have a world full of 2D squares (z=0). At startup I setup projection in such a way that the whole world is visible on the screen, using:
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(left, right, bottom, top, zNear, zFar);
Then I allow the user to zoom in the world with fingers by using:
gl.glScalef(mScaleFactor, mScaleFactor, 1.0f);
I want to make visibility test for objects that appear not visible as the user is zooming in to not render them (performance boost).
I found this method:
android.opengl.Visibility.visibilityTest(float[] ws, int wsOffset, float[] positions, int positionsOffset, char[] indices, int indicesOffset, int indexCount);
But I can't make it work, nor I found ANY examples of the usage of this method on Internet. Currently this method returns result=0 for every square I test, even when scaling is not applied (mScaleFactor = 1.0)
The way I'm doing this:
final short SQUARE_VERTICES_ORDER_TEMPLATE[] = {0, 1, 2, 0, 2, 3};
.....
float[] vertices = toArray(mVertexBuffer);
short[] indices = toArray(mIndicesBuffer);
char[] charIndices = new char[indices.length];
// method needs char[]
for (int i = 0; i < indices.length; i++) {
short shortIndex = indices[i];
charIndices[i] = (char) shortIndex;
}
for (int i = 0; i < mSquares.size(); i++) {
int numIndicesPerSquare = SQUARE_VERTICES_ORDER_TEMPLATE.length;
int indicesOffset = i * numIndicesPerSquare;
int result = Visibility.visibilityTest(matrixGrabber.mProjection, 0, vertices, 0, charIndices, indicesOffset, numIndicesPerSquare);
switch (result) {
case 0:
Log.v(TAG, "Object NOT VISIBLE: " + mSquares.get(i)); // hits every time
break;
case 1:
Log.v(TAG, "Object PARTIALLY VISIBLE: " + mSquares.get(i));
break;
default:
TAG.toString(); // to place a break point
break;
}
}
I'm not sure if I'm picking up the right Matrix required by this method.
Could you please validate the right usage of this method or give any other tips or workarounds ?
I've figured out what is wrong, visibilityTest method requires multiplied matrix. Here is how it should be:
matrixGrabber.getCurrentState(gl);
float[] resultMatrix = new float[matrixGrabber.mProjection.length];
Matrix.multiplyMM(resultMatrix, 0, matrixGrabber.mProjection, 0, matrixGrabber.mModelView, 0);
....
Visibility.visibilityTest(resultMatrix, 0, vertices, 0, charIndices, indicesOffset, numIndicesPerSquare);
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;
}
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 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.