jeudi 16 juin 2016

Libgdx if statement is not working well

I am trying to make the player die when it hits the rock or the time is up and the score is not reached, in this code at the first if statement it works when Rockhit is true, the player jump and disappear at the bottom of screen and the music is work very well, but when the time is 0 and score is not reached , the player is jump and keeps going out of the top of screen and the sound is work but slow and not clear. (notice: that before I adding the Rockhit condition the player was going up out of screen and the voice wasn't clear) this is the code:

 public State getState()
        {
            //Gdx.app.log(Float.toString(b2body.getLinearVelocity().x),"hi");
            if ((Hud.getTime()==0&& Hud.getScore()<(level*15)+50)|| Rockhit()) {
                Fruits.manager.get("music/Backmusic.ogg", Music.class).stop();
                collectoIsDead=true;
                Filter filter = new Filter();
                filter.maskBits = Fruits.NOTHING_BIT;
                for (Fixture fixture : b2body.getFixtureList())
                    fixture.setFilterData(filter);
                b2body.applyLinearImpulse(new Vector2(0, 5f), b2body.getWorldCenter(), true);
                //b2body.applyLinearImpulse(new Vector2(0,-2.5f), b2body.getWorldCenter(), true);
                Fruits.manager.get("music/fail.mp3", Sound.class).play();
                return State.DEAD;

            }

            if((Hud.getTime()==0&& Hud.getScore()>=(level*18)+50)|| (Hud.getScore()>=(level*15)+50)) {
                Fruits.manager.get("music/Backmusic.ogg", Music.class).stop();
                Fruits.manager.get("music/cheering.mp3", Sound.class).play();
                return State.SUCCESS;
            }
            if (b2body.getLinearVelocity().x!=0)
                return State.RUNNING;
            else
                return State.STANDING;


}

here is the full code of the class

public Collector(World world,PlayScreen screen,float level)
    {
        super(screen.getAtlas().findRegion("myboy1"));
        this.level=level;
        this.world=world;
        currentState=State.STANDING;
        previousState=State.STANDING;
        stateTimer=0;
        rocknum=3;
        runningRight=true;
        collectoIsDead=false;
        Array<TextureRegion> frames=new Array<TextureRegion>();
        //for(int i=0;i<3;i++)
        frames.add(new TextureRegion(getTexture(),0,153,90,300));
        frames.add(new TextureRegion(getTexture(),90,153,100,300));
        frames.add(new TextureRegion(getTexture(),200,153,100,300));

        collectorRun=new Animation(0.1f,frames);
        frames.clear();


        collectorStand=new TextureRegion(getTexture(), 0, 153, 89, 300);
        defineCollector();
        setBounds(0, 0, 35 / Fruits.PPM, 75 / Fruits.PPM);//here we can change the size of our Animation
        setRegion(collectorStand);
    }
    public TextureRegion myregion(float dt)
    {
        TextureRegion region;
        region=collectorStand;

        if(b2body.getLinearVelocity().x<0 )
        {
            region.flip(true, false);
        }
        return region;
    }

Aucun commentaire:

Enregistrer un commentaire