c#按钮点击事件。(c# button click event. Extracting the coordinates from a two dimensional array)
我在c#上制作游戏扫雷,我的代码的这部分是检查用户是否按下网格上的某个按钮。 然而,这样做的方式我相信它分割了他按下的按钮的xy坐标。
这意味着代码的工作方式是这里有两个二维数组。 有100个按钮按钮[10,10]。 后面是一个叫做网格的网格[10,10],在网格上我说-1是炸弹,1是空的空间。 我试图从按钮中提取他按下的xy坐标并用它检查网格。 这是下面的代码:
但是,当我按下一个按钮 - 代码int x = System.Convert.ToInt32(split [0]); 我得到'System.FormatException'。
我究竟做错了什么?
void bttnOnclick(object sender, System.EventArgs e) { Button bttnClick = sender as Button; string[] split = bttnClick.Name.Split(new Char[] { ' ' }); int x = System.Convert.ToInt32(split[0]); int y = System.Convert.ToInt32(split[1]); if (grid[x, y] == -1) { //Game Over! for (int xx = 0; xx < SizeX; xx++) { for (int yy = 0; yy < SizeY; yy++) { if (grid[xx, yy] == -1) { buttons[xx, yy].Visible = false; } } } } }
I am making the game minesweeper on c# and this part of my code is checking to see if the user pressed a certain button on the grid. However, the way to do that I believe it split the x y coordinates of the button he pressed.
Meaning the way the code works is that there are two 2-dimensional array going on here. There are 100 buttons button[10,10]. and behind that is a grid called grid[10,10] and on the grid I said -1 is a bomb and 1 is an empty space. I am trying to extract the x-y coordinate that he presses from the button and check the grid with it. And this is the code below:
However,when I press a button - at code int x = System.Convert.ToInt32(split[0]); I get a 'System.FormatException'.
What am I doing wrong?
void bttnOnclick(object sender, System.EventArgs e) { Button bttnClick = sender as Button; string[] split = bttnClick.Name.Split(new Char[] { ' ' }); int x = System.Convert.ToInt32(split[0]); int y = System.Convert.ToInt32(split[1]); if (grid[x, y] == -1) { //Game Over! for (int xx = 0; xx < SizeX; xx++) { for (int yy = 0; yy < SizeY; yy++) { if (grid[xx, yy] == -1) { buttons[xx, yy].Visible = false; } } } } }
原文:https://stackoverflow.com/questions/34227832
最满意答案
Here is a very good description of your question http://www.vogella.com/tutorials/AndroidTaskScheduling/article.html
相关问答
更多-
ACTION_BATTERY_LOW是系统的广播操作,因此出于安全原因您将无法生成它 - 至少不是在“常规”设备上 ACTION_BATTERY_LOW is a system's broadcast action, so you won't be able to generate it for security reasons - at least not on a "regular" device
-
测试电池使用(Testing Battery Usage)[2023-01-16]
实际上,我相信大部分有电源问题的应用程序,都有“CPU”问题。 也就是说,应用程序的CPU使用情况的配置文件可能是您的电池消耗的很好的近似值。 如果您的应用程序在GPU,无线网络,存储等方面做的很昂贵,而且昂贵的操作并不花费太多的CPU时间,那么请注意和例外。 这是一个关于“Power Tutor”应用程序的有趣博客,它提供了比内置电池应用程序更准确的运行系统测量: http : //gigaom.com/mobile/android-power-consumption-app/ 。 我还没试过 对于另一个 ... -
我可以告诉你,当你使用低功率-startMonitoringSignificantLocationChanges时,不能也不需要定时器。 该方法仅在设备检测到更改时响应委托的回调。 这个位置检查不使用GPS,它使用已经发生的Wifi和蜂窝塔三角测量。 因此,通过使用此方法,无需减慢速度以节省电池寿命。 只需设置委托方法并做出相应的响应。 我不确定您的位置实现是什么,但区域监控也是使用很少甚至没有电池来获取位置更新的另一种好方法。 当您具有要监视的特定位置而不仅仅是一般用户位置时,区域监视会更有帮助。 希望这 ...
-
AlarmManager使用setExactAndAllowWhileIdle重复(AlarmManager use setExactAndAllowWhileIdle so it repeats)[2022-03-15]
ADM 点亮 。 作为TrackingAlarmReceiver工作的一部分,您可以调用setExactAndAllowWhileIdle()来安排下一步的工作。 请记住,此类事件的最小粒度为〜10分钟IIRC,即使您已获得控制权,您也可能无法访问网络。 ADM is spot-on. As part of the work in TrackingAlarmReceiver, you call setExactAndAllowWhileIdle() to schedule the next bit of w ... -
非常低的GPU使用率(Very low GPU usage)[2022-01-19]
正如雅罗斯拉夫指出的那样 - 这是一个很难回答的问题,因为它需要分析代码(或者有幸能够识别问题的人)。 这个对github问题的评论对于分析来说是一个很好的起点,就像新的TensorFlow Performance页面一样。 As Yaroslav noted - this is a hard question to answer, because it requires profiling of your code (or someone lucky enough to recognize the pro ... -
//尝试这个 AlarmManager am=(AlarmManager)getApplicationContext getSystemService(Context.ALARM_SERVICE); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); am.setRepeating(AlarmManager.RTC_WAKEUP, ...
-
这取决于你对“更好”的定义。 您无法从清单中注册ACTION_BATTERY_CHANGED广播,因此您必须始终运行一项服务来管理此广播的动态注册接收器。 许多用户不喜欢这个。 AlarmManager避免了对持续运行服务的需求,但这意味着您的应用程序小部件在查找电池电量变化时会稍微滞后。 但是,您可以允许用户通过SharedPreference控制轮询频率,而不是将其硬编码为五分钟。 这样,用户可以控制应用程序从轮询中消耗多少CPU /电池,因此可以控制将会有多少延迟。 That depends upon ...
-
AlarmManager和Handler + WakeLock之间最好的是什么?(What's the best between AlarmManager and Handler+WakeLock?)[2024-03-02]
我做了很多测试,这是结果: -Alarm Manager比使用handler + wakelock节省更多电池以进行长时间操作。 但是,您必须对警报启动的活动/服务使用额外的唤醒锁定,因为警报管理器唤醒锁定不会覆盖它。 即使这种方法使用两个WakeLock,电池似乎更高效,寿命更长! 在测试期间(2天), AlarmManager使用比其他方法少6倍的电池 。 就我个人而言...... 希望这可以帮助一些人! I have made many test and this is the result: -Al ... -
以下是对您的问题的非常好的描述http://www.vogella.com/tutorials/AndroidTaskScheduling/article.html Here is a very good description of your question http://www.vogella.com/tutorials/AndroidTaskScheduling/article.html
-
电池时间计算(Battery time calculation)[2022-08-29]
这个功能很难开发,因为它取决于很多因素。 您最好为每个特定平台实施本机电池通知。 有些平台提供有关功耗和剩余电池寿命的全面信息。 这将(可能)导致更好的估计并为您节省大量工作:) iPhone的例子; UIDevice *device = [UIDevice currentDevice]; device.batteryMonitoringEnabled = YES; [[NSNotificationCenter defaultCenter] addObserver:self selector:@select ...