Android 基于数组值的图像
我有一个骰子游戏,我正试图改变骰子掷到“死骰子”图像后选择的骰子图像。我已经尝试了我能想到的一切,我总是在索引中找到值“0”或其他东西,但正确的值永远不会消失 当选择骰子时,它将其值设置为负数。示例I选择6它将值更改为-6,并将模具更改为-6模具图像 我怎样才能让它显示并保留我想要的“死”图像 这是它获取图像的区域Android 基于数组值的图像,android,arrays,image,dice,Android,Arrays,Image,Dice,我有一个骰子游戏,我正试图改变骰子掷到“死骰子”图像后选择的骰子图像。我已经尝试了我能想到的一切,我总是在索引中找到值“0”或其他东西,但正确的值永远不会消失 当选择骰子时,它将其值设置为负数。示例I选择6它将值更改为-6,并将模具更改为-6模具图像 我怎样才能让它显示并保留我想要的“死”图像 这是它获取图像的区域 //Get the Dice Images public Integer getImage(int index) {
//Get the Dice Images
public Integer getImage(int index) {
if (diceValues[index] == 0) {
return letterImages.get(index);
} else {
return diceImages.get((diceValues[index]));
}
}
我试过换衣服
return letterImages.get(index);
当我让它改变图像时,它总是以“0”或当前选择的骰子数或其他一些我不确定它是如何产生的数字结束
这是整个DieManager课程
package com.mygames.dice;
import java.util.HashMap;
import android.util.Log;
public class DieManager {
// Will return the Indexes of the dice when this is used
public static final int INDEX_FLAG = 1;
// Will return the values of the dice when this is used
public static final int VALUE_FLAG = 2;
// Will return the absolute values of the dice when this is used
public static final int ABS_VALUE_FLAG = 3;
// The array that holds the dice
private int[] diceValues = { 0, 0, 0, 0, 0, 0 };
private HashMap<Integer, Integer> diceImages = new HashMap<Integer, Integer>();
private HashMap<Integer, Integer> deadImages = new HashMap<Integer, Integer>();
private HashMap<Integer, Integer> letterImages = new HashMap<Integer, Integer>();
// Sets @newValue to dieValues[@index]
public void setValue(int index, int newValue) {
Log.w(getClass().getName(), "Index = " + index + " Value = " + newValue);
diceValues[index] = newValue;
}
public DieManager() {
super();
initializeMaps();
}
private void initializeMaps() {
diceImages.put(-6, R.drawable.die6_select);
diceImages.put(-5, R.drawable.die5_select);
diceImages.put(-4, R.drawable.die4_select);
diceImages.put(-3, R.drawable.die3_select);
diceImages.put(-2, R.drawable.die2_select);
diceImages.put(-1, R.drawable.die1_select);
diceImages.put(1, R.drawable.die1_roll);
diceImages.put(2, R.drawable.die2_roll);
diceImages.put(3, R.drawable.die3_roll);
diceImages.put(4, R.drawable.die4_roll);
diceImages.put(5, R.drawable.die5_roll);
diceImages.put(6, R.drawable.die6_roll);
deadImages.put(-1, R.drawable.die1_dead);
deadImages.put(-2, R.drawable.die2_dead);
deadImages.put(-3, R.drawable.die3_dead);
deadImages.put(-4, R.drawable.die4_dead);
deadImages.put(-5, R.drawable.die5_dead);
deadImages.put(-6, R.drawable.die6_dead);
letterImages.put(0, R.drawable.die_no);
letterImages.put(1, R.drawable.die_no);
letterImages.put(2, R.drawable.die_no);
letterImages.put(3, R.drawable.die_no);
letterImages.put(4, R.drawable.die_no);
letterImages.put(5, R.drawable.die_no);
}
public void rollDice() {
boolean isNewRound = (numOnTable() == 0);
for (int j = 0; j < 6; j++) {
// If its a new round then the dice value can be changed from 0.
// Else it can't
if (isNewRound || diceValues[j] != 0)
diceValues[j] = (int) ((Math.random() * 6) + 1);
}
}
// Returns the value or absolute value
public int getValue(int index, int flag) {
if (flag == ABS_VALUE_FLAG)
return Math.abs(diceValues[index]);
return diceValues[index];
}
// If a dice value is 0 then its a letter
public int numOnTable() {
int count = 6;
for (int i : diceValues) {
if (i == 0)
count--;
}
return count;
}
// Picking up makes the dice value 0
public void pickUp(int[] highlighted) {
for (int i = 0; i < highlighted.length; i++)
diceValues[highlighted[i]] = 0;
}
// A negative value means a die is highlighted
public void toggleHighlight(int index) {
diceValues[index] *= -1;
}
public void clearTable() {
diceValues[0] = 0;
diceValues[1] = 0;
diceValues[2] = 0;
diceValues[3] = 0;
diceValues[4] = 0;
diceValues[5] = 0;
}
// Return the dice that aren't 0
public int[] diceOnTable(int flag) {
if (flag == ABS_VALUE_FLAG) {
int[] array = new int[diceValues.length];
for (int i = 0; i < diceValues.length; i++)
array[i] = Math.abs(diceValues[i]);
return array;
}
return diceValues;
}
//Returns dice that are negative
public int[] getHighlighted(int flag) {
int[] dirtyArray = { 0, 0, 0, 0, 0, 0 };
int count = 0;
for (int j = 0; j < 6; j++) {
if (diceValues[j] < 0) {
if (flag == INDEX_FLAG)
dirtyArray[count++] = j;
if (flag == VALUE_FLAG)
dirtyArray[count++] = diceValues[j];
if (flag == ABS_VALUE_FLAG)
dirtyArray[count++] = Math.abs(diceValues[j]);
}
}
int[] cleanArray = new int[count];
//Returns an array of length 0
if (count == 0)
return cleanArray;
System.arraycopy(dirtyArray, 0, cleanArray, 0, count);
return cleanArray;
}
// Finds in dieValues same @value and excludes @index
public int[] findPairs(int index, int flag) {
int[] dirtyArray = { 0, 0, 0, 0, 0, 0 };
int count = 0;
for (int j = 0; j < 6; j++) {
if (j != index
&& Math.abs(diceValues[j]) == Math.abs(diceValues[index])) {
if (flag == INDEX_FLAG)
dirtyArray[count++] = j;
if (flag == VALUE_FLAG)
dirtyArray[count++] = diceValues[j];
if (flag == ABS_VALUE_FLAG)
dirtyArray[count++] = Math.abs(diceValues[j]);
}
}
int[] cleanArray = new int[count];
if (count == 0)
return cleanArray;
System.arraycopy(dirtyArray, 0, cleanArray, 0, count);
return cleanArray;
}
//Get the Dice Images
public Integer getImage(int index) {
if (diceValues[index] == 0) {
return letterImages.get(index);
} else {
return diceImages.get((diceValues[index]));
}
}
//Get the Number of dice remaining that are not 0
public int numDiceRemain() {
int counter = 0;
for (int j = 0; j < diceValues.length; j++) {
if (diceValues[j] > 0)
counter++;
}
return counter;
}
}
package com.mygames.dice;
导入java.util.HashMap;
导入android.util.Log;
公共课经理{
//使用时将返回骰子的索引
公共静态最终整数索引_标志=1;
//使用此选项时,将返回骰子的值
公共静态最终int值_标志=2;
//使用时将返回骰子的绝对值
公共静态最终int ABS_值_标志=3;
//持有骰子的阵列
私有int[]值={0,0,0,0,0,0};
私有HashMap diceImages=新HashMap();
私有HashMap deadImages=新HashMap();
私有HashMap letterImages=新HashMap();
//将@newValue设置为dieValues[@index]
public void setValue(int index,int newValue){
Log.w(getClass().getName(),“Index=“+Index+”Value=“+newValue”);
diceValues[索引]=新值;
}
公共事务经理(){
超级();
初始化映射();
}
私有无效初始值映射(){
骰子图像。放置(-6,右可绘制。骰子6_选择);
骰子图像。放置(-5,右可拖动。骰子5_选择);
骰子图像。放置(-4,右可绘制。骰子4_选择);
骰子图像。放置(-3,右可绘制。骰子3_选择);
骰子图像。放置(-2,R。可绘制。骰子2_选择);
骰子图像。放置(-1,右可绘制。骰子1_选择);
掷骰子。掷骰子(1,右,可拉深。骰子卷);
骰子图像。放置(2,R。可拉伸。骰子2_卷);
骰子图像。放置(3,R。可拉伸。骰子3_卷);
骰子图像。放置(4,R。可拉伸。骰子4_卷);
骰子图像。放置(5,右拉深。骰子5_卷);
掷骰子。掷骰子(6,右可拉深。骰子6_卷);
deadImages.put(-1,R.drawable.die1_dead);
deadImages.put(-2,R.drawable.die2_dead);
deadImages.put(-3,R.drawable.die3_dead);
deadImages.put(-4,R.drawable.die4_dead);
deadImages.put(-5,R.drawable.die5_dead);
deadImages.put(-6,R.drawable.die6_dead);
字母图像。放置(0,可拉深。模具编号);
字母图像。放置(1,右可拉深。模具编号);
字母图像。放置(2个,右可拉深。模具编号);
字母图像。放置(3,右侧可拉深。模具编号);
字母图像。放置(4,右可拉深。模具编号);
字母图像。放置(5,右可拉深。模具编号);
}
公共无效滚动骰子(){
布尔值isNewRound=(numtable()==0);
对于(int j=0;j<6;j++){
//如果是新一轮,则骰子值可以从0更改。
//否则就不行了
if(isNewRound | | diceValues[j]!=0)
数值[j]=(int)((Math.random()*6)+1);
}
}
//返回值或绝对值
公共int getValue(int索引,int标志){
如果(标志==ABS\U值\U标志)
返回Math.abs(diceValues[index]);
返回值[索引];
}
//如果骰子值为0,则为字母
公共整数可数(){
整数计数=6;
for(int i:dice值){
如果(i==0)
计数--;
}
返回计数;
}
//拾取使骰子值为0
公共无效拾取(突出显示int[]{
for(int i=0;idiceValues[j] = (int) ((Math.random() * 6) + 1);