libgdx 实现转盘功能

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;
		
	}
	
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值