如何使用精灵作为另一个精灵锚点? [英] how to use a sprite as an anchor point of another sprite?
问题描述
我想知道,如果你可以,连接两个精灵在libgdx。
I want to know , if you can , join two sprites in libgdx .
让我解释一下,我有一个飞船,我必须把我的游戏...而且是第一个精灵。
Let me explain, I have a spaceship that I have to turn in my game ... and is the first sprite .
另一个精灵我会把飞船上的提示,这样,当它变成我,我通过第二雪碧我总是可以知道的坐标,因此得出一个载体,以推进我的船。
another sprite I would put on the tip of the spacecraft so that when it turns me I through the second sprite I can always know the coordinates and so derive a vector in order to advance my ship .
我是新来的libgdx,如果任何人有任何想法将不胜感激。
I'm new to libgdx and if anyone had any idea would be grateful.
感谢你。
修改 :我会按照它总是使其他精灵的位置,目前还没有发生,提示吗?谢谢你。
EDIT : I would follow that always render the position of the other sprites , currently does not happen , tips ? Thank You.
@Override
public void render () {
sprite.setPosition(Gdx.graphics.getWidth() / 2 - sprite.getWidth()/2, Gdx.graphics.getHeight() / 2 - sprite.getHeight()/2);
point.setPosition(sprite.getX() + (sprite.getWidth()/2 - point.getWidth()/2), sprite.getY() + (sprite.getHeight() / 2 + point.getHeight()));
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if(Gdx.input.isButtonPressed(Input.Buttons.LEFT)){
//sprite.setPosition(Gdx.input.getX() - sprite.getWidth()/2, Gdx.graphics.getHeight() - Gdx.input.getY() - sprite.getHeight()/2);
if(Gdx.input.getX() < Gdx.graphics.getWidth() / 2)
{
//System.out.println("x: " + Gdx.input.getX() + " - y: " + Gdx.input.getY());
sprite.setRotation(rotation++);
}
else
{
System.out.println("x: " + Gdx.input.getX() + " - y: " + Gdx.input.getY());
sprite.setRotation(rotation--);
point.setRotation(rotation--);
}
}
batch.begin();
spacesprite.draw(batch);
sprite.draw(batch);
point.draw(batch);
batch.end();
}
编辑2
正如你可以在图片中看到,我想,当我旋转飞船的精灵,红点(这是另一种精灵)留在飞船的一角。所以,你可以计算出与两个精灵的起源点的载体。
As you can see in the picture , I would like that when I rotate the sprite of the spacecraft , the red dot ( which is another sprite ) remain on the tip of the spacecraft . So you can calculate a vector with the points of origin of the two sprites .
我希望已经明确。 两个精灵联合是不可能在libgdx别的吗?
I hope to have been clear . A union of two sprites is not possible in libgdx else?
修改3
照片,我希望永远当船旋转红球留在飞船的一角。这可能吗?
picture , I wish that when the ship rotates the red ball remains always on the tip of the spacecraft . is it possible?
推荐答案
如果你的两个精灵具有相同的宽度可以使用这样的:
If your two sprite have the same width can use this:
这是psudo code铁道部或更少。
this is psudo code mor or less.
float pointY = yourSprite.getY() + yourSprite.getHeight();
float pointX = yourSprite.getX();
yourOtherSprite. setX(pointX);
yourOtherSprite. setY(pointY);
如果diferente宽度
if diferente width
float pointY = yourSprite.getY() + yourSprite.getHeight();
float pointX = yourSprite.getX() + (yourSprite.getWidth() / 2);
yourOtherSprite. setX(pointX - (thisSprite.getWidth() / 2));
yourOtherSprite. setY(pointY);
我现在不能测试,但让你有一个想法。
I can not test now, but so you have an idea.
新
我不知道这是你的意思,如果不是这样评论,并删除:
I do not know if is this what you mean if not so comment it and delete:
P.S:点*是一个Vector2:
P.s: points* is a Vector2:
pointSpriteA.x = yourSprite.getX() + (yourSprite.getHeight() / 2);
pointSpriteB.x = yourOtherSprite.getY() + (yourOtherSprite.getHeight() / 2);
pointSpriteA.y = yourSprite.getY() + (yourSprite.getWidth() / 2);
pointSpriteB.y = yourOtherSprite.getY() + (yourOtherSprite.getWidth() / 2);
pointC.X = (pointSpriteA.x + pointSpriteB.x) / 2);
pointC.y = (pointSpriteA.Y + pointSpriteB.y) / 2);
的pointC向量是两个向量的中心。
the pointC vector is the center of the two vectors.
如果这就是你想要什么,并问自己,怎么转?精灵可这有助于。
if that's what you want, and ask yourself, how to rotate? sprites can this helps.
<一个href="http://stackoverflow.com/questions/27555023/libgdx-rotate-a-2d-array-of-sprites-around-their-center/27555676#27555676">LibGDX - 绕其中心精灵的二维数组
这篇关于如何使用精灵作为另一个精灵锚点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!