I created a simple game where move the objects from one place to another and each moves increment count of moves in score table. After all objects are moved correct I want set opacity to screen and slowly resizing score from initial size(e.g. 15px) to whole screen(e.g. 50px). I load font like this:
FontFactory.setAssetBasePath("font/");
final ITexture mainFontTexture = new BitmapTextureAtlas(activity.getTextureManager(), 256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
font = FontFactory.createStrokeFromAsset(activity.getFontManager(), mainFontTexture, activity.getAssets(), "font.ttf", 15, true, Color.WHITE, 2, Color.BLACK);
font.load();
In createScene() method I create HUD with text and I initial text:
Int mMoves = 0;
HUD gameHUD = new HUD();
Text mText = new Text(25, 25, font, "Moves: " + "123456789", getVertexBufferObjectManager());
And each moves set text with actual moves count:
mText.setText("Moves: " + mMoves++);
And when level was complete I don't know how can I resize this text. I mean something like scale with transition in CSS3..
Thanks for all comments
You can set the size of the text by using the setscale function.
If you just want to set the size use the following :
Example : mText.setScale(1.5f);
You can also use Entity modifiers to create an animation:
ScaleModifier scale = new ScaleModifier(1f, 1f, 1.1f);
LoopEntityModifier loop = new LoopEntityModifier(scale);
mText.registerEntityModifier(loop);
This will give a animation effect re-sizing the text in a loop.
Related
I'm using Andengine to create and android app, and I'm wondering if there is any possible way to change the background colour in a gradient fashion.
I know you can create a gradient in Andengine and thats from one part of the screen to another I what each colour in the gradient to fill the screen as it changes over time.
If you are using the anchor center branch of andengine then you can just import the Gradient Class from org.andengine.entity.primitive.Gradient and set it's height and width to the camera width and height as shown:
final int CameraHeight = 480;
final int CameraWidth = 480;
int directionX = 1;
int directionY = 1;
Gradient g = new Gradient(0,0,CameraWidth,CameraHeight,
this.getVertexBufferObjectManager());
g.setGradient(yourFirstColor,yourSecondColor,directionX,directionY);
And then all you must do is attach it to your scene as the first child scene.attachChild(g);
Then to make it change over time you can register an update handler with the scene that changes the colors or changes the direction like:
scene.registerUpdateHandler(new IUpdateHandler(){
#Override
onUpdate(float pSeconds){
//Change the color or the direction of the gradient
}
});
or you could register an entity modifier like ColorModifier
ColorModifier cm = new ColorModifier(5, Color.BLUE, Color.GREEN);
g.registerEntityModifier(cm);
I don't know if it is just me, but drawables confuse me. Are they intended to keep the size they are given, or is there a way to scale the image within them? I understand they can use ninepatch to fill certain areas by stretching an image, but I want to scale the image that it stretches. I am using a TextButton
for my menu buttons, but they are way too big, and I would love to know how to scale them. I am retrieving the skin from an atlas, which has ninepatch images in it.
Here is the settings screen:
Here are the images in the pack I am using:
Here is the initialization of the TextureAtlas, and Skin:
buttonAtlas = new TextureAtlas(Gdx.files.internal("buttons/buttons.pack"));
buttonSkin = new Skin(buttonAtlas);
Here is the initialization of that menu button.
TextButtonStyle textButtonStyle = new TextButtonStyle();
textButtonStyle.up = Assets.buttonSkin.getDrawable("bluebutton");
textButtonStyle.down = Assets.buttonSkin.getDrawable("redbutton");
textButtonStyle.font = font;
buttonMenu = new TextButton("Menu", textButtonStyle);
buttonMenu.pad(20.0f);
buttonMenu.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
game.fade.startFadeOut();
super.clicked(event, x, y);
}
});
And perhaps I could change how I put it in the table, or how I put the table in the stage?
table.add(buttonMenu).width(Gdx.graphics.getWidth()/4);
stage.addActor(table);
As a last resort I suppose I could attempt creating my own text button actor that I could scale, thank you for your time.
First of all if you know that your images are too big: The best would be to scale the images themselves to a smaller size and then use the TexturePacker too generate a new TextureAtlas that contain the smaller images.
Anyhow you can scale the image button with the TableLayout if you really want to, see example:
table.add(buttonMenu).width(100).height(100);
The above answer didnt work for me, but i have a solution.
You can make a secondary camera and attach it to your stage.
To scale the table just scale the viewport size of the secondary camera
An example would be:
//Heres where you set the scale of your buttons and your table
Camera secondaryCamera = new Camera(Gdx.graphics.getWidth() * scale,
Gdx.graphics.getHeight() * scale);
Stage stage = new Stage();
Table table = new Table();
ImageButton button = new ImageButton(drawableUp, drawableDown, drawableChecked);
table.setFillParent(true);
stage.getViewport().setCamera(secondaryCamera);
table.add(ImageButton);
stage.addActor(table);
This works great when your using it for touch controls. You can use a table for the entire UI and scale it to your liking independent of your other game items;
table.add(buttonMenu).width(100).height(100); should work, and you just need to adjust the width and height parameters to your liking, or you can use the setBounds method. Here is an example:
TextButton myButton = new TextButton("Press Me", style);
myButton.setBounds(xCoordinate, yCoordinate, width, height);
You can set size of Drawable in ButtonStyle to resize your Button:
float scale = 0.5f;
float currentHeight = textButtonStyle.up.getMinHeight();
textButtonStyle.up.setMinHeight(currentHeight * scale);
For more information, see official document for layout:
UI widgets do not set their own size and position. Instead, the parent widget sets the size and position of each child. Widgets provide a minimum, preferred, and maximum size that the parent can use as hints. Some parent widgets, such as Table and Container, can be given constraints on how to size and position the children. To give a widget a specific size in a layout, the widget's minimum, preferred, and maximum size are left alone and size constraints are set in the parent.
And you can find the implementation of Button.getPrefHeight() in the source code:
public float getPrefHeight () {
float height = super.getPrefHeight();
if (style.up != null) height = Math.max(height, style.up.getMinHeight());
if (style.down != null) height = Math.max(height, style.down.getMinHeight());
if (style.checked != null) height = Math.max(height, style.checked.getMinHeight());
return height;
}
I'm a bit stuck here. I have a graph but next to the graph I would love to put a textview.
The textView should get my records straight from the database and add a colour code to it.
(For now a simple for loop will do)
I've got to the point that the pie chart gets drawn and a textview next to the pie chart shows all strings printed (which is now test + the number from the array)
protected void createPiechart(){
Number[] seriesOfNumbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int teller = 0, teller2 = 0;
String text= "";
PieGraph pg = (PieGraph) findViewById(R.id.piegraph);
for (teller = 0; teller < seriesOfNumbers.length;) {
//Random colors in the whole range
Random randomFullRange = new Random();
int color = Color.argb(255, randomFullRange.nextInt(256), randomFullRange.nextInt(256), randomFullRange.nextInt(256));
teller2 = 100 + (15 * teller);
seriesOfNumbers[teller] = teller;
PieSlice slice = new PieSlice();
//Random colors only in green tints.
//Random rnd = new Random();
//int color = Color.argb(255, 0, teller2, 0);
slice.setColor(color);
slice.setValue(teller);
slice.setTitle("test");
pg.addSlice(slice);
text += "test " + teller + "\n";
teller++;
}
TextView textPieGraph = (TextView) findViewById(R.id.textPieGraph);
textPieGraph.setText(text);
}
Now I would like to add the colour that the pieslice has in the textview in some sort of bitmap (lets say 20px*20px)
Which would give
Bitmap Categoryname1
Bitmap2 Categoryname2
... etc etc.
Now how could I add bitmaps to it dynamically with the same colours of my int color value?
I'd love a colour coded list with the text.
Thanks
Yenthe
You could set the background of the textview to the color or add a bitmap behind the text.
textPieGraph.setBackgroundColor(Color.blahblah);
If you wanted it beside it (which might be easier for the user) in a smaller form you could add a linearlayout horizontal around the textview and put a small view beside it and color the background in it. This way there could be the color square beside the text.
i have been having trouble setting padding or something similar to an actor. Cant figure out the way. I guess that I must add something in the skin maybe?
I have this TextField:
textboxskin = new Skin();
textboxskin.add("textfieldback", new Texture("data/textfieldback.png"));
textboxskin.add("cursor", new Texture("data/cursortextfield.png"));
textboxskin.add("selection", new Texture("data/selection.png"));
textboxskin.add("font", font);
TextFieldStyle textfieldstyle = new TextFieldStyle();
textfieldstyle.background= textboxskin.getDrawable("textfieldback");
textfieldstyle.disabledFontColor=Color.BLACK;
textfieldstyle.font=textboxskin.getFont("font");
textfieldstyle.fontColor=Color.WHITE;
textfieldstyle.cursor=textboxskin.getDrawable("cursor");
textfieldstyle.selection=textboxskin.getDrawable("selection");
textfieldusername = new TextField("username", textfieldstyle);
which looks like this:
As you can see it looks horrible left centered...
EDIT:
I tried to do the following and it worked in my tests:
textfieldstyle.background.setLeftWidth(textfieldstyle.background.getLeftWidth() + 10);
Searching the libgdx API I couldn't find a way to specify padding for the text inside the TextField component.
As a workaround, yout could copy the original TextField source and create a new TextField, with another name providing a way to implement the padding.
Copy the contents of the libgdx TextField (https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/TextField.java?source=cc)
to another file and call it MyTextField (as an example). Replace the broken TextField references with the new class name.
Create a new variable called paddingLeft, for example:
public float paddingLeft = 0.0f;
In the draw() method, you can add your padding to the sum to define a new position for the String. Where the code says:
font.draw(batch, displayText, x + bgLeftWidth + textOffset, y + textY + yOffset, visibleTextStart, visibleTextEnd);
replace with:
font.draw(batch, displayText, x + bgLeftWidth + textOffset + leftPadding, y + textY + yOffset, visibleTextStart, visibleTextEnd);
(notice the " + leftPadding" in the second code)
Then, if you are using skins, remember to reference your TextField in the uiskin.json file:
mypackage.MyTextField$TextFieldStyle: {
default: { selection: selection, background: textfield, font: default-font, fontColor: white, cursor: cursor }
}
and use it in your code:
MyTextField txtTest = new MyTextField("Test", skin);
txtTest.leftPadding = 10.0f;
This is not the ideal way, but will work.
Use the Table class to lay out scene2d UIs. To set up the Table:
stage = new Stage();
Table table = new Table();
table.setFillParent(true);
stage.addActor(table);
table.add(textFieldUsername).padBottom(20f); //also use padTop, padLeft, and padRight
table.row();
In the main loop, call:
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
For more information on tables, see: http://code.google.com/p/table-layout/
You could use a NinePatch. This separates your texture into nine "patches" you define their height and width, and then you give it to a NinePatchDrawable which you can give to a TextButton. I believe the outer "patches" act as margins and with some tweaking (not strenuous) it would look great and accomplish your goals.
I learned about them from this post:
Loading nine-patch image as a Libgdx Scene2d Button background looks awful
Hi i want to reduce the length of the edittext view to 3/4 of the screen by animation. Help me on this and below is my code to animate. But it does not happen to move. I want to move it from right to left. Help me please thanks in advance
below is my code
private void animate() {
initialLength = etSearchText.getWidth();
currentLength = initialLength - ibtnSearch.getWidth();
ScaleAnimation anim = new ScaleAnimation(initialLength, currentLength,0, 0);
anim.setInterpolator(new LinearInterpolator());
anim.setDuration(3000);
etSearchText.startAnimation(anim);
initialLength = currentLength;
}
If the above code is called in the onCreate method, you'll find that ibtnSearch.getWidth() is equal to 0. Elements only gain a size after the onResume method I believe. Try temporally hardcoding the size of the search button to see if the animation works or not
Also you'll need to change the scale constructor used to change the point of scaling
try new ScaleAnimation(1, 0.75, 1, 1, 0, 0); Note that the scaling is done by factor terms, not pixel difference.