简体   繁体   English

如何相对于另一个精灵的旋转移动精灵?

[英]How to move a sprite relative to another sprite's rotation?

In my game I have a rocket sprite and at the bottom of the rocket there is an animated flame sprite , I want the flame sprite to align with the bottom of the rocket when the rocket rotates. 在我的游戏中,我有一个火箭精灵,在火箭底部有一个动画的火焰精灵,我希望当火箭旋转时,火焰精灵与火箭底部对齐。

This is the rocket when the game starts, everything is OK: 这是游戏开始时的火箭,一切正常:

初始位置

But when i rotate the rocket the flame does not align with the rocket's bottom like this: 但是当我旋转火箭时,火焰不会像这样与火箭底部对齐:

旋转后

How can i make it align? 我如何使其对齐?

here is my code: 这是我的代码:

    // draw rocket
    rocketSprite.setRegion(AssetLoader.rocketTexture);
    rocketSprite.setPosition(rocket.getX(), rocket.getY());
    rocketSprite.setOrigin(rocket.getWidth() / 2, rocket.getHeight() / 2);
    rocketSprite.setSize(rocket.getWidth(), rocket.getHeight());
    rocketprite.setRotation(rocket.getRotation());
    rocketSprite.setScale(1, 1);
    rocketPodSprite.draw(game.batch);


    //flame width and height
    int w = 70;
    int h = 20;

    // randomly change scale of flame for effects
    float sw = MathUtils.random(2f / 7f, 1);
    float sh = MathUtils.random(0.5f, 1);

    // draw flame
    flameSprite.setRegion(AssetLoader.flameTexture);
    flameSprite.setPosition(rocket.getX() + rocket.getWidth() / 2 - w, rocket.getY() - 15);
    flameSprite.setOrigin(w, h / 2);
    flameSprite.setSize(w, h);
    flameSprite.setRotation(rocket.getRotation());
    flameSprite.setScale(sw, sh);
    flameSprite.draw(game.batch);

You need to change origin - point arround which your sprite will be rotated. 您需要更改origin -旋转精灵的圆点。

Your problem is that both rocket and flame are rotated arround their own centres however you would rather like to rotate them both arround rocket's one. 您的问题是,火箭和火焰都绕着自己的中心旋转,但是您想同时绕着火箭的中心旋转。 Then you need to set the flame sprite origin with position of rocket center. 然后,需要用火箭中心的位置设置火焰精灵的原点。

    flameSprite.setOrigin(w, h / 2 + rocket.getHeight() / 2 + SPACE_BETWEEN_ROCKET_AND_FLAME);

As I see in your case the SPACE_BETWEEN_ROCKET_AND_FLAME has value of 15 - flame.height due to this position set: 如您15 - flame.height由于设置了此位置,因此SPACE_BETWEEN_ROCKET_AND_FLAME值为15 - flame.height

    flameSprite.setPosition(rocket.getX() + rocket.getWidth() / 2 - w, rocket.getY() - 15);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM