Sprite does not rotate in onManagedUpdate function - android

I have a Sprite in Andengine. When I use first code below, it's working and displaying on scene and rotating. But when I use sprite's onManageUpdate method for detect collision or for anything else, sprite isn't rotating...
circleBox = new CircleBox(x, y, resourcesManager.circleBoxRegion, 2, vbom);
There is rotate function CircleBox class and is rotating in code above.When I use code below is not rotating why?
circleBox = new CircleBox(x, y, resourcesManager.circleBoxRegion, 2, vbom){
#Override
protected void onManagedUpdate(float pSecondsElapsed)
{
if(player.collidesWith(this)){
player.setCurrentTileIndex(8); // olunce duran adam pozistonuna gelsin
player.getBody().setTransform(new Vector2(100/PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT,
400/PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT), 0); //1=32
}
}
};

I think you should call - super.onManagedUpdate(pSecondsElapsed) in onManagedUpdate() method.

Related

How to drag an indivudual texture in libgdx android

I have two textures drawn in my 2d scene. And I want to drag them each separately. When they are touched and dragged I want both textures to be dragged to each points. How it drag them individually, or is there any other method to implement. as I am a beginner to libgdx.
My code:
public class MyGdxGame implements ApplicationListener{
OrthographicCamera camera;
ShapeRenderer shapeRenderer;
float screenOffset=10,circleRadius=30;
Texture firstTexture;
Texture secondTexture;
float firstTextureX;
float firstTextureY;
float secondTextureX;
float secondTextureY;
float touchX;
float touchY;
SpriteBatch batch;
#Override
public void create()
{
firstTexture= new Texture("b1.jpg");
firstTextureX = 50;
firstTextureY = 50;
secondTexture = new Texture("b2.jpg");
secondTextureX = 250;
secondTextureY = 250;
batch = new SpriteBatch();
camera=new OrthographicCamera();
shapeRenderer=new ShapeRenderer();
shapeRenderer.setAutoShapeType(true);
}
#Override
public void render()
{
Gdx.gl.glClearColor(1,1,1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin();
shapeRenderer.setColor(Color.RED);
shapeRenderer.circle(camera.viewportWidth/2,camera.viewportHeight/2,circleRadius);
shapeRenderer.rect(screenOffset,screenOffset,camera.viewportWidth-2*screenOffset,camera.viewportHeight-2*screenOffset);
shapeRenderer.line(screenOffset,screenOffset,camera.viewportWidth-screenOffset,camera.viewportHeight-screenOffset);
shapeRenderer.line(screenOffset,camera.viewportHeight-screenOffset,camera.viewportWidth-screenOffset,screenOffset);
shapeRenderer.line(screenOffset,camera.viewportHeight/2,camera.viewportWidth-screenOffset,camera.viewportHeight/2);
shapeRenderer.line(camera.viewportWidth/2,screenOffset,camera.viewportWidth/2,camera.viewportHeight-screenOffset);
shapeRenderer.end();
batch.begin();
batch.draw(firstTexture, firstTextureX, firstTextureY);
batch.draw(secondTexture, secondTextureX, secondTextureY);
batch.end();
}
Well, you should save the current position of the texture, and on the update, move the texture checking the movement of the mouse if the mouse right click is active. This is the general idea, valid for any framework or code.
I recommend you to move the texture to their own class, with a getter and a setter of the position and the texture, so it's easy to manage.
Using libdgx you can check if the mouse is clicked at any time with
Gdx.input.justTouched();
On every update you can check what was the last position of the mouse and calculate the difference with the new position on every update using
Gdx.input.getX()
Gdx.input.getY()
To syncronize the position inside your screen with the position on your camera you should use unproject on your camera, for example:
Vector3 mousePos = new Vector3();
mousePos.x = Gdx.input.getX();
mousePos.y = Gdx.input.getY();
mousePos.z = 0;
camera.unproject(mousePos); //this will convert the screen position to your camera position
TL;DR you need to check what was the last position of the mouse when it was clicked, and on the next update calculate the difference, and then you update the position of the texture.
BTW, although this is for when you get more experience, you can create a class that implements InputProcessor, alow the class to be processed by libgdx with, for example:
public class CameraControllerDesktop implements InputProcessor, ControllerListener {
public CameraControllerDesktop() {
Gdx.input.setInputProcessor(this);
}
and the you could use the function
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return false;
}
to let libgx calculate the position of what you are dragging.
Sorry about not writing the entire solution, but if whit this info you can solve your problem, I'm pretty sure that you sooner or later will be able to make the game you want.

How to add a button with a Stage class that does not implement the Screen interface?

I have my own Libgdx class that does not implement the Screen
Interface. I control my camera movement. It's an orthographic camera whose viewport width is WorldWidth and viewport height is WorldHeight. I am trying to add a button via the Stage class. I am able to add it and I see that its drawn, but since I don't implement the screen interface I call stage.draw() on my render() method, not on the render(float delta) method that seems to be required by the Screen interface. For comparison purposes, I draw a sprite whose size is (0.25fcamera.viewportWidth,0.25fcamera.viewportHeight). However, this sprite is not drawing when I call stage.draw(). The button draws but it seems very small even though I set its size to (0.25fcamera.viewportWidth,0.25fcamera.viewportHeight). Here's the code on
how I add my button class:
stage1= new Stage();
btn = new Button(new SpriteDrawable(new Sprite(new Texture("texone.png"))),
new SpriteDrawable(new Sprite(new Texture("textwo.png"))));
btn.addListener(new ClickListener(){
public void clicked(InputEvent event, float x, float y){ }
});
btn.setBounds(0f,0f,0.25f*camera.viewportWidth,0.25f*camera.viewportHeight);
btn.setPosition(0f,100f);
stage1.add(btn);
Gdx.input.setInputProcessor(stage1);
//a few lines down to my render() method
#Override
public void render(){
//.... more regular code that goes in method
batch.begin();
//more lines of code that draw my sprites
stage1.draw();
batch.end();
}
What I want to know is why is the button so small and why isn't my other sprite drawing? Perhaps there is a way to add my orthographic camera to my stage1 object. And, if I add my orthographic camera to my stage1 object, how should I update my stage1 viewport on the resize method? Please advise.
thank you
I don't see that you set a projection matrix for batch. Do you call this function?
batch.setProjectionMatrix(camera.combined);
you should do it in render function

Can you draw multiple Bitmaps on a single View Method?

currently I am trying to make an animation where some fish move around. I have successfully add one fish and made it animate using canvas and Bitmap. But currently I am trying to add a background that I made in Photoshop and whenever I add it in as a bitmap and draw it to the canvas no background shows up and the fish starts to lag across the screen. I was wondering if I needed to make a new View class and draw on a different canvas or if I could use the same one? Thank you for the help!
Here is the code in case you guys are interested:
public class Fish extends View {
Bitmap bitmap;
float x, y;
public Fish(Context context) {
super(context);
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.fish1);
x = 0;
y = 0;
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(bitmap, x, y, null);
if (x < canvas.getWidth())
{
x += 7;
}else{
x = 0;
}
invalidate();
}
}
You can draw as many bitmaps as you like. Each will overlay the prior. Thus, draw your background first, then draw your other images. Be sure that in your main images, you use transparent pixels where you want the background to show through.
In your code, don't call Invalidate() - that's what causes Android to call onDraw() and should only be called from somewhere else when some data has changed and needs to be redrawn.
You can do something like this, where theView is the view containing your animation:
In your activity, put this code in onCreate()
myAnimation();
Then
private void myAnimation()
{
int millis = 50; // milliseconds between displaying frames
theView.postDelayed (new Runnable ()
{
#Override public void run()
{
theView.invalidate();
myAnimation(); // you can add a conditional here to stop the animation
}
}, millis);
}

Android moving a shape faster on Canvas

I have a class which is extended from ImageView (lets call it 'surface'). On onDraw method, a little dot is drawing on canvas. When I click a button I try to move this dot to another location. You can consider like manual version of translate animation. It works but now I try to figured out speed of this moving. I mean I want dot moving faster.
Relevant part of surface :
private float actual_x=100,actual_y=100; // Dot is drawn on 100,100 at beginning
private float increase_x,increase_y;
private boolean isMovingResume=false;
private int moving_counter;
public void changeLocation(float x,float y){
isMovingResume=true;
moving_counter=0;
increase_x=(x-actual_x)/50;
increase_y=(y-actual_y)/50;
invalidate(); // This trigger onDraw method
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(actual_x,actual_y,15,fill_paint);
if(isMovingResume){
actual_x=actual_x+increase_x;
actual_y=actual_y+increase_y;
if(moving_counter==49){ // Moving will end after 50 redraw
isMovingResume=false;
}
else{
moving_counter++;
}
invalidate(); //redraw
}
}
And my button click :
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
surface.changeLocation(200,200);
}
});
Like I said, it works but I want it more faster. For example, in this case moving time is 2 second, How can I make it 0,5 second ?
Thanks for all replies.
You need to switch to surfaceview or even faster textureview, both use a canvas as well, and have better performance. Textureview especially as it is hardware accelerated and I also feel it behaves better than surfaceview. That or even consider going to GLSurfaceView.
You can see some example code of surfaceview and textureview: Here

Rotate Sprite with AnalogController

Hello I am developing a game using andengine and now I want my sprite to be rotated with an OnScreenAnalogController. I've initialized it but now I can't figure out how to do the rest of the job. Any examples code or anything would be much appreciated.
Thanks in advance.
P.S Rotation should be around sprite's axis. And when I let go the controller I want the sprite to be facing in the direction where it has rotated not the initial one.
For sprite rotations we override the applyRotation() method ->
Sprite sprite = new Sprite(0, 0, textureRegionForMySprite, getVertexBufferObjectManager()){
#Override
protected void applyRotation(GLState pGLState) {
pGLState.rotateModelViewGLMatrixf(this.mRotation, 0, 1, 0);
}
};
I kinda found a solution using this code
mAnalogController = new AnalogOnScreenControl(90, cameraHeight - 130, mCamera, mControllerTextureRegion, mKnobTextureRegion, 0.01f, getVertexBufferObjectManager(), new IAnalogOnScreenControlListener(){
#Override
public void onControlChange(
BaseOnScreenControl pBaseOnScreenControl, float pValueX,
float pValueY) {
//rect.registerEntityModifier(new RotationByModifier(0.5f, MathUtils.radToDeg((float) Math.atan2(-pValueX, pValueY))));
rect.registerEntityModifier(new RotationModifier(0.1f, rect.getRotation(), MathUtils.radToDeg((float) Math.atan2(pValueX, -pValueY))));
}
#Override
public void onControlClick(
AnalogOnScreenControl pAnalogOnScreenControl) {
// TODO Auto-generated method stub
}
});
the only thing is when I release the controller it get's back to the initial position and rotating which is not what I want. Any ideas on how I can do that?

Categories

Resources