LibGdx - 具有不同类型掉落物的简单游戏
LibGdx -simple game with different type of drops
希望大家都知道这个Libgdx游戏的基础link:
https://github.com/libgdx/libgdx/wiki/A-simple-game
在简单的游戏中,雨滴从顶部随机落下,是一种类型。
在我的例子中,我想实现同样的事情,因为 drops.The 不同之处在于,有四种类型 drops.When 它与另一个物体碰撞,四种应该表现出四种不同的特征。
简而言之,我用它的对象和数组创建了一个 Drop class,并且也可以生成 drop 对象。
Drop drop=new Drop();
private Array<Drop> drops=new Array<Drop>();
但是如果我想在这种情况下为掉落物定义单独的属性怎么办?
我希望将放置对象分为四种类型。例如有不同颜色的黄色、红色、绿色和 blue.Also 四种水滴应该从顶部随机落下。
我应该以何种方式,以及如何实现这样的概念?
如果我对此有所了解,那将非常有帮助。
编辑:
我有一个抽象 class Drop and .I am not passing texture and rectangle in the constructor.Because graphics is not available for the project right now.I am planning to assign it in后来 stage.As 现在,我用 shaperenderer 做的一切。
我的 objectFactory class 中有用于创建所有对象的三个不同颜色滴的 create() 方法:
public YellowDrop createYellow(){
YellowDrop yellow = new YellowDrop();
yellow.setSize(100f,100f);
yellow.setPosition(MathUtils.random(0, 800),1280);
return yellow;
}
public RedDrop createRed(){
RedDrop red = new RedDrop();
red.setSize(100f,100f);
red.setPosition(MathUtils.random(0, 800),1280);
return red;
}
public GreenDrop createGreen(){
GreenDrop green = new GreenDrop();
green.setSize(100f,100f);
green.setPosition(MathUtils.random(0, 800),1280);
return green;
}
我写了这样的 createRandomDrop() 代码(对我来说,你的代码令人困惑 though.How 我可以在方法中使用那个冒号吗?我从来没有用过它。)
私有块 createRandomDrop() {
开关 (MathUtils.random(0, 3)) {
案例 0:
System.out.println("000000");
return objectFactory.createYellow();
情况1 :
return objectFactory.createGreen();
case 2 : return objectFactory.createRed();
default:
return objectFactory.createGreen();
}
}
在这里,我对如何 return 单个 drop 对象感到困惑。
尽管我是这样写调用的:
if(TimeUtils.nanoTime() - lastDropTime > 1000000000) {
drops.add(createRandomDrop());
}
private void updateBlocks(float delta) {
Iterator<Drop> iter = drops.iterator();
while(iter.hasNext()) {
Drop b = iter.next();
b.update(delta);//moving drop from top to bottom in update()
if(b.getY()<0) iter.remove();
}
// System.out.println(drops.size);
}
最后我正在为所有数组元素绘制 shaperenderer:
for (Drop b : drops) {
b.drawDebug(shapeRenderer);}
这个 drawdebug() 在 Drop class 中,我正在覆盖它
public void drawDebug(ShapeRenderer shapeRenderer) {
shapeRenderer.setColor(Color.YELLOW);
shapeRenderer.rect(collisionRectangle.x, collisionRectangle.y,
collisionRectangle.width,
collisionRectangle.height);
}
问题是,创建了太多掉落,overlapping.May 开关盒造成了问题。
你有什么问题?这很简单。
创建 4 children of Drop class。 yellowDrop、greenDrop 等...并在 Drop class 中创建 collide() 方法,它将为所有 children 实现相同的功能。
像这样:
abstract class Drop {
abstract Texture tex;
abstract Rectangle rect;
Drop(Texture tex, Rectangle rect){
// initialize values here
}
void showCollideAnimation(){ // example. here you can create your own void to show different characteristics.
tex.blabla()
}
void collide(){
showCollideAnimation();
}
// also draw, update methods etc... Hope you got that they should do functionality same for all children
}
然后创建 child 将覆盖 showCollideAnimation 方法。
class GreenDrop extends Drop {
@override
void showCollideAnimation(){
.... // here you pass effect for Green child
}
}
要选择随机 child 你可以创建 returns Drop
的函数
Drop createRandomDrop(Rectangle rect): Drop {
switch (MathUtils.random(0, 3)) {
case 0 : return new YellowDrop(Texture("pathToYellowTexture"), rect) break;
case 1 : return new GreenDrop(Texture("pathToGreenTexture"), rect) break;
// etc.
}
}
现在您可以填充 ArrayList of Drops。
private ArrayList<Drop> drops = new ArrayList<Drop>();
in render method:
if(TimeUtils.nanoTime() - lastDropTime > 1000000000) {
drops.add(createRandomDrop(Texture(""), Rectangle())); // specify texture and rectangle yourself
}
希望大家都知道这个Libgdx游戏的基础link:
https://github.com/libgdx/libgdx/wiki/A-simple-game
在简单的游戏中,雨滴从顶部随机落下,是一种类型。 在我的例子中,我想实现同样的事情,因为 drops.The 不同之处在于,有四种类型 drops.When 它与另一个物体碰撞,四种应该表现出四种不同的特征。
简而言之,我用它的对象和数组创建了一个 Drop class,并且也可以生成 drop 对象。
Drop drop=new Drop();
private Array<Drop> drops=new Array<Drop>();
但是如果我想在这种情况下为掉落物定义单独的属性怎么办? 我希望将放置对象分为四种类型。例如有不同颜色的黄色、红色、绿色和 blue.Also 四种水滴应该从顶部随机落下。
我应该以何种方式,以及如何实现这样的概念?
如果我对此有所了解,那将非常有帮助。
编辑:
我有一个抽象 class Drop and .I am not passing texture and rectangle in the constructor.Because graphics is not available for the project right now.I am planning to assign it in后来 stage.As 现在,我用 shaperenderer 做的一切。
我的 objectFactory class 中有用于创建所有对象的三个不同颜色滴的 create() 方法:
public YellowDrop createYellow(){
YellowDrop yellow = new YellowDrop();
yellow.setSize(100f,100f);
yellow.setPosition(MathUtils.random(0, 800),1280);
return yellow;
}
public RedDrop createRed(){
RedDrop red = new RedDrop();
red.setSize(100f,100f);
red.setPosition(MathUtils.random(0, 800),1280);
return red;
}
public GreenDrop createGreen(){
GreenDrop green = new GreenDrop();
green.setSize(100f,100f);
green.setPosition(MathUtils.random(0, 800),1280);
return green;
}
我写了这样的 createRandomDrop() 代码(对我来说,你的代码令人困惑 though.How 我可以在方法中使用那个冒号吗?我从来没有用过它。)
私有块 createRandomDrop() { 开关 (MathUtils.random(0, 3)) { 案例 0: System.out.println("000000"); return objectFactory.createYellow(); 情况1 : return objectFactory.createGreen();
case 2 : return objectFactory.createRed();
default:
return objectFactory.createGreen();
} } 在这里,我对如何 return 单个 drop 对象感到困惑。 尽管我是这样写调用的:
if(TimeUtils.nanoTime() - lastDropTime > 1000000000) {
drops.add(createRandomDrop());
}
private void updateBlocks(float delta) {
Iterator<Drop> iter = drops.iterator();
while(iter.hasNext()) {
Drop b = iter.next();
b.update(delta);//moving drop from top to bottom in update()
if(b.getY()<0) iter.remove();
}
// System.out.println(drops.size);
}
最后我正在为所有数组元素绘制 shaperenderer:
for (Drop b : drops) {
b.drawDebug(shapeRenderer);}
这个 drawdebug() 在 Drop class 中,我正在覆盖它
public void drawDebug(ShapeRenderer shapeRenderer) {
shapeRenderer.setColor(Color.YELLOW);
shapeRenderer.rect(collisionRectangle.x, collisionRectangle.y,
collisionRectangle.width,
collisionRectangle.height);
}
问题是,创建了太多掉落,overlapping.May 开关盒造成了问题。
你有什么问题?这很简单。
创建 4 children of Drop class。 yellowDrop、greenDrop 等...并在 Drop class 中创建 collide() 方法,它将为所有 children 实现相同的功能。
像这样:
abstract class Drop {
abstract Texture tex;
abstract Rectangle rect;
Drop(Texture tex, Rectangle rect){
// initialize values here
}
void showCollideAnimation(){ // example. here you can create your own void to show different characteristics.
tex.blabla()
}
void collide(){
showCollideAnimation();
}
// also draw, update methods etc... Hope you got that they should do functionality same for all children
}
然后创建 child 将覆盖 showCollideAnimation 方法。
class GreenDrop extends Drop {
@override
void showCollideAnimation(){
.... // here you pass effect for Green child
}
}
要选择随机 child 你可以创建 returns Drop
的函数Drop createRandomDrop(Rectangle rect): Drop {
switch (MathUtils.random(0, 3)) {
case 0 : return new YellowDrop(Texture("pathToYellowTexture"), rect) break;
case 1 : return new GreenDrop(Texture("pathToGreenTexture"), rect) break;
// etc.
}
}
现在您可以填充 ArrayList of Drops。
private ArrayList<Drop> drops = new ArrayList<Drop>();
in render method:
if(TimeUtils.nanoTime() - lastDropTime > 1000000000) {
drops.add(createRandomDrop(Texture(""), Rectangle())); // specify texture and rectangle yourself
}