对象数组定位

编程入门 行业动态 更新时间:2024-10-27 23:22:08
对象数组定位-LibGdx(object array positioning-LibGdx)

在我的游戏中,如果我触摸特定物体,硬币物体将以随机速度从它们中出来并占据随机位置。

public void update(delta){ if(isTouched()&& getY()<Constants.WORLD_HEIGHT/2){ setY(getY()+(randomSpeed * delta)); setX(getX()-(randomSpeed/4 * delta)); } }

现在我想让这些硬币占据一些模式的位置。如果3个硬币出来,三角形图案或4个硬币,像那样的矩形图案。

我试图让它工作,但硬币出来并移动,但相互重叠。不能创建任何模式。 模式如:

这就是我试过的

int a = Math.abs(rndNo.nextInt() % 3)+1;//no of coins int no =0; float coinxPos = player.getX()-coins[0].getWidth()/2; float coinyPos = player.getY(); int minCoinGap=20; switch (a) { case 1: for (int i = 0; i < coins.length; i++) { if (!coins[i].isCoinVisible() && no < a) { coins[i].setCoinVisible(true); coinxPos = coinxPos+rndNo.nextInt()%70; coinyPos = coinyPos+rndNo.nextInt()%70; coins[i].setPosition(coinxPos, coinyPos); no++; } } break; case 2: for (int i = 0; i < coins.length; i++) { if (!coins[i].isCoinVisible() && no < a) { coins[i].setCoinVisible(true); coinxPos = coinxPos+minCoinGap+rndNo.nextInt()%70; coinyPos = coinyPos+rndNo.nextInt()%150; coins[i].setPosition(coinxPos, coinyPos); no++; } } break: ...... ...... default: break;

可能这是一个简单的逻辑实现,但我浪费了很多时间,并对如何使其工作感到困惑。 任何帮助,将不胜感激。

In my game,if I touch a particular object,coin objects will come out of them at random speeds and occupy random positions.

public void update(delta){ if(isTouched()&& getY()<Constants.WORLD_HEIGHT/2){ setY(getY()+(randomSpeed * delta)); setX(getX()-(randomSpeed/4 * delta)); } }

Now I want to make this coins occupy positions in some patterns.Like if 3 coins come out,a triangle pattern or if 4 coins, rectangular pattern like that.

I tried to make it work,but coins are coming out and moved,but overlapping each other.Not able to create any patterns. patterns like:

This is what I tried

int a = Math.abs(rndNo.nextInt() % 3)+1;//no of coins int no =0; float coinxPos = player.getX()-coins[0].getWidth()/2; float coinyPos = player.getY(); int minCoinGap=20; switch (a) { case 1: for (int i = 0; i < coins.length; i++) { if (!coins[i].isCoinVisible() && no < a) { coins[i].setCoinVisible(true); coinxPos = coinxPos+rndNo.nextInt()%70; coinyPos = coinyPos+rndNo.nextInt()%70; coins[i].setPosition(coinxPos, coinyPos); no++; } } break; case 2: for (int i = 0; i < coins.length; i++) { if (!coins[i].isCoinVisible() && no < a) { coins[i].setCoinVisible(true); coinxPos = coinxPos+minCoinGap+rndNo.nextInt()%70; coinyPos = coinyPos+rndNo.nextInt()%150; coins[i].setPosition(coinxPos, coinyPos); no++; } } break: ...... ...... default: break;

may be this is a simple logic to implement,but I wasted a lot of time on it and got confused of how to make it work. Any help would be appreciated.

最满意答案

在我的游戏中,当我想要一些物体在X时,Y到达一些特定的坐标Xe,Ye在每一帧我都加入它的当前和想要位置之间的坐标差,除以常数并乘以从最后一帧传递的时间。 这样它开始快速移动,慢慢地走,因为它越来越近,看起来有点酷。

X += ((Xe - X)* dt)/ CONST; Y += ((Ye - Y)* dt)/ CONST;

你将通过实验获得CONST值,更大的值意味着更慢的移动。 如果您希望它看起来更冷,您可以添加速度变量,而不是根据距离终点位置的距离直接更改坐标,您可以调整该速度。 这样,即使物体在某个点到达终点位置,它仍将具有一定的速度并且它将继续移动 - 它将具有惯性。 要实现起来有点复杂,但运动会更加激烈。

如果你想要那个Xe,你是一个特定的位置(不是随机的),那么只需设置那些常量值。 不需要让它更复杂。 像另一个constat OFFSET一样设置:

static final int OFFSET = 100; Xe1 = X - OFFSET; // for first coin Ye1 = Y - OFFSET; Xe2 = X + OFFSET; // for second coin Ye2 = Y - OFFSET; ...

In my game, when I want some object at X,Y to reach some specific coordinates Xe,Ye at every frame I'm adding to it's coordinates difference between current and wanted position, divided by constant and multiplied by time passed from last frame. That way it starts moving quickly and goes slowly and slowly as it's closer, looks kinda cool.

X += ((Xe - X)* dt)/ CONST; Y += ((Ye - Y)* dt)/ CONST;

You'll experimentally get that CONST value, bigger value means slower movement. If you want it to look even cooler you can add velocity variable and instead of changing directly coordinates depending on distance from end position you can adjust that velocity. That way even if object at some point reaches the end position it will still have some velocity and it will keep moving - it will have inertia. A bit more complex to achieve, but movement would be even wilder.

And if you want that Xe,Ye be some specific position (not random), then just set those constant values. No need to make it more complicated then that. Set like another constat OFFSET:

static final int OFFSET = 100; Xe1 = X - OFFSET; // for first coin Ye1 = Y - OFFSET; Xe2 = X + OFFSET; // for second coin Ye2 = Y - OFFSET; ...

更多推荐

本文发布于:2023-08-03 07:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1385217.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   对象

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!