1
因此,我正在嘗試在一個教程上學習如何在C#中爲移動應用程序進行編碼。我一直在研究最初針對Windows Forms的教程,並且一直試圖將代碼格式化爲android版本,以便我可以在手機上進行測試。 我目前遇到的問題是從微調器中獲取一個值並稍後使用它。我想要做的就是從旋轉器上拿一把「武器」,然後用那個武器對怪物造成傷害。 原始代碼看起來像這樣爲Windows窗體:Android:將一個變量設置爲一個微調項目供以後使用
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));
}
}
}
的cboWeapons Windows窗體是一個組合框。 我在我的代碼中保留了相同的命名,但它是一個微調。
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();
}
我的武器級轎車在發動機項目定義,我有做currentWeapon東西,我可以像Windows窗體確實後來的代碼中引用的問題。
這使得其被定義爲 「TheSelectedItem」 一個變種。我怎樣才能讓這個變量引用我的武器類? –
在我的應用程序中,我使用了兩個並行的字符串列表,一個用於武器,另一個用於獲取關於武器的信息,var SelectedItem包含列表中項目的位置。例如,如果列表包含「Spear,Axe,Bow」並且用戶選擇「Ax」,那麼var等於2.然後,使用該值來索引我的項目列表,例如:var weaponinfo = weaponitem [SelectedItem]的ToString();這讓我瞭解了武器的所有信息。我自己對Android應用程序相當陌生,並且此解決方案適用於我。 –
我喜歡使用2個列表的想法。我想在我的代碼中使用它,但是我從第二組代碼中收到的錯誤是我無法將字符串轉換爲int。看看你的第一個代碼,是不是定義了一個字符串而不是2的整數? –