मैं libgdx में नया हूं और मुझे अपने ऐरे में एक आयताकार ("दो") जोड़ने की समस्या है। मैं इसे जोड़ने और इसे प्राप्त करने के बाद टक्कर का पता नहीं लगा सकता। मेरा कोड बोले है:
...
public class MyGdxGame implements ApplicationListener
{
Texture texture;
SpriteBatch batch;
Rectangle one, two;
float x1=0,x2;
float y1, y2;
Array <Rectangle> array;
बनाएँ () में:
texture = new Texture(Gdx.files.internal("android.jpg"));
batch = new SpriteBatch();
x2 = Gdx.graphics.getWidth()-40;
y1 = y2 = (Gdx.graphics.getHeight()/2)-15;
one = new Rectangle();
two = new Rectangle();
one.set(x1, y1, 40, 30);
two.set(x2, y2, 40, 30);
array = new Array <Rectangle>();
array.add(two);
रेंडरर () में:
...
batch.begin();
batch.draw(texture, x1, y1, 40, 30);
batch.draw(texture, x2, y2, 40, 30);
try
{
Thread.sleep(10);
x1 += 2;
x2 -= 2;
one.set(x1, y1, 40, 30);
two.set(x2, y2, 40, 30);
यहां समस्या है, "कारण" एक "आयताकार" दो "आयताकार के साथ टकराव का पता नहीं लगाता है:
if(one.overlaps(array.get(1)))
{
x1 = 0;
x2 = Gdx.graphics.getWidth()-40;
}
}
catch(Exception e){}
batch.end();
क्या कोई कृपया मेरी सहायता कर सकता है?
उत्तर:
जवाब के लिए 0 № 1आप अपनी सरणी में केवल 1 तत्व जोड़ रहे हैं।
array.add(two);
इस ऑब्जेक्ट को पाने के लिए आपको इसका उपयोग करना चाहिए: array.get(0);
के बजाय array.get(1);
सबसे पहले मैं उपयोग करने का सुझाव दूंगा सूची, और जैसा कि आप शायद उनके साथ टकराव की जांच के लिए कई आयताकार रखना चाहते हैं:
List<Rectangle> myList = new ArrayList<>();
myList.add(two);
for(Rectangle rect : myList) {
if(one.overlaps(rect)) {
//...
}
}