如何使用一个精灵作为另一个精灵的锚点? [英] how to use a sprite as an anchor point of another sprite?

查看:30
本文介绍了如何使用一个精灵作为另一个精灵的锚点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,如果可以的话,在 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:

这是或多或少的伪代码.

this is psudo code mor or less.

float pointY = yourSprite.getY() + yourSprite.getHeight();
float pointX = yourSprite.getX(); 

yourOtherSprite. setX(pointX);
yourOtherSprite. setY(pointY);

如果宽度不同

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:points* 是一个 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.

LibGDX - 旋转二维围绕其中心的精灵阵列

这篇关于如何使用一个精灵作为另一个精灵的锚点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆