我希望在精灵单位周围创造一种战争风格的效果,并被告知scissorstack会做这个工作,但我无法弄清楚我做错了什么......
我有典型的libgdx设置。每个游戏角色我都有一个班级。我想通过配置每个类来控制每个单元看到的“多远”。所以说我希望MainPlayer具有战争迷雾5,但是一个Pawn单位在该单位所在的棋子周围有一个1级空间的战争迷雾。
我加载了精灵,tmx地图和碰撞检测工作正常。现在我无法弄清楚我应该将scissorstack代码放在Player类中/如何使其工作。我也想知道雾周围可见瓷砖的坐标......
public class Player extends Sprite implements InputProcessor{
private Vector2 velocity = new Vector2();
public Player(Sprite sprite, TiledMapTileLayer collision layer){
super(sprite);
this.collisionLayer=collisionLayer;
}
public void draw(SpriteBatch spriteBatch){
update(Gdx.graphics.getDeltaTime());
super.draw(spriteBatch);
}
public Vector2 getVelocity() {
return velocity;
}
public void setVelocity(Vector2 velocity) {
this.velocity = velocity;
}
public float getSpeed() {
return speed;
}
public void setSpeed(float speed) {
this.speed = speed;
}
public float getGravity() {
return gravity;
}
public void setGravity(float gravity) {
this.gravity = gravity;
}
public TiledMapTileLayer getCollisionLayer() {
return collisionLayer;
}
public void setCollisionLayer(TiledMapTileLayer collisionLayer) {
this.collisionLayer = collisionLayer;
}
public boolean keyDown(int keycode){
switch(keycode){
case Keys.W:
setY(getY()+1*50);//velocity.x=speed;
break;
case Keys.A:
setX(getX()-1*50);//velocity.x=-speed;
break;
case Keys.D:
setX(getX()+1*50);//velocity.x=speed;
break;
case Keys.S:
setY(getY()-1*50);//velocity.y=-speed;
break;
}
System.out.println(getX()/50+", "+getY()/50);
return true;
}
public boolean keyUp(int keycode){
switch(keycode){
case Keys.W:
velocity.y=0;
break;
case Keys.A:
velocity.x=0;
break;
case Keys.D:
velocity.x=0;
break;
case Keys.S:
velocity.y=0;
break;
}return true;
}
@Override
public boolean keyTyped(char character) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean scrolled(int amount) {
// TODO Auto-generated method stub
return false;
}
}
}
我尝试了以下内容:Making a Group hide Actors outside of its bounds
这是我在Player类'draw方法中尝试过的:
public void draw(SpriteBatch spriteBatch){
update(Gdx.graphics.getDeltaTime());
Rectangle scissors = new Rectangle();
Rectangle clipBounds = new Rectangle(getX(),getY(),4*width,4*height);
ScissorStack.calculateScissors(camera, spriteBatch.getTransformMatrix(), clipBounds, scissors);
ScissorStack.pushScissors(scissors);
spriteBatch.draw(...);
spriteBatch.flush();
ScissorStack.popScissors();
super.draw(spriteBatch);
}
我感到困惑。在玩家实体类中使用scissorstack的一个例子会很有帮助。感谢
答案 0 :(得分:2)
NathanSweet非常友好地帮我解决了一段时间后如何使其工作(对于LibGdx 0.9.9): CalculateScissors
所以对于任何寻找示例的人来说,此代码之前对我有用:
Rectangle scissor = new Rectangle();
Rectangle clipBounds = new Rectangle(0, 0, worldW, worldH);
...
batch.begin();
...
ScissorStack.calculateScissors(camera, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), batch.getTransformMatrix(), clipBounds, scissor);
ScissorStack.pushScissors(scissor);
...
ScissorStack.popScissors();
batch.end();
您只需要使用clipBounds的值,即可实现所需的效果。
答案 1 :(得分:0)
我自己没有用过它,但我会这样试试:
public void draw(SpriteBatch spriteBatch) {
update(Gdx.graphics.getDeltaTime());
Rectangle scissors = new Rectangle();
Rectangle clipBounds = new Rectangle(getX(),getY(),4*width,4*height);
ScissorStack.calculateScissors(camera, spriteBatch.getTransformMatrix(), clipBounds, scissors);
ScissorStack.pushScissors(scissors);
super.draw(spriteBatch);
ScissorStack.popScissors();
}