我需要你的一点帮助。我有一个简单的Android游戏,其中障碍是矩形,但我想增加半径这些矩形,所以游戏将是一个“平滑”的外观。
这是我的密码:
Rectangle.java
package com.mygame.mygame;
import com.badlogic.gdx.graphics.g2d.Sprite;
public class Rectangle {
public Sprite sp;
public Rectangle(float x, float width){
sp = new Sprite(GeneralStorage.pixel);
sp.setSize(GeneralStorage.w * width, GeneralStorage.height_obstacles);
sp.setPosition(GeneralStorage.w * x, GeneralStorage.h);
sp.setColor(GeneralStorage.color_bars);
}
public void move(){
sp.setPosition(sp.getX(), sp.getY() - RunningUpdate.pixels_fall);
}
public void draw(){
sp.draw(GeneralStorage.batch);
}
}
Obstacle.java
package com.mygame.mygame;
import com.badlogic.gdx.math.MathUtils;
import java.util.ArrayList;
public class Obstacle {
public ArrayList<com.mygame.mygame.Rectangle> rectangles;
public boolean moving = false;
public Obstacle(){
initilize_rectangles();
}
public void initilize_rectangles(){
rectangles = new ArrayList<com.mygame.mygame.Rectangle>();
moving = false;
switch(MathUtils.random(1,7)){
case 1:
rectangles.add(new com.mygame.mygame.Rectangle(0.2f, 0.6f));
break;
case 2:
rectangles.add(new com.mygame.mygame.Rectangle(0.1f, 0.3f));
rectangles.add(new com.mygame.mygame.Rectangle(0.6f, 0.3f));
break;
case 3:
rectangles.add(new com.mygame.mygame.Rectangle(0.0f, 0.35f));
rectangles.add(new com.mygame.mygame.Rectangle(0.65f, 0.35f));
break;
case 4:
rectangles.add(new com.mygame.mygame.Rectangle(0.3f, 0.4f));
break;
case 5:
rectangles.add(new com.mygame.mygame.Rectangle(0, 0.4f));
break;
case 6:
rectangles.add(new com.mygame.mygame.Rectangle(0.6f, 0.4f));
break;
case 7:
rectangles.add(new com.mygame.mygame.Rectangle(0, 0.1f));
rectangles.add(new com.mygame.mygame.Rectangle(0.35f, 0.3f));
rectangles.add(new com.mygame.mygame.Rectangle(0.9f, 0.1f));
break;
}
}
public void move(){
if(moving) {
for (com.mygame.mygame.Rectangle rectangle : rectangles)
rectangle.move();
// Gdx.app.log("muevo", "muevo");
}
if(rectangles.get(0).sp.getY() <= - rectangles.get(0).sp.getHeight()){
moving = false;
initilize_rectangles();
}
}
public void change_possible(){
if(rectangles.get(0).sp.getY() + rectangles.get(0).sp.getHeight() < GeneralStorage.meatball.big_ball.sp.getY()){
if(com.mygame.mygame.RunningUpdate.possible_crash == GeneralStorage.obstacles.size() - 1)
com.mygame.mygame.RunningUpdate.possible_crash = 0;
else com.mygame.mygame.RunningUpdate.possible_crash++;
com.mygame.mygame.RunningUpdate.score++;
com.mygame.mygame.RunningUpdate.increase_difficulty();
}
}
public void draw(){
if(moving)
for(com.mygame.mygame.Rectangle rectangle: rectangles)
rectangle.draw();
}
}
我会非常感谢你的帮助!
发布于 2015-09-27 21:48:08
我认为Rami有你的解决方案,只是一个把它和你的矩形放在一起的问题。
所以我为你做了些调查。希望我能帮上忙。我不是想偷拉米的答案。我只是喜欢数学和游戏。
https://github.com/libgdx/libgdx/blob/fa21bbd665de1d25f326bc0f78acc862e6ca2a7b/gdx/src/com/badlogic/gdx/graphics/Pixmap.java有fillCircle
的定义
/** Fills a circle with the center at x,y and a radius using the current color.
*
* @param x The x-coordinate of the center
* @param y The y-coordinate of the center
* @param radius The radius in pixels */
public void fillCircle (int x, int y, int radius)
{
pixmap.fillCircle(x, y, radius, color);
}
和fillRectangle
/** Fills a rectangle starting at x, y extending by width to the right and
* by height downwards (y-axis points downwards) using the current color.
*
* @param x The x coordinate
* @param y The y coordinate
* @param width The width in pixels
* @param height The height in pixels */
public void fillRectangle (int x, int y, int width, int height)
{
pixmap.fillRect(x, y, width, height, color);
}
我认为您只需要调整他引用的代码,方法是添加(在调用中的x-坐标)要循环的矩形的实际x位置。不知道该拿Y做什么;希望你知道。
为了更好地理解,Rami引用的例子是:
public static Pixmap getPixmapRoundedRectangle(int width, int height, int radius, int color) {
pixmap = new Pixmap(width, height, Format.RGBA8888);
pixmap.setColor(color);
// pink rectangle
pixmap.fillRectangle(0 ,radius, pixmap.getWidth() , pixmap.getHeight()-2*radius);
// green rectangle
pixmap.fillRectangle(radius,0 , pixmap.getWidth() - 2*radius, pixmap.getHeight() );
// Bottom-left circle
pixmap.fillCircle(radius, radius , radius);
// Top-left circle
pixmap.fillCircle(radius , pixmap.getHeight()-radius, radius);
// Bottom-right circle
pixmap.fillCircle(pixmap.getWidth()-radius, radius , radius);
// Top-right circle
pixmap.fillCircle(pixmap.getWidth()-radius, pixmap.getHeight()-radius, radius);
return pixmap;
}
如果您不想涉及另一个库,那么这些想法可能会让您在创建自己的fillCircle
方面走上正轨。
https://stackoverflow.com/questions/32812278
复制相似问题