Libgdx阵列如何获得最后一个项目? [英] Libgdx Array<> how to get last item?

查看:66
本文介绍了Libgdx阵列如何获得最后一个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当相机的Y位置高于上次创建的存储桶"时,我想更改存储桶"的Y位置Y位置:"create()"方法中有我的代码

I want to change Y position of "bucket", when camera Y position is higher than "last created bucket" Y position: There is my code in "create()" method

buckets = new Array<Bucket>();
for(int i=1;i<BUCKET_COUNT;i++){
    buckets.add(new Bucket(world,(rand.nextInt((int)W)-45)/PPM,BUCKET_MARGIN*i/PPM));
}

我正在"render()"方法中这样做:

And I am doing like this in "render()" method:

for(Bucket bucket : buckets){
    if(cam.position.y > buckets.peek().getBody().getPosition().y){
        bucket.repos(rand.nextInt((int)W)/PPM,buckets.size+BUCKET_MARGIN/PPM);
    }
}

repos():

public void repos(float x, float y){
    setPosition(x, y);//Bucket class is extends Sprite
}

但是它不起作用,我也不知道如何解决这个问题

But it doesn't work and I don't know how to solve this problem

我想知道如何从那些数组中获得 last 个项目?

I want to know how to get last item from those array?

推荐答案

您应该尝试一下

if(cam.position.y > buckets.peek().getPosition().y){
     for(Bucket bucket : buckets){
        bucket.repos(rand.nextInt((int)W)/PPM,buckets.size+BUCKET_MARGIN/PPM);
    }
}

在此y位置中,未正确指定存储桶,因为根据您的代码,所有存储桶都设置为特定的y位置.

In this y position of buckets are not properly specified because according to your code all bucket set to particular y position.

编辑

if(cam.position.y > buckets.peek().getPosition().y){
    for(Bucket bucket : buckets){
            bucket.repos(rand.nextInt((int)W)/PPM,cam.position.y+bucket.getPositon().y);
    }
}

编辑2

看看这个教程,我认为这可能对您有所帮助.

Take a look of this tutorial, I think it may be helpful for you.

这篇关于Libgdx阵列如何获得最后一个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆