libgdx 实现转盘功能

xiaoxiao2021-02-28  85

package jaze.gdx; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.backends.lwjgl.LwjglApplication; import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.Image; import com.badlogic.gdx.utils.Align; import com.jicent.ui.button.Button; import com.jicent.ui.button.Button.InputListenerEx; import com.jicent.ui.button.ScaleChangeBtn; public class Test_ZhuanPan extends ApplicationAdapter implements InputListenerEx{ private Stage st; //背景 Image imgBg; // boolean c , f = false; private float rotateStep; private float keepTime=120; private float keepCount; private boolean start; private int state; private static final int acc=0; private static final int keep=1; private static final int reduce=2; private int itemNum=8;//奖品数量 private float resultRotation; @Override public void create() { //初始化 st = new Stage(); //添加 按钮 点击事件 Gdx.input.setInputProcessor(st); //给舞台 注册 点击事件的 监听 //背景 图 imgBg = new Image(new Texture(Gdx.files.internal("image/cjBg.png"))); imgBg.setPosition(280, 445,Align.center).addTo(st); imgBg.setOrigin(Align.center); //抽奖 按钮 Button bt = new ScaleChangeBtn(new Texture(Gdx.files.internal("image/cj.png"))); bt.setPosition(280, 441,Align.center); bt.addListener(this); st.addActor(bt); } /** * 每帧 刷新 */ @Override public void render() { // 清除 界面 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); //清除 后界面的背景色 Gdx.gl.glClearColor(0.5f, 0.8f, 0.5f, 0.8f); st.act(); st.draw(); if (start) { switch (state) { case acc://加速过程 rotateStep+=0.1f; if (rotateStep>10) { rotateStep=10; state=keep; } break; case keep://保持旋转 keepCount++; if (keepCount>keepTime) { keepCount=0; state=reduce; //计算从10->0时,每帧减少0.1,累计减少的角度是多少 float allRotation=add(rotateStep); imgBg.setRotation(resultRotation+allRotation); } break; case reduce://减速 rotateStep-=0.1f; if (rotateStep<0) { rotateStep=0; state=-1; start=false; } break; default: break; } imgBg.rotateBy(-rotateStep); } } public float add(float num){ if (num<0) { return 0; }else { float b=add(num-0.1f); return num+b; } } /** * 程序入口 * @param args */ public static void main(String[] args){ LwjglApplicationConfiguration lcf = new LwjglApplicationConfiguration(); lcf.width = 540; lcf.height = 960; new LwjglApplication(new Test_ZhuanPan(), lcf); } @Override public boolean touchDown(Actor actor, float x, float y, int pointer) { return true; } @Override public void touchUp(Actor actor, float x, float y, int pointer) { start=true; state=acc; float result=MathUtils.perc(); float startPerc=0; float gap=1f/itemNum; int resultItem=1; while (result>startPerc+gap) {//计算出最终结果在哪个item的范围内部 startPerc+=gap; resultItem++; } System.out.println(resultItem); resultRotation = (resultItem-1)*360f/itemNum; } }
转载请注明原文地址: https://www.6miu.com/read-37318.html

最新回复(0)