如何在Android中使用Libgdx中的方法触地移动玩家?

时间:2015-04-02 01:07:51

标签: android libgdx

  

我开始使用面向Android的Libgdx进行编程,我就在   让玩家正确移动的麻烦。我正在使用这种方法   触地得分,其中x和y坐标经过手机显示屏   坐标,但玩家在TiledMap中移动,即与不同   坐标,任何人都可以帮我构建方法的形式   玩家移动得当吗?方法代码如下。   应注意,播放器必须移动到播放的确切位置>在屏幕上

import  com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.mygdx.game.screens.Play;

public class Player extends Sprite implements InputProcessor {

    private Vector3 velocity=new Vector3();
    private Vector3 touch = new Vector3 (); 

    private float speed = (60 * 2), gravity =0,increment;
    private TiledMapTileLayer collisionLayer;
    private boolean dragged;
    private float deltaaux=0;
    private String blockedKey = "bloqueado";
    public Player (Sprite sprite,TiledMapTileLayer collisionLayer){
        super(sprite);
        this.collisionLayer = collisionLayer;
    }

    @Override
    public void draw(Batch batch) {
        // TODO Auto-generated method stub
        update(Gdx.graphics.getDeltaTime());
        super.draw(batch);
    } 

    public void update (float delta){
        deltaaux = delta;
        //velocity.y -= gravity * delta;
        //System.out.println("update y!!!");

        //clamp velocity

        if (velocity.y > speed)
            velocity.y = speed;

        else if (velocity.y < -speed)
            velocity.y = -speed;

        // save old position
        float oldX = getX(), oldY = getY(), tileWidth = collisionLayer.getTileWidth(), tileHeight=collisionLayer.getTileHeight();
        boolean collisionX = false, collisionY = false;

        //move x
        setX(getX() + velocity.x * delta);

        if(velocity.x < 0){
            System.out.println("velocity<0!!!");
            //acima esquerda
            collisionX = collisionLayer.getCell((int) (getX()/tileWidth), (int) ((getY() + getHeight())/tileHeight)).getTile().getProperties().containsKey("bloqueado");
            if(collisionX)
                System.out.println ("colidiu acima esquerda");

            //meio esquerda
            if(!collisionX){
            collisionX = collisionLayer.getCell((int) (getX()/tileWidth), (int) ((getY() + getHeight()/2)/tileHeight)).getTile().getProperties().containsKey("bloqueado");
            if(collisionX)
                System.out.println ("colidiu meio esquerda");
            }

            //abaixo esquerda
            if(!collisionX){
            collisionX = collisionLayer.getCell((int)(getX()/tileWidth), (int)(getY()/tileHeight)).getTile().getProperties().containsKey("bloqueado");
            if(collisionX)
                System.out.println ("colidiu abaixo esquerda");
            }

        }else if(velocity.x>0){
            System.out.println("velocity.x >0");
            //acima direita
            collisionX = collisionLayer.getCell((int)((getX() + getWidth())/tileWidth), (int)((getY() + getHeight())/tileHeight)).getTile().getProperties().containsKey("bloqueado");


            //meio direita
            if(!collisionX)
            collisionX = collisionLayer.getCell((int)((getX() + getWidth())/tileWidth), (int)((getY() + getHeight()/2)/tileHeight)).getTile().getProperties().containsKey("bloqueado");


            //abaixo direita
            if(!collisionX)
            collisionX = collisionLayer.getCell((int)((getX() + getWidth())/tileWidth), (int)(getY()/tileHeight)).getTile().getProperties().containsKey("bloqueado");


        }
        //se colidir!!!
        if(collisionX){
            System.out.println("colidiu!!!");
            setX(oldX);
            velocity.x = 0;
        }
        setY(getY() + velocity.y * delta);

        if(velocity.y< 0){

            //abaixo esquerda

            collisionY = collisionLayer.getCell((int)(getX()/tileWidth), (int)(getY()/tileHeight)).getTile().getProperties().containsKey("bloqueado");

            if(collisionY)
                System.out.println ("colidiu abaixo esquerda");

            //abaixo meio

            if(!collisionY){
            collisionY = collisionLayer.getCell((int)((getX() + getWidth()/2)/tileWidth), (int)(getY()/tileHeight)).getTile().getProperties().containsKey("bloqueado");
            if(collisionY)
                System.out.println ("colidiu abaixo meio");
            }


            //abaixo direita
            if(!collisionY){
            collisionY = collisionLayer.getCell((int)((getX() + getWidth())/tileWidth), (int)(getY()/tileHeight)).getTile().getProperties().containsKey("bloqueado");
            if(collisionY)
                System.out.println ("colidiu abaixo direita");
            }


        }else if(velocity.y>0){

            //acima esquerda

            collisionY = collisionLayer.getCell((int)(getX()/tileWidth), (int)((getY() + getHeight())/tileHeight)).getTile().getProperties().containsKey("bloqueado");
            if(collisionY)
                System.out.println ("colidiu acima esquerda");


            //acima meio
            if(!collisionY){
            collisionY = collisionLayer.getCell((int)((getX()+ getWidth()/2)/tileWidth), (int)((getY()+ getHeight())/tileHeight)).getTile().getProperties().containsKey("bloqueado");
            if(collisionY)
                System.out.println ("colidiu acima meio");
            }
            //acima direita
            if(!collisionY){
            collisionY = collisionLayer.getCell((int)((getX()+getWidth())/tileWidth), (int)((getY()+getHeight())/tileHeight)).getTile().getProperties().containsKey("bloqueado");
            if(collisionY)
                System.out.println ("colidiu acima direita");
            }
        }
        //se colidir!!!
        if(collisionY){
            System.out.println("colidiu y!!!");
            setY(oldY);
            velocity.y=0;

        }



    }


    public Vector3 getvelocity() {
        return velocity;
    }

    public void setvelocity(Vector3 velocity) {
        this.velocity = velocity;
    }

    public float getspeed() {
        return speed;
    }

    public void setspeed(float speed) {
        this.speed = speed;
    }

    public float getgravity() {
        return gravity;
    }

    public void setgravity(float gravity) {
        this.gravity = gravity;
    }

    public TiledMapTileLayer getCollisionLayer() {
        return collisionLayer;
    }

    public void setCollisionLayer(TiledMapTileLayer collisionLayer) {
        this.collisionLayer = collisionLayer;
    }

    @Override
    public boolean keyDown(int keycode) {
        switch(keycode) {
        case Keys.W:

            break;
        case Keys.A:
            velocity.x = -speed;

            break;
        case Keys.D:
            velocity.x = speed;

        }
        return true;
    }
    @Override
    public boolean keyUp(int keycode) {
        // TODO Auto-generated method stub
        switch (keycode){
        case Keys.A:

        case Keys.D:
            velocity.x = 0;

        }

        return true;
    }

    @Override
    public boolean keyTyped(char character) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {

        //collisionLayer -> coordinates for TiledMap
        //touch -> is a Vector3 for coordinates of screen
        touch.set((screenX/collisionLayer.getTileWidth()),(screenY/collisionLayer.getTileHeight()),0);

        setX((int) (touch.x / (collisionLayer.getTileWidth()))*collisionLayer.getTileWidth());
        setY((int) (touch.y /(collisionLayer.getTileHeight()))*collisionLayer.getTileWidth());


        return true;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    //  animationTime = 0;

        return false;
    }
    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        // TODO Auto-generated method stub


        return false;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        // TODO Auto-generated method stub
        return false;
    }




}

0 个答案:

没有答案