为什么eclipse用颜色填充我的纹理?

时间:2015-08-14 09:21:32

标签: java android eclipse opengl-es

我正在制作一个游戏,其中一个物体的角落里有一个白色的填充物:enter image description here

你能帮我解决吗?我不想在角落里有任何颜色,只是像其他物体一样空的纹理?顺便说一下,它们来自一个纹理文件,没有意义只用颜色填充它。 这是我的加载类:

package com.vratsasoftware.zbhelpers;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class AssetLoader {
    public static Texture texture, logoTexture, texture1;
    public static TextureRegion bg;
    public static TextureRegion grass;
    public static Animation logoAnimation;
    public static TextureRegion logo, logoDown, logoUp, playButtonUp,
            playButtonDown, ready, gameOver, highScore, scoreboard, star,
            gameLogo, noStar, retry, firstCandy, secondCandy, mmLogo;
    public static TextureRegion barTopUp, barTopDown, bar;
    public static TextureRegion androidLogo, appleLogo, enemyLogo;
    public static Sound dead, flap, coin, fall, shoot, hit;
    public static BitmapFont font, shadow, whiteFont;
    private static Preferences prefs;

    public static void load() {
        logoTexture = new Texture(Gdx.files.internal("data/logo.png"));
        logoTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
        mmLogo = new TextureRegion(logoTexture, 0, 0, 512, 114);

        texture = new Texture(Gdx.files.internal("data/texture.png"));
        texture.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);

        texture1 = new Texture(Gdx.files.internal("data/texture1.png"));
        texture1.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);


        playButtonUp = new TextureRegion(texture, 0, 83, 29, 16);
        playButtonDown = new TextureRegion(texture, 29, 83, 29, 16);
        playButtonUp.flip(false, true);
        playButtonDown.flip(false, true);

        ready = new TextureRegion(texture, 59, 83, 34, 7);
        ready.flip(false, true);

        retry = new TextureRegion(texture, 59, 110, 33, 7);
        retry.flip(false, true);

        gameOver = new TextureRegion(texture, 59, 92, 46, 7);
        gameOver.flip(false, true);

        scoreboard = new TextureRegion(texture, 111, 83, 97, 37);
        scoreboard.flip(false, true);

        star = new TextureRegion(texture, 152, 70, 10, 10);
        noStar = new TextureRegion(texture, 165, 70, 10, 10);
        star.flip(false, true);
        noStar.flip(false, true);

        highScore = new TextureRegion(texture, 59, 101, 48, 7);
        highScore.flip(false, true);

        gameLogo = new TextureRegion(texture1, 67, 444, 160, 36);
        gameLogo.flip(false, true);

        bg = new TextureRegion(texture1, 0, 0, 490, 225);
        bg.flip(false, true);

        grass = new TextureRegion(texture, 0, 43, 143, 11);
        grass.flip(false, true);

        logoDown = new TextureRegion(texture1, 0, 225, 340, 200);
        logoDown.flip(false, true);
        logo = new TextureRegion(texture1, 0, 225, 340, 200);
        logo.flip(false, true);
        logoUp = new TextureRegion(texture1, 0, 225, 340, 200);
        logoUp.flip(false, true);

        TextureRegion[] birds = { logoDown, logo, logoUp };
        logoAnimation = new Animation(0.06f, birds);
        logoAnimation.setPlayMode(Animation.PlayMode.LOOP_PINGPONG);


        //Pipe!!!
        barTopUp = new TextureRegion(texture, 192, 0, 24, 14);
        // Create by flipping existing skullUp
        barTopDown = new TextureRegion(barTopUp);
        barTopDown.flip(false, true);

        androidLogo = new TextureRegion(texture1, 0, 455, 60, 30);
        androidLogo.flip(false, true);
        appleLogo = new TextureRegion(texture1, 460, 245, 300, 230);
        appleLogo.flip(false, true);
        enemyLogo = new TextureRegion(texture1, 492, 0, 225, 225);
        enemyLogo.flip(true, true);

        bar = new TextureRegion(texture, 136, 16, 22, 3);
        bar.flip(false, true);

        dead = Gdx.audio.newSound(Gdx.files.internal("data/dead.wav"));
        flap = Gdx.audio.newSound(Gdx.files.internal("data/flap.wav"));
        coin = Gdx.audio.newSound(Gdx.files.internal("data/coin.wav"));
        fall = Gdx.audio.newSound(Gdx.files.internal("data/fall.wav"));
        shoot = Gdx.audio.newSound(Gdx.files.internal("data/shoot.wav"));
        hit = Gdx.audio.newSound(Gdx.files.internal("data/hit.wav"));

        font = new BitmapFont(Gdx.files.internal("data/text.fnt"));
        font.getData().setScale(.25f, -.25f);

        whiteFont = new BitmapFont(Gdx.files.internal("data/whitetext.fnt"));
        whiteFont.getData().setScale(.1f, -.1f);

        shadow = new BitmapFont(Gdx.files.internal("data/shadow.fnt"));
        shadow.getData().setScale(.25f, -.25f);
        // Create (or retrieve existing) preferences file
        prefs = Gdx.app.getPreferences("MMGame");

        if (!prefs.contains("highScore")) {
            prefs.putInteger("highScore", 0);
        }
    }

    public static void setHighScore(int val) {
        prefs.putInteger("highScore", val);
        prefs.flush();
    }

    public static int getHighScore() {
        return prefs.getInteger("highScore");
    }

    public static void dispose() {
        // We must dispose of the texture when we are finished.
        texture.dispose();
        texture1.dispose();
        // Dispose sounds
        dead.dispose();
        flap.dispose();
        coin.dispose();
        fall.dispose();
        shoot.dispose();
        hit.dispose();
        font.dispose();
        shadow.dispose();
    }
}

EnemyLogo是有问题的星球。

这是我使用它们的渲染类:

package com.vratsasoftware.gameworld;

import java.util.List;

import aurelienribon.tweenengine.Tween;
import aurelienribon.tweenengine.TweenEquations;
import aurelienribon.tweenengine.TweenManager;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.vratsasoftware.gameobjects.BonusLogo;
import com.vratsasoftware.gameobjects.EnemyLogo;
import com.vratsasoftware.gameobjects.Grass;
import com.vratsasoftware.gameobjects.Logo;
import com.vratsasoftware.gameobjects.ScrollHandler;
import com.vratsasoftware.tweenaccessors.Value;
import com.vratsasoftware.tweenaccessors.ValueAccessor;
import com.vratsasoftware.ui.SimpleButton;
import com.vratsasoftware.zbhelpers.AssetLoader;
import com.vratsasoftware.zbhelpers.InputHandler;

public class GameRenderer {

    // Game Objects
    private Logo logo;

    // Game Assets
    private Animation logoAnimation;
    private TextureRegion bg, grass;
    private TextureRegion logoMid, mmGameLogo;
    private TextureRegion bonusLogoPlanet1, bonusLogoPlanet2, enemyLogo, bonusLogoRocket;
    private TextureRegion ready, gameOver, highScore, scoreboard, star, noStar, retry;

    private ScrollHandler scroller;
    private Grass frontGrass, backGrass;
    private BonusLogo bLogo1, bLogo2, bullet;

    private EnemyLogo enemyObject;

    private GameWorld myWorld;
    private OrthographicCamera cam;
    private ShapeRenderer shapeRenderer;

    private SpriteBatch batcher;

    public SpriteBatch getBatcher() {
        return batcher;
    }

    private int midPointY;

    // Tween stuff
    private TweenManager manager;
    private Value alpha = new Value();

    private Color transitionColor;

    // Buttons
    private List<SimpleButton> menuButtons;




    public GameRenderer(GameWorld world, int gameHeight, int midPointY) {
        myWorld = world;

        // The word "this" refers to this instance.
        // We are setting the instance variables' values to be that of the
        // parameters passed in from GameScreen.

        this.midPointY = midPointY;
        this.menuButtons = ((InputHandler) Gdx.input.getInputProcessor()).getMenuButtons();

        cam = new OrthographicCamera();
        cam.setToOrtho(true, 136, gameHeight);

        batcher = new SpriteBatch();
        batcher.setProjectionMatrix(cam.combined);
        shapeRenderer = new ShapeRenderer();
        shapeRenderer.setProjectionMatrix(cam.combined);

        // Call helper methods to initialize instance variables
        initGameObjects();
        initAssets();

        transitionColor = new Color();
        // setupTween
        prepareTransition(255, 255, 255, .5f);

    }

    private void initGameObjects() {
        logo = myWorld.getLogo();
        scroller = myWorld.getScroller();
        frontGrass = scroller.getFrontGrass();
        backGrass = scroller.getBackGrass();
        bLogo1 = scroller.getBonusLogo1();
        bLogo2 = scroller.getBonusLogo2();
        bullet = scroller.getBullet();
        enemyObject = scroller.getEnemyObject();
    }

    private void initAssets() {
        bg = AssetLoader.bg;
        mmGameLogo = AssetLoader.gameLogo;
        grass = AssetLoader.grass;
        logoAnimation = AssetLoader.logoAnimation;
        logoMid = AssetLoader.logo;
        bonusLogoRocket = AssetLoader.androidLogo;
        bonusLogoPlanet1 = AssetLoader.appleLogo;
        bonusLogoPlanet2 = AssetLoader.appleLogo;
        enemyLogo = AssetLoader.enemyLogo;
        highScore = AssetLoader.highScore;
        scoreboard = AssetLoader.scoreboard;
        retry = AssetLoader.retry;
        ready = AssetLoader.ready;
        star = AssetLoader.star;
        noStar = AssetLoader.noStar;
        gameOver = AssetLoader.gameOver;

    }

    private void drawGrass() {
        // Draw the grass
        batcher.draw(grass, frontGrass.getX(), frontGrass.getY(), frontGrass.getWidth(), frontGrass.getHeight());
        batcher.draw(grass, backGrass.getX(), backGrass.getY(), backGrass.getWidth(), backGrass.getHeight());
    }

    private void drawLogos() {
        // Draw the first logo
        if (bLogo1.isVisible() && logo.isAlive()) {
            batcher.draw(bonusLogoPlanet1, bLogo1.getX(), bLogo1.getY(), bLogo1.getWidth(), bLogo1.getHeight());
        }

        // Draw the second logo
        if (bLogo2.isVisible() && logo.isAlive()) {
            batcher.draw(bonusLogoPlanet2, bLogo2.getX(), bLogo2.getY(), bLogo2.getWidth(), bLogo2.getHeight());
        }

    }

    public void drawBullet() {
        if (myWorld.isRunning()) {
            if (bullet.isVisible() && logo.isAlive()) {
                batcher.draw(bonusLogoRocket, bullet.getX(), bullet.getY(), bullet.getWidth(), bullet.getHeight());
            }
        } else {
            bullet.setVisible(false);
        }
    }
    private void drawEnemyObject() {
        if (myWorld.isRunning()) {
            if (enemyObject.isVisible() && logo.isAlive()) {
                batcher.draw(enemyLogo, enemyObject.getX(), enemyObject.getY(), enemyObject.getWidth(), enemyObject.getHeight());
            }
        } else {
            enemyObject.setVisible(false);
        }

    }

    private void drawLogoCentered(float runTime) {
        batcher.draw(logoAnimation.getKeyFrame(runTime), 59, logo.getY() - 15, logo.getWidth() / 2.0f,
                logo.getHeight() / 2.0f, logo.getWidth(), logo.getHeight(), 1, 1, logo.getRotation());
    }

    private void drawLogo(float runTime) {

        if (logo.shouldntFlap()) {
            batcher.draw(logoMid, logo.getX(), logo.getY(), logo.getWidth() / 2.0f, logo.getHeight() / 2.0f,
                    logo.getWidth(), logo.getHeight(), 1, 1, logo.getRotation());

        } else {
            batcher.draw(logoAnimation.getKeyFrame(runTime), logo.getX(), logo.getY(), logo.getWidth() / 2.0f,
                    logo.getHeight() / 2.0f, logo.getWidth(), logo.getHeight(), 1, 1, logo.getRotation());
        }

    }

    private void drawMenuUI() {
        batcher.draw(mmGameLogo, 136 / 2 - 56, midPointY - 50, mmGameLogo.getRegionWidth() / 1.2f,
                mmGameLogo.getRegionHeight() / 1.2f);

        for (SimpleButton button : menuButtons) {
            button.draw(batcher);
        }

    }

    private void drawScoreboard() {
        batcher.draw(scoreboard, 22, midPointY - 30, 97, 37);

        batcher.draw(noStar, 25, midPointY - 15, 10, 10);
        batcher.draw(noStar, 37, midPointY - 15, 10, 10);
        batcher.draw(noStar, 49, midPointY - 15, 10, 10);
        batcher.draw(noStar, 61, midPointY - 15, 10, 10);
        batcher.draw(noStar, 73, midPointY - 15, 10, 10);

        if (myWorld.getScore() > 5) {
            batcher.draw(star, 73, midPointY - 15, 10, 10);
        }

        if (myWorld.getScore() > 17) {
            batcher.draw(star, 61, midPointY - 15, 10, 10);
        }

        if (myWorld.getScore() > 50) {
            batcher.draw(star, 49, midPointY - 15, 10, 10);
        }

        if (myWorld.getScore() > 80) {
            batcher.draw(star, 37, midPointY - 15, 10, 10);
        }

        if (myWorld.getScore() > 120) {
            batcher.draw(star, 25, midPointY - 15, 10, 10);
        }

        int length = ("" + myWorld.getScore()).length();

        AssetLoader.whiteFont.draw(batcher, "" + myWorld.getScore(), 104 - (2 * length), midPointY - 20);

        int length2 = ("" + AssetLoader.getHighScore()).length();
        AssetLoader.whiteFont.draw(batcher, "" + AssetLoader.getHighScore(), 104 - (2.5f * length2), midPointY - 3);

    }

    private void drawRetry() {
        batcher.draw(retry, 36, midPointY + 10, 66, 14);
    }

    private void drawReady() {
        batcher.draw(ready, 36, midPointY - 50, 68, 14);
    }

    private void drawGameOver() {
        batcher.draw(gameOver, 24, midPointY - 50, 92, 14);
    }

    private void drawScore() {
        int length = ("" + myWorld.getScore()).length();
        AssetLoader.shadow.draw(batcher, "" + myWorld.getScore(), 68 - (3 * length), midPointY - 82);
        AssetLoader.font.draw(batcher, "" + myWorld.getScore(), 68 - (3 * length), midPointY - 83);
    }

    private void drawHighScore() {
        batcher.draw(highScore, 22, midPointY - 50, 96, 14);
    }

    public void render(float delta, float runTime) {

        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        shapeRenderer.begin(ShapeType.Filled);

        // Draw Background color 108, 180, 226, 0.5f
        shapeRenderer.setColor(10 / 255.0f, 16 / 255.0f, 12 / 255.0f, 1);
        shapeRenderer.rect(0, 0, 136, midPointY + 66);

        // Draw Grass
        shapeRenderer.setColor(111 / 255.0f, 186 / 255.0f, 45 / 255.0f, 1);
        shapeRenderer.rect(0, midPointY + 66, 136, 11);

        // Draw Dirt
        shapeRenderer.setColor(147 / 255.0f, 80 / 255.0f, 27 / 255.0f, 1);
        shapeRenderer.rect(0, midPointY + 77, 136, 52);

        shapeRenderer.end();

        batcher.begin();
        batcher.disableBlending();

        batcher.draw(bg, 0, midPointY + 23, 136, 43);


        drawEnemyObject();
        batcher.enableBlending();

        if (myWorld.isRunning()) {

            drawLogo(runTime);
            drawScore();
        } else if (myWorld.isReady()) {
            drawLogo(runTime);
            drawReady();
        } else if (myWorld.isMenu()) {
            drawLogoCentered(runTime);
            drawMenuUI();
        } else if (myWorld.isGameOver()) {
            drawScoreboard();
            drawLogo(runTime);
            drawGameOver();
            drawRetry();
        } else if (myWorld.isHighScore()) {
            drawScoreboard();
            drawLogo(runTime);
            drawHighScore();
            drawRetry();
        }

        drawGrass();
        batcher.end();
        batcher.begin();
        drawLogos();
        drawBullet();

        batcher.end();

        drawTransition(delta);

    }



    public void prepareTransition(int r, int g, int b, float duration) {
        transitionColor.set(r / 255.0f, g / 255.0f, b / 255.0f, 1);
        alpha.setValue(1);
        Tween.registerAccessor(Value.class, new ValueAccessor());
        manager = new TweenManager();
        Tween.to(alpha, -1, duration).target(0).ease(TweenEquations.easeOutQuad).start(manager);
    }

    private void drawTransition(float delta) {
        if (alpha.getValue() > 0) {
            manager.update(delta);
            Gdx.gl.glEnable(GL20.GL_BLEND);
            Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
            shapeRenderer.begin(ShapeType.Filled);
            shapeRenderer.setColor(transitionColor.r, transitionColor.g, transitionColor.b, alpha.getValue());
            shapeRenderer.rect(0, 0, 136, 300);
            shapeRenderer.end();
            Gdx.gl.glDisable(GL20.GL_BLEND);

        }
    }
}

这是我真正创建它们的课程:

package com.vratsasoftware.gameobjects;

import com.vratsasoftware.gameworld.GameWorld;
import com.vratsasoftware.zbhelpers.Sound;

public class ScrollHandler {

    private Grass frontGrass, backGrass;
    private Pipe pipe1, pipe2;
    private BonusLogo bonusLogo1, bonusLogo2, bullet;
    private EnemyLogo enemyObject;
    private GameWorld gameWorld;

    public static final int SCROLL_SPEED = -59;
    public static final int PIPE_GAP = 143;
    public ScrollHandler(GameWorld gameWorld, float yPos) {
        this.gameWorld = gameWorld;

        frontGrass = new Grass(0, yPos, 143, 11, SCROLL_SPEED);
        backGrass = new Grass(frontGrass.getTailX(), yPos, 143, 11, SCROLL_SPEED);

        pipe1 = new Pipe(210, 0, 22, 60, SCROLL_SPEED, yPos);
        bonusLogo1 = new BonusLogo(pipe1.getTailX() + PIPE_GAP / 2, 50, 17, 12, SCROLL_SPEED);

        pipe2 = new Pipe(pipe1.getTailX() + PIPE_GAP, 0, 22, 60, SCROLL_SPEED, yPos);
        bonusLogo2 = new BonusLogo(pipe2.getTailX() + PIPE_GAP / 2, 50, 17, 12, SCROLL_SPEED);

        bullet = new BonusLogo(gameWorld.getLogo().getX(), gameWorld.getLogo().getY(),
                (int) gameWorld.getLogo().getWidth(), (int) gameWorld.getLogo().getHeight(), -SCROLL_SPEED);

        setEnemyObject(new EnemyLogo(140, 15, 40, 40, SCROLL_SPEED));
    }



    public void updateReady(float delta) {
        frontGrass.update(delta);
        backGrass.update(delta);
        // Same with grass
        if (frontGrass.isScrolledLeft()) {
            frontGrass.reset(backGrass.getTailX());
        } else if (backGrass.isScrolledLeft()) {
            backGrass.reset(frontGrass.getTailX());
        }

    }

    public void update(float delta) {
        // Update our objects
        frontGrass.update(delta);
        backGrass.update(delta);
        pipe1.update(delta);
        bonusLogo1.update(delta);
        pipe2.update(delta);
        bonusLogo2.update(delta);
        bullet.update(delta);
        enemyObject.update(delta);
        // Check if any of the pipes are scrolled left, and reset accordingly
        // the bonusLogo
        if (pipe1.isScrolledLeft()) {
            pipe1.reset(pipe2.getTailX() + PIPE_GAP);
            bonusLogo2.reset(pipe2.getTailX() + PIPE_GAP / 2);
        } else if (pipe2.isScrolledLeft()) {
            pipe2.reset(pipe1.getTailX() + PIPE_GAP);
            bonusLogo1.reset(pipe1.getTailX() + PIPE_GAP / 2);
        }
        // Same with grass
        if (frontGrass.isScrolledLeft()) {
            frontGrass.reset(backGrass.getTailX());
        } else if (backGrass.isScrolledLeft()) {
            backGrass.reset(frontGrass.getTailX());
        }

        if (enemyObject.isScrolledLeft()) {
            enemyObject.reset(enemyObject.getTailX() + PIPE_GAP);

        } else if (enemyObject.isScrolledLeft()) {
            enemyObject.reset(enemyObject.getTailX() + PIPE_GAP);

        }



    }

    public void stop() {
        frontGrass.stop();
        backGrass.stop();
        pipe1.stop();
        pipe2.stop();
        bonusLogo1.stop();
        bonusLogo2.stop();
        bullet.stop();
        enemyObject.stop();
    }



    public void onRestart() {
        frontGrass.onRestart(0, SCROLL_SPEED);
        backGrass.onRestart(frontGrass.getTailX(), SCROLL_SPEED);
        pipe1.onRestart(210, SCROLL_SPEED);
        bonusLogo1.onRestart(pipe1.getTailX() + PIPE_GAP / 2, SCROLL_SPEED);
        pipe2.onRestart(pipe1.getTailX() + PIPE_GAP, SCROLL_SPEED);
        bonusLogo2.onRestart(pipe2.getTailX() + PIPE_GAP / 2, SCROLL_SPEED);
        bullet.onRestart(gameWorld.getLogo().getX(), -SCROLL_SPEED);
        enemyObject.onRestart(210, SCROLL_SPEED);
    }



    private void addScore(int increment) {
        gameWorld.addScore(increment);
    }

    public boolean scored(Logo bird) {
        if (bonusLogo1.collides(bird)) {
            if (!bonusLogo1.isScored()) {
                bonusLogo1.setScored(true);
                Sound.playCoin();
                addScore(1);
                bonusLogo1.setVisible(false);

                return true;
            } else {
                return true;
            }
        } else if (bonusLogo2.collides(bird)) {
            if (!bonusLogo2.isScored()) {
                bonusLogo2.setScored(true);
                Sound.playCoin();
                addScore(1);
                bonusLogo2.setVisible(false);

                return true;
            } else {
                return true;
            }
        } else if (enemyObject.collides(bullet)) {
            if (!enemyObject.isScored() && enemyObject.isVisible() && bullet.isVisible()) {
                enemyObject.setScored(true);
                Sound.playHit();
                enemyObject.setVisible(false);
                bullet.setVisible(false);

                return true;
            } else {
                return true;
            }
        } else {
            return false;
        }
    }

    public Grass getFrontGrass() {
        return frontGrass;
    }

    public Grass getBackGrass() {
        return backGrass;
    }

    public Pipe getPipe1() {
        return pipe1;
    }

    public Pipe getPipe2() {
        return pipe2;
    }

    public BonusLogo getBonusLogo1() {
        return bonusLogo1;
    }

    public BonusLogo getBonusLogo2() {
        return bonusLogo2;
    }

    public BonusLogo getBullet() {
        return bullet;
    }



    public EnemyLogo getEnemyObject() {
        return enemyObject;
    }



    public void setEnemyObject(EnemyLogo enemyLogo) {
        this.enemyObject = enemyLogo;
    }

}

1 个答案:

答案 0 :(得分:6)

在再次启用混合之前,您正在调用drawEnemyObject();,这意味着敌方对象上的透明背景显示为白色。重新启用批处理中的混合后,请尝试放置drawEnemyObject();

像这样:

batcher.enableBlending()
drawEnemyObject();