I want to ask about animation in android.
What's different between
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
and
TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)
If I have 2 textview belong a layout, I want to move text1 to text2, I coded:
animation = new TranslateAnimation(txt1.getX(),txt2.getX(),
txt1.getY(),txt2.getY());
But when run code, the text 1 move other position text2.
What should I do?
Thanks for your help :))
Related
I'm trying to flip some animations in LibGDX, but because they are of different width, the animation plays weird. Here's the problem:
(the red dot marks the X/Y coordinate {0,0})
As you can see, when the animation plays "left" when you punch, the feet starts way behind than were it was, but when you punch right, the animations plays fine because the origin of both animations is the left corner, so the transition is smooth.
The only way I think of achieving what I want is to see what animation is playing and adjust the coordinates accordingly.
This is the code:
public static float draw(Batch batch, Animation animation, float animationState,
float delta,
int posX, int posY, boolean flip) {
animationState += delta;
TextureRegion r = animation.getKeyFrame(animationState, true);
float width = r.getRegionWidth() * SCALE;
float height = r.getRegionHeight() * SCALE;
if (flip) {
batch.draw(r, posX + width, posY, -width, height);
} else {
batch.draw(r, posX, posY, width, height);
}
return animationState;
}
Any suggestion is welcome as how to approach this.
Use some other batch.draw option (with other parameters). You can set "origin" parameters. It's like a hot spot...center of the image... So if you i.e. rotate, rotation will be done around that hot spot.
https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/Batch.html
I didn't use it for flipping, but it should work the same way. But if it doesn't then you have to adjust coordinates on your own, make some list with X offset for every frame and add it for flipped images.
Other solution would be to have wider frame images and keep center of the character always match the center of the image. That way your images will be wider then they have to - you'll have some empty space, but for sane number of frame it's acceptable.
What I am trying to accomplish is this:
We have CCSprite Circle A and CCSprite Circle B.
Move Circle B around Circle A. I already tried to create a CCNode and attach the circle B to it. In this case it works perfectly but the position is constant also. I need to move the circle around A and update the position. I will have more objects on the screen and I will check if B intersect some other objects, but for that case I need to update the position while rotating. Much appreciate your help guys. I am using Cocos2D v3.0
Maybe something like this? Put this in your update method. I used this code in my box2d project for orbit. Change it for your needs.
b2Vec2 center = bodyA->GetPosition();
int smoothness = 1000;
int radius = 100;
for (int i = 0; i < smoothness; i++) {
float angle = (i / smoothness) * 360 * DEGTORAD;
b2Vec2 pos( sinf(angle), cosf(angle));
b2Vec2 newposition = center + radius * pos;
bodyB->SetTransform(newposition, bodyB->GetAngle());
}
Put the anchor point of B in the position of the anchor point of A and then rotate the B by 360 in animation.
I want to scroll my screen to LHS and RHS. So, iam using the following statement to scroll the screen to RHS and its working fine
solo.scrollToSide(Solo.RIGHT);
but when iam using the following statement to scroll the screen to LHS, the screen is not scrolling to left
solo.scrollToSide(Solo.LEFT);
even i tried solo.scrollToSide(21);
but no use....?
Why don't you use this method:
void drag(float fromX, float toX, float fromY, float toY, int stepCount)
Simulate touching the specified location and dragging it to a new location.
This is the official documentation about method:
drag
public void drag(float fromX,
float toX,
float fromY,
float toY,
int stepCount)
Simulate touching the specified location and dragging it to a new location.
Parameters:fromX - X coordinate of the initial touch, in screen coordinatestoX - X coordinate of the drag destination, in screen coordinatesfromY - Y coordinate of the initial touch, in screen coordinatestoY - Y coordinate of the drag destination, in screen coordinatesstepCount - How many move steps to include in the drag
I'm at a bit of a loss here, forgive me if this has already been asked - i've have searched google high and low but i cant find anything?
i'm trying to rotate a group of sprites that are generated in a class, then rotating this object in the main gamescene on a menuitem click but the rotation is not at the center of the sprite? it a some larger area probably the layer size?
I've tried setting the anchorpoint to every possible combination?
Here is what iv got
This is the gamecharacter.h
#define COMPUTE_X(x) ((abs(x)) * 16) + (16*2) + (16/2)
#define COMPUTE_Y(y) (386 - (abs(y) * 16)) + (16/2)
#define COMPUTE_X_Y(x,y) ccp( COMPUTE_X(x), COMPUTE_Y(y))
// Game character class
#include "cocos2d.h"
using namespace cocos2d;
//a class to encapsulate playable game character by creating a group of sprites etc..
#ifndef GAMECHARACTER_H
#define GAMECHARACTER_H
class GameCharacter : public CCNode {
private:
//some private methods etc....
public:
void addSprite(const char* filename);
void setInitialPosition(CCPoint* position);
//Various other methods.........
};
#endif
void GameCharacter::addSprite(const char* filename)
{
//go get the sprite sheet
CCTexture2D* gameArtTexture = CCTextureCache::sharedTextureCache()->addPVRImage("SpriteSheet.pvr.ccz");
CCSpriteBatchNode::batchNodeWithTexture(gameArtTexture);
CCSprite *tempBlock = CCSprite::spriteWithSpriteFrameName(filename);
this->addChild((CCSprite*)tempBlock,0);
}
void GameCharacter::setInitialPosition(CCPoint* position)
{
//loop through the positions and set the character up
CCArray *children = this->getChildren();
CCSprite *currentBlock;
for (int i=0;i<7;i++){
currentBlock = (CCSprite*) children->objectAtIndex(i);
//compute x y grid positions (1,1) ---> to real (72,394)
currentBlock->setPosition(COMPUTE_X_Y(position[i].x,position[i].y));
}
}
This is the gamecharacter.cpp
void GameScene::AddCharacter(CCPoint* position)
{
const char* filename;
GameCharacter* character = new GameCharacter();
for (int i = 0; i < 7; i++) {
filename = helperFunctions::Format("character%d.png",i+1); //character1.png -> character7.png
character->addSprite(filename);
}
character->setInitialPosition(position);
this->addChild((CCSprite*) character,-1,2);
_sprite = character;
}
//here is the menuitem click handler
void GameScene::menuRotateRightCallback(CCObject* pSender)
{
//rotate the character right
//really slowly so we can see whats happening
_sprite->runAction((CCRotateBy::actionWithDuration(2.50,90)));
}
Thanks
It is much easier to do, using
x = center.x + cos(angle) * radius;
y = center.y + sin(angle) * radius;
Ive figured it out, looking at the docs for CCNode made me think.
CCNode has a position of (0,0) by default, so the rotation was using this as an origin.
Setting the position to the center of where i want the character with a bit of maths to calculate the offsets works for me.
I am trying to draw a grid on the screen of a Windows Phone; it will help me better position my sprites on the screen rather than guessing locations on the screen.
I have found several examples of a grid (2d or 3d) using XNA 3.0, but unfortunately the architectures are different and so the code doesnt work in XNA 4.0
Does anyone have something that could work?
Thanks!
You can download a PrimitiveBatch class here http://create.msdn.com/en-US/education/catalog/sample/primitives and use the code below to generate an appropriate grid as a texture.
PrimitiveBatch primitiveBatch;
private Texture2D GenerateGrid(Rectangle destRect, int cols, int rows, Color gridColor, int cellSize)
{
int w = (int)(cols * gridCellSize);
int h = (int)(rows * gridCellSize);
float uselessWidth = destRect.Width - w;
float uselessHeigth = destRect.Height - h;
Rectangle bounds = new Rectangle((int)(uselessWidth / 2) + destRect.X, (int)(uselessHeigth / 2) + destRect.Y, w, h);
RenderTarget2D grid = new RenderTarget2D(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
GraphicsDevice.SetRenderTarget(grid);
GraphicsDevice.Clear(Color.Transparent);
primitiveBatch.Begin(PrimitiveType.LineList);
float x = bounds.X;
float y = bounds.Y;
for (int col = 0; col < cols + 1; col++)
{
primitiveBatch.AddVertex(new Vector2(x + (col * gridCellSize), bounds.Top), gridColor);
primitiveBatch.AddVertex(new Vector2(x + (col * gridCellSize), bounds.Bottom), gridColor);
}
for (int row = 0; row < rows + 1; row++)
{
primitiveBatch.AddVertex(new Vector2(bounds.Left, y + (row * gridCellSize)), gridColor);
primitiveBatch.AddVertex(new Vector2(bounds.Right, y + (row * gridCellSize)), gridColor);
}
primitiveBatch.End();
GraphicsDevice.SetRenderTarget(null);
return grid;
}
One quick and dirty way you can do it (as it is for debugging the quicker the better) is to simply create a texture of a grid that is of the same resolution you are running your XNA game at (if you are running it at the same resolution as the phone, this will be 480x800). Most of the texture will simply be an alpha map and with grid lines of one pixel, you could create multiple resolutions or you can repeat a small texture of a single pixel cross dividing a section of the screen that is divisible by the resolution you are running at.
The draw method will be something as below and be called everyframe.
This code can declared inside your game class
Texture2D gridTexture;
Rectangle gridRectangle;
This code should be in your LoadContent method
//Best to use something like a png file
gridTexture = content.Load<Texture2D>("pathto/mygridtexture");
gridRectangle = new Rectangle(0,0,resolutionX,resolutionY);
This method should be called from your Draw method last to ensure it is on top assuming you are just using the standard spriteBatch.Begin() to render sprites (first if you are doing FrontToBack rendering).
public void DrawGrid()
{
spriteBatch.Draw(gridTexture, gridRectangle, Color.White);
}
This grid will remain stationary throughout the running of your application and should be useful when trying to line up your UI or objects that have relative positions in your game.
HTH.
You may want to take a look at the XPF project by RedBadger. It enables you to use XAML style layout in an XNA project.