为游戏随机生成精灵?
我正在使用我创建的方法来随机创建不同的精灵,并使用 andEngine 将其添加到我的场景中。
Random randItem = new Random();
face = null;
float x = rand.nextInt((int) mCamera.getWidth() - mBallTextureRegion.getHeight());
int ItemNumber = randFruit.nextInt(6) + 1;
if(ItemNumber == 1){
face = new Sprite(x,0, this.mBallTextureRegion);
}else if(ItemNumber == 2){
face = new Sprite(x,0,this.strawberryTextureRegion);
}else if(ItemNumber == 3){
face = new Sprite(x,0,this.grapeTextureRegion);
}else if(ItemNumber == 4){
face = new Sprite(x,0, this.watermelonRegion);
}else if(ItemNumber == 5){
face = new Sprite(x,0, this.cherryRegion);
}else if(ItemNumber == 6){
Sprite poison = new Sprite(x,0,this.poisionRegion);
}
我正在做的是随机生成数字 1 - 6。并且每次测试以查看生成了哪个数字,然后根据 number 添加正确的精灵。
所以问题是..因为这是一个使用 AndEngine 的游戏项目。我想随机生成奖励项目,这些奖励项目是随机生成的,但生成频率不如上面的精灵那么频繁。这种情况应该每隔添加 100 个精灵才会发生一次。
有更好的方法来做这件事吗?就像专门为此目的创建的类一样?
或者我走在正确的轨道上吗?任何帮助将不胜感激!
I am using this method i created to randomly create different sprites to be added to my scene using andEngine.
Random randItem = new Random();
face = null;
float x = rand.nextInt((int) mCamera.getWidth() - mBallTextureRegion.getHeight());
int ItemNumber = randFruit.nextInt(6) + 1;
if(ItemNumber == 1){
face = new Sprite(x,0, this.mBallTextureRegion);
}else if(ItemNumber == 2){
face = new Sprite(x,0,this.strawberryTextureRegion);
}else if(ItemNumber == 3){
face = new Sprite(x,0,this.grapeTextureRegion);
}else if(ItemNumber == 4){
face = new Sprite(x,0, this.watermelonRegion);
}else if(ItemNumber == 5){
face = new Sprite(x,0, this.cherryRegion);
}else if(ItemNumber == 6){
Sprite poison = new Sprite(x,0,this.poisionRegion);
}
What i am doing is randomly generating numbers 1 - 6. And testing each time to see which number is generated and then i add the correct sprite according to the numbER.
So the problem is..Since this is for a game project using AndEngine. I want to randomly generate bonus items that are randomly generated but not generated as often as the sprites above. This should only happen in like once every few 100 sprites that are added.
Is there a better way to go about doing this? Like a class created just for this purpose?
Or am i on the right track? Any help will be greatly apprecciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能应该使用
switch
语句而不是嵌套的if-statements
来检查您想要的项目。如果您愿意,您也可以实现每隔
x
百分比丢弃额外随机生成的物品的机会,而不是在其上放置一个硬计数器(例如每 100 个),请考虑如下所示:You should probably use a
switch
statement rather than nestedif-statements
to check which item you want.You can implement a chance to drop additional random generated items every
x
percentage if you want aswell, rather than putting a hard-counter on it (e.g. every 100), consider something like this: