无法偏移纹理原点:Sprite旋转偏移设置失效技术求助
Fixing Sprite Origin & Rotation for Your FireRifle
Let's work through your problem step by step—you're trying to get your FireRifle to rotate around its top edge instead of the default bottom-left origin, but your current origin setup isn't working, and the sprite isn't moving as expected when flipping directions.
First, Let's Diagnose the Issues in Your Code
- Origin is being overwritten repeatedly: In your
update()method, you set the origin twice (once inside therunningRightblock, once at the end), which cancels out any intentional setup. You're also usingPlayScreen.larm's origin instead of defining one specific to the rifle itself. - Flip logic is inconsistent: Your flip checks (
xflip/yflipflags) are causing unnecessary state toggles, and the position offset when running right doesn't account for how flipping affects the sprite's anchor point. - Order of operations is off: You're setting rotation after position, but origin/flip state should be set before adjusting position or rotation to get the correct behavior.
Here's the Fixed & Explained Code
Let's rewrite your FireRifle class to fix these issues:
package com.mygdx.game.Sprites.weapons; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.physics.box2d.World; import com.mygdx.game.PlayScreen; import com.mygdx.game.main; import com.mygdx.game.Sprites.player.LArm; import com.mygdx.game.Sprites.player.Player; @SuppressWarnings("unused") public class FireRifle extends Sprite { private World world; // Store the rifle's size in world units for easy reference private final float rifleWidth = 8 / main.PPM; private final float rifleHeight = 19 / main.PPM; public FireRifle(PlayScreen screen){ this.world = screen.getWorld(); setRegion(new Texture(Gdx.files.internal("fire rifle.png"))); // Set origin to the TOP CENTER of the rifle—this is where rotation will pivot setOrigin(rifleWidth / 2, rifleHeight); // Initialize bounds and position relative to the arm setBounds(0, 0, rifleWidth, rifleHeight); updatePosition(); } public void update(float dt) { handleDirectionFlip(); updatePosition(); // Match the arm's rotation (adjust the offset if needed) setRotation(PlayScreen.larm.getRotation()); } private void handleDirectionFlip() { // Flip the sprite based on player's direction, no need for extra flags boolean shouldFlipX = !Player.runningRight; if (isFlipX() != shouldFlipX) { flip(true, false); } // Remove the unnecessary Y flip—you probably don't need this unless your texture is upside-down by default if (isFlipY()) { flip(false, true); } } private void updatePosition() { float armX = PlayScreen.larm.getX(); float armY = PlayScreen.larm.getY(); if (Player.runningRight) { // When facing right, position the rifle so its top origin aligns with the arm's position setPosition(armX - (rifleWidth / 2), armY); } else { // When facing left, since we flipped the sprite, adjust the X position to keep the origin aligned setPosition(armX - (rifleWidth / 2), armY); } // If you need a small Y offset when facing right, add it here (e.g., + 5/main.PPM) // setY(getY() + 5/main.PPM); } }
Key Changes Explained
- Fixed Origin Setup: We set the origin once in the constructor to the top center of the rifle (
rifleWidth/2, rifleHeight). This ensures all rotations pivot around the top edge, exactly what you wanted. - Simplified Flip Logic: We removed the
xflip/yflipflags and just check if the sprite's flip state matches the player's direction. This avoids messy state toggles. - Separate Position Update: The
updatePosition()method handles aligning the rifle's origin with the arm's position, accounting for flip state so the rotation pivot stays in the right place. - Cleaner Order of Operations: We handle flipping first, then position, then rotation—this ensures each step uses the correct state from the previous one.
Quick Troubleshooting Tips
- If the rotation still feels off, adjust the origin coordinates (e.g., use
rifleWidth * 0.3finstead ofrifleWidth/2to pivot from the top-left instead of center). - If the position is misaligned when flipping, tweak the
setPosition()values inupdatePosition()to match your arm sprite's anchor point.
内容的提问来源于stack exchange,提问作者Lily Peterson
相关产品推荐
相关产品推荐

