使精灵从边缘反弹
Making A Sprite Bounce Off The Edge
我试图让精灵在它小于 0 时从屏幕边缘反弹。现在它只是在屏幕上放大到空白处。这是我的代码注释,CentipedeBody 是一个扩展 Sprite 的 class。在 render
方法中,我调用了 ex.update();
,它是 class 的一个对象。然后在 batch.begin()
和 batch.end()
之间我有 batch.draw(ex,ex.getPosition().x,ex.getPosition().y,ex.getSize().x,ex.getSize().y);
public class CentipedeBody extends Sprite
{
public CentipedeBody(TextureRegion image,Vector2 position,Vector2 size) {
super(new TextureRegion(image));
this.position = position;
this.size=size;
bounds=new Rectangle(position.x,position.y,8,8);
left=true;
}
public void update() {
bounds.set(getPosition().x,getPosition().y,8,8);
if (left==true) {
position.x-=(.5f);
up=false;
down=false;
right=false;
left=true;
}
if (right==true) {
position.x+=.5f;
left=false;
right=true;
down=false;
up=false;
}
if (down==true) {
position.y-=(.5f);
right=false;
left=false;
down=true;
up=false;
if(position.x<0)
{
left=false;
right=true;
}
}
}
为什么在你的 child class 中设置边界,Sprite
已经设置了边界,如果你对与其他 objects 的碰撞感兴趣,请使用它。位置和大小相同,我认为您不需要 Child class 中的这些额外数据成员,使用 parent x
,y
作为位置width
和 height
维度。
public class CentipedeBody extends Sprite {
enum State{
LEFT,RIGHT,DOWN
}
State currentState,previousState ;
public static final float DOWN_MOVEMENT=50;
public float downMovCounter;
public float speed;
public CentipedeBody(TextureRegion image, Vector2 position, Vector2 size) {
super(new TextureRegion(image));
setPosition(position.x,position.y);
setSize(size.x,size.y);
currentState=State.LEFT;
previousState=State.LEFT;
speed=50;
}
public void update() {
float delta=Gdx.graphics.getDeltaTime();
if(currentState ==State.LEFT){
setPosition(getX()-speed*delta,getY());
if(getX()<0) {
previousState=currentState;
currentState = State.DOWN;
}
}
if(currentState ==State.RIGHT){
setPosition(getX()+speed*delta,getY());
if(getX()> Gdx.graphics.getWidth()-getWidth()) {
previousState=currentState;
currentState = State.DOWN;
}
}
if(currentState ==State.DOWN){
setPosition(getX(),getY()+speed*delta);
downMovCounter++;
if(downMovCounter>DOWN_MOVEMENT){
downMovCounter=0;
currentState =previousState==State.LEFT?State.RIGHT:State.LEFT;
}
}
}
}
在渲染方法中
batch.begin();
batch.draw(centipedeBody,centipedeBody.getX(),centipedeBody.getY(),centipedeBody.getWidth(),centipedeBody.getHeight());
batch.end();
centipedeBody.update();
可能您需要 CentipedeBody
中的边界、大小和位置,我无法通过一个 class 代码来判断您的游戏需求,因此您可以轻松地将您的变量集成到我的代码中。
我试图让精灵在它小于 0 时从屏幕边缘反弹。现在它只是在屏幕上放大到空白处。这是我的代码注释,CentipedeBody 是一个扩展 Sprite 的 class。在 render
方法中,我调用了 ex.update();
,它是 class 的一个对象。然后在 batch.begin()
和 batch.end()
之间我有 batch.draw(ex,ex.getPosition().x,ex.getPosition().y,ex.getSize().x,ex.getSize().y);
public class CentipedeBody extends Sprite
{
public CentipedeBody(TextureRegion image,Vector2 position,Vector2 size) {
super(new TextureRegion(image));
this.position = position;
this.size=size;
bounds=new Rectangle(position.x,position.y,8,8);
left=true;
}
public void update() {
bounds.set(getPosition().x,getPosition().y,8,8);
if (left==true) {
position.x-=(.5f);
up=false;
down=false;
right=false;
left=true;
}
if (right==true) {
position.x+=.5f;
left=false;
right=true;
down=false;
up=false;
}
if (down==true) {
position.y-=(.5f);
right=false;
left=false;
down=true;
up=false;
if(position.x<0)
{
left=false;
right=true;
}
}
}
为什么在你的 child class 中设置边界,Sprite
已经设置了边界,如果你对与其他 objects 的碰撞感兴趣,请使用它。位置和大小相同,我认为您不需要 Child class 中的这些额外数据成员,使用 parent x
,y
作为位置width
和 height
维度。
public class CentipedeBody extends Sprite {
enum State{
LEFT,RIGHT,DOWN
}
State currentState,previousState ;
public static final float DOWN_MOVEMENT=50;
public float downMovCounter;
public float speed;
public CentipedeBody(TextureRegion image, Vector2 position, Vector2 size) {
super(new TextureRegion(image));
setPosition(position.x,position.y);
setSize(size.x,size.y);
currentState=State.LEFT;
previousState=State.LEFT;
speed=50;
}
public void update() {
float delta=Gdx.graphics.getDeltaTime();
if(currentState ==State.LEFT){
setPosition(getX()-speed*delta,getY());
if(getX()<0) {
previousState=currentState;
currentState = State.DOWN;
}
}
if(currentState ==State.RIGHT){
setPosition(getX()+speed*delta,getY());
if(getX()> Gdx.graphics.getWidth()-getWidth()) {
previousState=currentState;
currentState = State.DOWN;
}
}
if(currentState ==State.DOWN){
setPosition(getX(),getY()+speed*delta);
downMovCounter++;
if(downMovCounter>DOWN_MOVEMENT){
downMovCounter=0;
currentState =previousState==State.LEFT?State.RIGHT:State.LEFT;
}
}
}
}
在渲染方法中
batch.begin();
batch.draw(centipedeBody,centipedeBody.getX(),centipedeBody.getY(),centipedeBody.getWidth(),centipedeBody.getHeight());
batch.end();
centipedeBody.update();
可能您需要 CentipedeBody
中的边界、大小和位置,我无法通过一个 class 代码来判断您的游戏需求,因此您可以轻松地将您的变量集成到我的代码中。