How to set PathModifier's coordinates randomly in start of LoopEntityModifier? - android

I created a live wallpaper service using AndEngine library. On screen there is a bird Sprite that flying repeatedly from the left to right. I'm using LoopEntityModifier and PathModifier for the solution. The bird is coded to start randomly on Y-position everytime it shows up from the left screen.
The code is like this:
public class MyLiveWallpaperService extends BaseLiveWallpaperService {
private AnimatedSprite birdSprite;
...
public Scene onLoadScene() {
...
float[] coordY = generateRandomCoordY(); // my custom function to generate random array of Y-coordinates
Path path = new Path(coordX, coordY); // set the coordinate to Path object
// register the modifiers (for the one who is curious, 1st argument of PathModifier is the duration,
// but it has nothing to do with the question)
birdSprite.registerEntityModifier(new LoopEntityModifier(new PathModifier(10, path)));
...
}
}
The problem is the Path's Y-coordinates value cannot be changed anymore when the LoopEntityModifier & PathModifier has run. I want everytime the loop started, I can set the new Path's Y-coordinate value again.

I think you can get around this problem by overriding onModifierFinished() and creating a new PathModifier with the changed path. It would look something like this:
public LoopEntityModifier createModifier(Path path) {
return new LoopEntityModifier(new PathModifier(path)) {
#Override
public void onModifierFinished(final IModifier<IEntity> pEntityModifier, final IEntity pEntity) {
birdSprite.registerEntityModifier(createModifier(path));
}
}
}
birdSprite.registerEntityModifier(createModifier());
This only works if onModifierFinished() is called at the end of every loop.

Related

Wear OS animation Android

How can i animate a line on a canvas in WearOS every 5 seconds?
I know that we have to use an AnimatorTask with a call to postInvalidate(). However since I am directly drawing the line on the canvas, I do not have a View object.
public class AnimatorTask extends TimerTask {
private WatchEventInfo eventInfo;
public AnimatorTask(WatchEventInfo eventInfo) {
this.eventInfo = eventInfo;
}
#Override
public void run() {
drawAndAnimate();
}
public WatchEventInfo getEventInfo() {
return eventInfo;
}
private void drawAndAnimate() {
Canvas canvas = eventInfo.getCanvas();
// For testing
canvas.drawLine(100, 100, 300, 300 markerPaint);
}
}
How do get access to the canvas.drawLine() method object and notify the canvas to redraw itself from the TimerTask assuming that my TimerTask exists in CanvasWatchFaceService subclass?
You get access to the canvas by overriding the onDraw(Canvas canvas, Rect bounds) method declared in the CanvasWatchFaceService.Engine class.
If you create your watch face using the New Project wizard in Android Studio, you'll notice a method at the end of the MyWatchFace class called handleUpdateTimeMessage(). This logic will ensure that your onDraw method gets called at the interval specified by INTERACTIVE_UPDATE_RATE_MS (default is once per second) in interactive mode.
Add a check in your onDraw method to only trigger your line animation if five seconds have past since the last update.

Using a value from json file in Libgdx

I want to use a value from a json file
Here is the Json file
{
"button" : [
{
"x" : 50.0
},
{
"x" : 150.0
}
]
}
I have the following classes
(Button Class)
public class Button extends Sprite{
float x;
public Button() {
super(new Texture("button.png"));
}
#Override
public void setX(float x) {
this.x = x;
}
}
(Data Class)
public class Data {
public Array<Button> buttons;
public void load() {
buttons = new Array<Button>();
Json json = new Json();
json.setTypeName(null);
json.setUsePrototypes(false);
json.setIgnoreUnknownFields(true);
json.setOutputType(JsonWriter.OutputType.json);
json.fromJson(Data.class, Gdx.files.internal("buttons.json"));
}
}
(Main Class)
public class GameMain extends ApplicationAdapter {
SpriteBatch batch;
Data data;
#Override
public void create () {
batch = new SpriteBatch();
data = new Data();
data.load();
for(Button b : data.buttons) {
b.setX(b.x);
}
}
#Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
for(Button b : data.buttons) {
b.draw(batch);
}
batch.end();
}
}
I want to draw buttons in specific x positions that is held in json file
but it gives me nothing .
What is wrong in my code ?
Any ideas ?
at the end of load() you haven't asign the results. Just add buttons = :
public void load() {
//instead of this line:
//buttons = new Array<Button>();
Json json = new Json();
json.setTypeName(null);
json.setUsePrototypes(false);
json.setIgnoreUnknownFields(true);
json.setOutputType(JsonWriter.OutputType.json);
// set buttons here:
buttons = json.fromJson(Data.class, Gdx.files.internal("buttons.json"));
}
Your Data class loads another instance of a Data class and doesn't assign it to anything. It's circular and doesn't make sense. The load method should be static, return a Data object (which is what the last line of your current load method provides), and not be trying to instantiate an empty buttons array that goes unused.
Your button class hides both the x field and the setX method of the superclass, making it impossible to change the actual X position of the sprite that is used when it is drawn. Sprite already has an x parameter, so you should not be adding your own. If you merely remove those two things from your Button class, it should work.
That said, you should not be loading another copy of the same texture for each button. That's a waste of memory and texture swapping. And unless you are very careful about disposing the textures "owned" by these sprites, you are also leaking memory.

aChartEngine; determining when a GraphicalView is fully rendered/drawn

I've got a fragment, which I construct and insert a GraphicalView, and I need to know when the GraphicalView (chart) is done being drawn. the api isChartDrawn always returns false, including within onResume of the containing fragment.
public void onResume()
{
Log.d(TAG, "onResume, the mChart isDrawn: " + mChart.isChartDrawn());
super.onResume();
mListener.didNotificyChartDrawn();
}
Is there a notification I'm not seeing, or strategy for knowing when the chart is done being rendered? I'm asking because I need to access the series points from within one of the series of the XYChart used to construct the graphical view, like this:
mChart = new GraphicalView(getActivity(), mXYChart);
where mXYChart is an instance of the LineChart.
The graph renders fine, and I'm able to access the points I need later on via touch handling, just need to get to them a little earlier now and am hitting this issue. Any work arounds, etc, appreciated.
you're getting that because during onResume still was not draw yet. That's whole Android, not just aChartEngine. I'm not sure it's the best design decision, but that's how it is.
But good news is: there's a nice trick.
getView().getViewTreeObserver().addOnDrawListener(new OnDrawListener(){
void onDraw(){
getView().getViewTreeObserver().removeOnDrawListener(this);
// do your stuff here
}
});
this trick is used A LOT for animation, so you can measure stuff on screen and do the proper animations.
If you look at the onDraw method in GraphicalView, it sets the boolean mDrawn to true at the very end. So what must be happening is you are calling
public boolean isChartDrawn() {
return mDrawn;
}
Before it has completed the onDraw method. I would either create a interval handler to keep checking if mDrawn has been changed to true, or modify the library file GraphicalView so that it has an optional listener that you can attach to be fired off when the thing is drawn:
DrawnInterface mCallback;
public interface DrawnInterface{
public void onDrawn();
}
.....
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.getClipBounds(mRect);
......
canvas.drawBitmap(fitZoomImage, left + width - zoomSize * 0.75f, buttonY, null);
}
mDrawn = true;
mCallback.onDrawn();
}
Then make your calling activity implement the DrawnInterface you defined, and initialize the interface inside the constructor of GraphicalView

passing instructions from UI thread to Rendering thread (GLSurfaceView)

The Setup : A RelativeLayout with a GLSurfaceView and a Button as shown in the image..
The Problem: Lets say I have other triangle models (The one in the picture being the initial model)... I wish to change the models cyclically on click of the button. Since button is on the UI thread and glSurfaceView runs on a separate thread, I don't exactly know how to pass the info/instruction to it. I know there is this thing called Handler in Android which might be useful in this case... But I need some help here..
Edit: If Handler is the right way, I need to know how to add Looper to that Handler... The documentation says add looper.prepare() at the start of run() method.. But the glSurfaceView creates thread implicitly, resulting in no run() method directly available..
I don't think it is necessary to use handlers to solve this issue but you may need to adjust the way you organise your classes.
Here is an example of an organisational structure that might solve your issue:
Activity Class
public class MainActivity extends Activity {
private int modelNumber = 0;
private ArrayList<Model> models = new ArrayList<Model>();
private YourRendererClass renderer;
#Override
public void onCreate(Bundle savedInstanceState) {
...
// Setup GLSurfaceView
GLSurfaceView surface = new GLSurfaceView(this);
setContentView(surface);
renderer = new YourRendererClass();
surface.setRenderer(renderer);
// Set up models
models.add(new Model(x, y, size etc..));
models.add(new Model(x, y, size etc..));
models.add(new Model(x, y, size etc..));
etc.
// Display first model
renderer.setCurrentModel(models.get(modelNumber));
...
}
// Called by the button press:
// Use android:onClick="onClick"
// in your layout xml file within button
public void onClick(View view){
// Make it loop round
modelNumber++;
if(modelNumber>=models.size()){
modelNumber=0;
}
// Display current model
renderer.setCurrentModel(models.get(modelNumber));
}
}
Renderer Class
public class YourRendererClass implements Renderer {
private Model currentModel;
#Override
public void onDrawFrame(GL10 gl) {
// ** Your existing set-up code **//
// Draw model
if (currentModel!=null){
currentModel.draw(gl);
}
}
public void setCurrentModel(Model model){
currentModel = model;
}
}
Model class
public class Model {
// Holds model information
private int size;
private int x;
private int y;
// etc...
public model(int x, int y, int size etc...){
this.x=x;
this.y=y;
this.size=size;
// etc....
}
public void draw(GL10 gl) {
// ** Draw model based on model information fields above **
}
}
The above code is untested as I don't have access to your drawing code but the structure should work if implemented correctly. I've tried to make it clear where you'll have to insert your own code to make it work. In particular I wasn't sure what defines each of your different models so you'll need to include sufficient local variables within the Model class to define them.
I hope my answer helps, let me know if you have any questions.
Tim
You should look at queueEvent! It's a very convenient way to pass informations from UI Thread to renderer Thread:
queueEvent(new Runnable(){
#Override
public void run() {
mRenderer.method();
}});

How to draw a bitmapfont on a stage in libgdx?

This is my current render method on my level in my Libgdx game. I am trying to draw a BitmapFont on my level's top right corner but all I'm getting is a bunch of white boxes.
#Override
public void render(
float delta ) {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
this.getBatch().begin();
//myScore.getCurrent() returns a String with the current Score
font.draw(this.getBatch(), "Score: 0" + myScore.getCurrent(), 600, 500);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
this.getBatch().end();
}
I would like to add the score font into some kind of an actor and then do a scene.addActor(myScore) but I don't know how to do that.
I followed Steigert's tutorial to create the main game class that instantiates the scene, font, in the AbstractLevel class that is then extended by this level.
So far, I'm not using any custom font, just the empty new BitmapFont(); to use to default Arial font. Later I would like to use my own more fancy font.
Try moving the font.draw to after the stage.draw. Adding it to an actor would be very simple, just create a new class and Extend Actor Like such
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
public class Text extends Actor {
BitmapFont font;
Score myScore; //I assumed you have some object
//that you use to access score.
//Remember to pass this in!
public Text(Score myScore){
font = new BitmapFont();
font.setColor(0.5f,0.4f,0,1); //Brown is an underated Colour
}
#Override
public void draw(SpriteBatch batch, float parentAlpha) {
font.draw(batch, "Score: 0" + myScore.getCurrent(), 0, 0);
//Also remember that an actor uses local coordinates for drawing within
//itself!
}
#Override
public Actor hit(float x, float y) {
// TODO Auto-generated method stub
return null;
}
}
Hope this helps!
Edit 1:
Also try System.out.println(myScore.getCurrentScore()); Just to make sure that that isn't the issue. You can just get it to return a float or an int and when you do the "Score:"+ bit it will turn it into a string itself
Well, in this case, you may need to call this.getBatch().end first. Like this:
mSpriteBatch.begin();
mStage.draw();
mSpriteBatch.end();
//Here to draw a BitmapFont
mSpriteBatch.begin();
mBitmapFont.draw(mSpriteBatch,"FPS",10,30);
mSpriteBatch.end();
I don't know why, but it works for me.
I have solved similar problem with white boxes by closing batch before starting drawing stage. That is because the stage.draw() starts another batch and invalidates the previous batch not ended with end().
So in current example I would move this.getBatch().end() before drawing stage:
...
font.draw(this.getBatch(), "Score: 0" + myScore.getCurrent(), 600, 500);
this.getBatch().end();
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
If you are using Scene2D try using stage and actors. There is no need to write long codes, check for the MTX plugin that is very easy to use. You can create an awesome user experience
http://moribitotechx.blogspot.co.uk/

Categories

Resources