所以我正在尝试学习如何使用C#为移动应用程序编写代码。我一直在研究最初用于Windows Forms的教程,我一直在尝试将代码格式化为Android版本,以便我可以在手机上测试它。 我目前遇到的问题是从微调器中获取值并在以后使用它。我想做的是从旋转器中获取“武器”,然后使用该武器对怪物造成伤害。 对于Windows窗体,原始代码看起来像这样:
private void btnUseWeapon_Click(object sender, EventArgs e)
{
// Get the currently selected weapon from the cboWeapons ComboBox
Weapon currentWeapon = (Weapon)cboWeapons.SelectedItem;
// Determine the amount of damage to do to the monster
int damageToMonster = RandomNumberGenerator.NumberBetween(currentWeapon.MinimumDamage, currentWeapon.MaximumDamage);
// Apply the damage to the monster's CurrentHitPoints
_currentMonster.CurrentHitPoints -= damageToMonster;
// Display message
rtbMessages.Text += "You hit the " + _currentMonster.Name + " for " + damageToMonster.ToString() + " points." + Environment.NewLine;
// Check if the monster is dead
if(_currentMonster.CurrentHitPoints <= 0)
{
// Monster is dead
rtbMessages.Text += Environment.NewLine;
rtbMessages.Text += "You defeated the " + _currentMonster.Name + Environment.NewLine;
// Give player experience points for killing the monster
_player.ExperiencePoints += _currentMonster.RewardExperiencePoints;
rtbMessages.Text += "You receive " + _currentMonster.RewardExperiencePoints.ToString() + " experience points" + Environment.NewLine;
// Give player gold for killing the monster
_player.Gold += _currentMonster.RewardGold;
rtbMessages.Text += "You receive " + _currentMonster.RewardGold.ToString() + " gold" + Environment.NewLine;
// Get random loot items from the monster
List<InventoryItem> lootedItems = new List<InventoryItem>();
// Add items to the lootedItems list, comparing a random number to the drop percentage
foreach(LootItem lootItem in _currentMonster.LootTable)
{
if(RandomNumberGenerator.NumberBetween(1, 100) <= lootItem.DropPercentage)
{
lootedItems.Add(new InventoryItem(lootItem.Details, 1));
}
}
// If no items were randomly selected, then add the default loot item(s).
if(lootedItems.Count == 0)
{
foreach(LootItem lootItem in _currentMonster.LootTable)
{
if(lootItem.IsDefaultItem)
{
lootedItems.Add(new InventoryItem(lootItem.Details, 1));
}
}
}
// Add the looted items to the player's inventory
foreach(InventoryItem inventoryItem in lootedItems)
{
_player.AddItemToInventory(inventoryItem.Details);
if(inventoryItem.Quantity == 1)
{
rtbMessages.Text += "You loot " + inventoryItem.Quantity.ToString() + " " + inventoryItem.Details.Name +Environment.NewLine;
}
else
{
rtbMessages.Text += "You loot " + inventoryItem.Quantity.ToString() + " " + inventoryItem.Details.NamePlural + Environment.NewLine;
}
}
// Refresh player information and inventory controls
lblHitPoints.Text = _player.CurrentHitPoints.ToString();
lblGold.Text = _player.Gold.ToString();
lblExperience.Text = _player.ExperiencePoints.ToString();
lblLevel.Text = _player.Level.ToString();
UpdateInventoryListInUI();
UpdateWeaponListInUI();
UpdatePotionListInUI();
// Add a blank line to the messages box, just for appearance.
rtbMessages.Text += Environment.NewLine;
// Move player to current location (to heal player and create a new monster to fight)
MoveTo(_player.CurrentLocation);
}
else
{
// Monster is still alive
// Determine the amount of damage the monster does to the player
int damageToPlayer = RandomNumberGenerator.NumberBetween(0, _currentMonster.MaximumDamage);
// Display message
rtbMessages.Text += "The " + _currentMonster.Name + " did " + damageToPlayer.ToString() + " points of damage." + Environment.NewLine;
// Subtract damage from player
_player.CurrentHitPoints -= damageToPlayer;
// Refresh player data in UI
lblHitPoints.Text = _player.CurrentHitPoints.ToString();
if(_player.CurrentHitPoints <= 0)
{
// Display message
rtbMessages.Text += "The " + _currentMonster.Name + " killed you." + Environment.NewLine;
// Move player to "Home"
MoveTo(World.LocationByID(World.LOCATION_ID_HOME));
}
}
}
Windows窗体中的cboWeapons是一个组合框。 我在代码中保留了相同的命名,但它是一个微调器。
Spinner cboWeapons = (Spinner)FindViewById(Resource.Id.cboWeapons);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<Weapon> adapter = new ArrayAdapter<Weapon>(this,
Android.Resource.Layout.SimpleSpinnerItem, weapons);
adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
cboWeapons.Adapter = adapter;
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
// On selecting a spinner item
object CurrentWeapon =parent.GetItemAtPosition(position);
Weapon currentWeapon = new CurrentWeapon();
}
我的武器类是在一个引擎项目中定义的,我遇到了一些问题,我可以在后面的代码中像Windows窗体那样引用currentWeapon。
答案 0 :(得分:1)
这是我用来获取所选项目的内容:
var TheSelectedIten = string.Format("{0}", SimpleSpinner.GetItemAtPosition(SimpleSpinner.SelectedItemPosition));
它适用于我的字符串微调列表。
R / 普雷斯科特......