gpt4 book ai didi

java - 在 LibGDX 中逐像素绘制形状

转载 作者:行者123 更新时间:2023-12-01 20:06:44 25 4
gpt4 key购买 nike

我正在尝试制作带有两个形状的三角形和圆形的介绍屏幕,如下图所示。

enter image description here

我想要做的是在 2 秒的时间内从一个点到最后一个像素地绘制这些形状,使其看起来像动画。我尝试使用 ShapeRenderer 但它只是简单地一次性放置形状。如何制作动画?

最佳答案

您可以创建一个反射(reflect)操作类的类。 Actions类用于制作动画,它需要 Actor 才能发挥作用。我用一个空的 Actor 来设置它,这有点老套,但它有效。当 Action 完成动画后,它会从 SequenceAction 中删除 ifself 并停止渲染。在您的自定义操作类中,您将在需要时将其设置为 Activity 状态并在完成动画时保持渲染。

private ShapeRenderer renderer;
private SequenceAction action;

@Override
public void create() {

Vector2 center = new Vector2( 0.5f * Gdx.graphics.getWidth(), 0.5f * Gdx.graphics.getHeight() );

renderer = new ShapeRenderer();
action = Actions.sequence(
new LineAction( 0.5f, new Vector2( 0, 0 ).add( center ), new Vector2( -20, 40 ).add( center ), 1, renderer ),
new LineAction( 0.5f, new Vector2( -20, 40 ).add( center ), new Vector2( -40, 0 ).add( center ), 1, renderer ),
new LineAction( 0.5f, new Vector2( -40, 0 ).add( center ), new Vector2( 20, 0 ).add( center ), 1, renderer ),
new CirleAction( 0.5f, center, 30, 20, 0, -315, 1, renderer )
);
action.setActor( new Actor() );
}

@Override
public void render() {

Gdx.gl.glClearColor( 0, 0, 0, 1 );
Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );

renderer.begin( ShapeRenderer.ShapeType.Line );
action.act( Gdx.graphics.getDeltaTime() );
renderer.end();
}

LineAction 类。

class LineAction extends TemporalAction {

private Vector2 pointA = new Vector2(), pointB = new Vector2(), tmp = new Vector2();
private float lineWidth;
private ShapeRenderer renderer;

public LineAction( float duration, Vector2 pointA, Vector2 pointB, float lineWidth, ShapeRenderer renderer ){

super( duration );

this.pointA.set( pointA );
this.pointB.set( pointB );
this.lineWidth = lineWidth;
this.renderer = renderer;
this.actor = new Actor();
}

@Override
protected void update( float percent ) {

Vector2 point = tmp
.set( pointB )
.sub( pointA )
.scl( percent )
.add( pointA );

renderer.rectLine( pointA, point, lineWidth );
}
}

用 vector 控制圆比用圆弧绘制方法更容易。要更改圆的线段,请更改长度参数。

class CircleAction extends TemporalAction {

private Vector2[] points;
private float lineWidth;
private ShapeRenderer renderer;

public CirleAction( float duration, Vector2 offset, int length, float radius, float startAngle, float endAngle, float lineWidth, ShapeRenderer renderer ){

super( duration );

this.points = new Vector2[ length ];
this.lineWidth = lineWidth;
this.renderer = renderer;
this.actor = new Actor();

float degrees = (endAngle - startAngle) / (float) length;

for ( int i = 0; i < length; ++i ){
points[ i ] = new Vector2( radius, 0 ).rotate( degrees * i ).add( offset );
}
}

@Override
protected void update( float percent ) {

for ( int i = 0, l = MathUtils.floor( (points.length - 1) * percent ); i < l; ++i ) {
renderer.rectLine( points[ i ], points[ i + 1 ], lineWidth );
}
}
}

关于java - 在 LibGDX 中逐像素绘制形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47354919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com