Java 如何在更新向量中解除精灵绑定?
如何将更新向量解除绑定到精灵?我正在使用Libgdx框架。这是我的密码Java 如何在更新向量中解除精灵绑定?,java,sprite,libgdx,Java,Sprite,Libgdx,如何将更新向量解除绑定到精灵?我正在使用Libgdx框架。这是我的密码 public class VectorSample extends GDX_TEST implements InputProcessor { Texture ball,bullet,guider; OrthographicCamera camera; Vector2 vector = new Vector2(); Vector2 vectoralt = new Vector2();
public class VectorSample extends GDX_TEST implements InputProcessor {
Texture ball,bullet,guider;
OrthographicCamera camera;
Vector2 vector = new Vector2();
Vector2 vectoralt = new Vector2();
long lastDropTime;
SpriteBatch batch;
Rectangle rect = new Rectangle();
float angle = 0,anglealt = 0;
private ShapeRenderer renderer;
TextureRegion tr;
Sprite sprite,sprite2;
ShapeRenderer sr;
Ship ship;
BallShit shit;
Vector2 nvec;
protected Vector2 center = new Vector2();
Array<Vector2> bullets;
Array<Rectangle> bulletsRect;
Boolean fire = false;
Rectangle tmpRect = new Rectangle();
@Override
public void create() {
renderer = new ShapeRenderer();
ball = new Texture(Gdx.files.internal("data/player.png"));
bullet = new Texture("data/ball_black.png");
tr = new TextureRegion(ball);
camera = new OrthographicCamera();
camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
ship = new Ship(new Vector2(5, 50), 1, 1, 0, 5f);
sr = new ShapeRenderer();
batch = new SpriteBatch();
Gdx.input.setInputProcessor(this);
rect.width = ball.getWidth();
rect.height = ball.getHeight();
vector.add(200,200);
sprite = new Sprite(ball);
vectoralt.add(200 + sprite.getWidth(),200);
shit = new BallShit(new Vector2(10,0),50f,50f);
bullets = new Array<Vector2>();
getCenter();
bulletsRect = new Array<Rectangle>();
fireatwill2();
}
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
{
batch.draw(sprite,vector.x,vector.y,sprite.getWidth()/2,sprite.getHeight()/2,sprite.getWidth(),sprite.getHeight(),1,1,angle);
if(fire == true){
for(Rectangle raindrop: bulletsRect) {
batch.draw(bullet,raindrop.x,raindrop.y);
}
}
}
batch.end();
if(Gdx.input.isKeyPressed(Input.Keys.D)){
vector.add(1,0);
if(vector.x > Gdx.graphics.getWidth()){
Gdx.app.log("x","x");
}
}
if(Gdx.input.isKeyPressed(Input.Keys.A)){
vector.add(-1,0);
if(vector.x < 0){
Gdx.app.log("x","-x");
}
}
float w = camera.frustum.planePoints[1].x - camera.frustum.planePoints[0].x;
float distance = w - (vector.x + sprite.getWidth());
if(distance <=0){
vector.x = w - sprite.getWidth();
}
Gdx.app.log("camera x" , " " + distance);
if(distance >= Gdx.graphics.getWidth() - sprite.getWidth()) {
vector.x = 0;
}
if(Gdx.input.isButtonPressed(Input.Buttons.LEFT)){
fire = true;
if(TimeUtils.nanoTime() - lastDropTime > 100000000) fireatwill2();
}
Iterator<Rectangle> iter = bulletsRect.iterator();
while(iter.hasNext()) {
Rectangle raindrop = iter.next();
double angletry = getAngle() * MathUtils.degreesToRadians;
float speed = 5;
float scale_x = (float)Math.cos(angletry);
float scale_y = (float)Math.sin(angletry);
float velocity_x = (speed* scale_x);
float velocity_y = (speed* scale_y);
raindrop.x += velocity_x;
raindrop.y += velocity_y;
if(raindrop.y < 0) iter.remove();
if(raindrop.y > Gdx.graphics.getHeight() || raindrop.x > Gdx.graphics.getWidth()) iter.remove();
}
//getting the angle
float angle = findAngle(Gdx.input.getX(),
Gdx.graphics.getHeight() - Gdx.input.getY());
this.angle = angle % 360;
}
private float getAngle(){
float angle = findAngle(Gdx.input.getX(),
Gdx.graphics.getHeight() - Gdx.input.getY());
return angle;
}
private void fireatwill2() {
Rectangle raindrop = new Rectangle();
raindrop.x = vector.x + sprite.getWidth() / 2 - (bullet.getWidth() / 2);
raindrop.y = vector.y + sprite.getHeight() / 2 - (bullet.getHeight() / 2);
bulletsRect.add(raindrop);
lastDropTime = TimeUtils.nanoTime();
}
public Vector2 getCenter() {
center.x = vector.x + sprite.getWidth() / 2;
center.y = vector.y + sprite.getHeight() / 2;
return center.cpy();
}
@Override
public void pause() {
super.pause(); //To change body of overridden methods use File | Settings | File Templates.
}
@Override
public void resume() {
super.resume(); //To change body of overridden methods use File | Settings | File Templates.
}
public float findAngle(float x1, float y1) {
Vector2 center = getCenter();
float x0 = center.x;
float y0 = center.y;
float a = MathUtils.atan2(y1 - y0, x1 - x0);
return a * MathUtils.radiansToDegrees;
}
}
公共类VectorSample扩展GDX_测试实现InputProcessor{
纹理球、子弹、导向器;
正交摄影机;
Vector2向量=新向量2();
Vector2 vectoralt=新Vector2();
持续时间长;
喷雾批;
矩形rect=新矩形();
浮动角度=0,角度Alt=0;
私有shaperender渲染器;
纹理区域tr;
雪碧雪碧,雪碧2;
Shaperender sr;
船舶;
狗屎;
向量2-nvec;
受保护向量2中心=新向量2();
阵列子弹;
阵列弹;
布尔火=假;
矩形tmpRect=新矩形();
@凌驾
公共void create(){
渲染器=新的ShaperEnder();
ball=新纹理(Gdx.files.internal(“data/player.png”);
bullet=新纹理(“data/ball_black.png”);
tr=新纹理区域(球);
摄影机=新的正交摄影机();
setToOrTo(false,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
ship=新船(新矢量2(5,50),1,1,0,5f);
sr=新的ShaperEnder();
批次=新的SpriteBatch();
Gdx.input.setInputProcessor(此);
rect.width=ball.getWidth();
rect.height=ball.getHeight();
vector.add(200200);
雪碧=新雪碧(球);
添加(200+sprite.getWidth(),200);
狗屎=新的狗屎(新矢量2(10,0),50f,50f);
项目符号=新数组();
getCenter();
bulletsRect=新数组();
fireatwill2();
}
@凌驾
公共无效呈现(){
Gdx.gl.glClearColor(1,1,1,1);
Gdx.gl.glClear(GL10.gl\u颜色\u缓冲\u位);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
{
batch.draw(sprite,vector.x,vector.y,sprite.getWidth()/2,sprite.getHeight()/2,sprite.getWidth(),sprite.getHeight(),1,1,angle);
if(fire==true){
用于(矩形雨滴:bulletsRect){
批量绘制(子弹,雨滴.x,雨滴.y);
}
}
}
batch.end();
如果(Gdx.input.isKeyPressed(input.Keys.D)){
向量加(1,0);
if(vector.x>Gdx.graphics.getWidth()){
Gdx.app.log(“x”、“x”);
}
}
如果(Gdx.input.isKeyPressed(input.Keys.A)){
向量相加(-1,0);
if(向量x<0){
Gdx.app.log(“x”、“-x”);
}
}
float w=camera.frustum.planePoints[1].x-camera.frustum.planePoints[0].x;
浮动距离=w-(vector.x+sprite.getWidth());
if(distance=Gdx.graphics.getWidth()-sprite.getWidth()){
向量x=0;
}
if(Gdx.input.isButtonPressed(input.Buttons.LEFT)){
火=真;
如果(TimeUtils.nanoTime()-lastDropTime>100000000)fireatwill2();
}
迭代器iter=bulletsRect.Iterator();
while(iter.hasNext()){
矩形雨滴=iter.next();
double angletry=getAngle()*MathUtils.degreesToRadians;
浮动速度=5;
float scale_x=(float)Math.cos(angletry);
float scale_y=(float)Math.sin(angletry);
浮动速度_x=(速度*刻度_x);
浮动速度_y=(速度*刻度_y);
雨滴.x+=速度_x;
雨滴.y+=速度_y;
如果(雨滴y<0)iter.remove();
如果(raindrop.y>Gdx.graphics.getHeight()| | raindrop.x>Gdx.graphics.getWidth())iter.remove();
}
//获取角度
float angle=findAngle(Gdx.input.getX(),
Gdx.graphics.getHeight()-Gdx.input.getY());
该角度=角度%360;
}
私有浮点getAngle(){
float angle=findAngle(Gdx.input.getX(),
Gdx.graphics.getHeight()-Gdx.input.getY());
返回角;
}
私有无效fireatwill2(){
矩形雨滴=新矩形();
raindrop.x=vector.x+sprite.getWidth()/2-(bullet.getWidth()/2);
raindrop.y=vector.y+sprite.getHeight()/2-(bullet.getHeight()/2);
项目清单补充(雨滴);
lastDropTime=TimeUtils.nanoTime();
}
公共向量2 getCenter(){
center.x=vector.x+sprite.getWidth()/2;
center.y=vector.y+sprite.getHeight()/2;
returncenter.cpy();
}
@凌驾
公共空间暂停(){
super.pause();//要更改重写方法的主体,请使用文件|设置|文件模板。
}
@凌驾
公众简历(){
super.resume();//要更改重写方法的主体,请使用文件|设置|文件模板。
}
公共浮点数悬挂(浮点数x1、浮点数y1){
Vector2-center=getCenter();
浮点数x0=中心点x;
浮动y0=中心y;
浮点数a=MathUtils.atan2(y1-y0,x1-x0);
返回a*MathUtils.radiansToDegrees;
}
}
这里是运行和源代码
我不能很好地解释它,但如果你能运行演示你会得到我说的
我真的被困在这里了。。谢谢。您的例子不适用于我,先生。您在执行jar文件之前提取了它吗?没有。在开始时,我刚刚得到一个错误
找不到主类:com.example.DesktopStart。程序将首先退出
提取它。你说你不会,这就是为什么…哦,我不明白。当然,我以前提取过。否则我就不能把它当作罐子运行。。。