<?xml version='1.0' encoding="utf-8"?>
      <rss version='2.0'>
      <channel>
      <title>Форум на Исходниках.RU</title>
      <link>https://forum.sources.ru</link>
      <description>Форум на Исходниках.RU</description>
      <generator>Форум на Исходниках.RU</generator>
  	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=348206&amp;view=findpost&amp;p=3053291</guid>
        <pubDate>Wed, 28 Dec 2011 14:29:55 +0000</pubDate>
        <title>[Android] пишем игру</title>
        <link>https://forum.sources.ru/index.php?showtopic=348206&amp;view=findpost&amp;p=3053291</link>
        <description><![CDATA[Doctor Drive: <div class='tag-quote'><a class='tag-quote-link' href='https://forum.sources.ru/index.php?showtopic=348206&view=findpost&p=3052797'><span class='tag-quote-prefix'>Цитата</span></a> <span class='tag-quote__quote-info'>mrco &#064; <time class="tag-quote__quoted-time" datetime="2011-12-27T21:52:19+04:00">27.12.11, 17:52</time></span><div class='quote '>View.getWidth() - вы ее в конструкторе вызываете, в этот момент размер вьюхи не известен и функция вернет 0.<br>
GameView - чей наследник? Для игры лучше использовать SurfaceView и отдельный поток отрисовки.</div></div><br>
<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">public final class GameView extends View</div></ol></div></div></div></div><script>preloadCodeButtons('1');</script><br>
<br>
Отдельный поток отрисовк я так понял не это имееться в виду.<br>
ViewRootCalledFromWrongThreadException<br>
<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">&nbsp;&nbsp; &nbsp;private void startGame() {</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;new Thread() {</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;public void run() {</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;setContentView(R.layout.game);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GameView gv = (GameView)findViewById(R.id.gameView);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while (true) {</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;gv.invalidate();</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try { Thread.sleep(100); } catch (InterruptedException ie) {}</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}.start();</div><div class="code_line">&nbsp;&nbsp; &nbsp;}</div></ol></div></div></div></div><br>
<br>
<br>
И так понял не это, так как висит да и вообще подозрительный подход<br>
<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">&nbsp;&nbsp; &nbsp;@Override</div><div class="code_line">&nbsp;&nbsp; &nbsp;protected void onDraw(final Canvas canvas) {</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;new Thread() { public void run() {</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;canvas.drawBitmap(apple.b, apple.x, apple.y, null);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;apple.y ++;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if (apple.y &#62; sh) initApple(); }}.start();</div><div class="code_line">&nbsp;&nbsp; &nbsp;}</div></ol></div></div></div></div><br>
<br>
Да и так будет ViewRootCalledFromWrongThreadException<br>
<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">public class GameView extends View implements Runnable {</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; ....</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;public void run() {</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;while (true) {</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;invalidate();</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try { Thread.sleep(100); } catch (InterruptedException ie) {}</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</div><div class="code_line">&nbsp;&nbsp; &nbsp;}</div></ol></div></div></div></div><br>
<br>
<div class='tag-quote'><a class='tag-quote-link' href='https://forum.sources.ru/index.php?showtopic=348206&view=findpost&p=3052908'><span class='tag-quote-prefix'>Цитата</span></a> <span class='tag-quote__quote-info'>Хрен &#064; <time class="tag-quote__quoted-time" datetime="2011-12-27T23:01:59+00:00">27.12.11, 23:01</time></span><div class='quote '>Mario Zechner - Beginning Android Games поможет</div></div><br>
Спс, посмотрю.]]></description>
        <author>Doctor Drive</author>
        <category>Android</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=348206&amp;view=findpost&amp;p=3052908</guid>
        <pubDate>Tue, 27 Dec 2011 23:01:59 +0000</pubDate>
        <title>[Android] пишем игру</title>
        <link>https://forum.sources.ru/index.php?showtopic=348206&amp;view=findpost&amp;p=3052908</link>
        <description><![CDATA[Хрен: Mario Zechner - Beginning Android Games поможет<br><br>getWidth обычно вызывается в OnCreate методе]]></description>
        <author>Хрен</author>
        <category>Android</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=348206&amp;view=findpost&amp;p=3052797</guid>
        <pubDate>Tue, 27 Dec 2011 17:52:19 +0000</pubDate>
        <title>[Android] пишем игру</title>
        <link>https://forum.sources.ru/index.php?showtopic=348206&amp;view=findpost&amp;p=3052797</link>
        <description><![CDATA[mrco: View.getWidth() - вы ее в конструкторе вызываете, в этот момент размер вьюхи не известен и функция вернет 0.<br>GameView - чей наследник? Для игры лучше использовать SurfaceView и отдельный поток отрисовки.]]></description>
        <author>mrco</author>
        <category>Android</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=348206&amp;view=findpost&amp;p=3052669</guid>
        <pubDate>Tue, 27 Dec 2011 14:12:04 +0000</pubDate>
        <title>[Android] пишем игру</title>
        <link>https://forum.sources.ru/index.php?showtopic=348206&amp;view=findpost&amp;p=3052669</link>
        <description><![CDATA[Doctor Drive: С целью освоить Android начинаю писать игру, кторую я написал на MobileBasic 5 лет назад.<br>
<img class='tag-img' src='http://docd.zx6.ru/proj/mb/Basket1.jpg' alt='user posted image'><br>
Надо ловить яблоки, падающие сверху.<br>
Когда я запускаю приложение на данном этапе, есть две проблемы.<br>
View.getWidth(); вероятно, возвращает null<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">&nbsp;&nbsp; &nbsp;private void initApple() {</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;apple.x = rnd.nextInt(400); //работает</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;apple.x = rnd.nextInt(sw); //не работает</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;apple.y = 0;</div><div class="code_line">&nbsp;&nbsp; &nbsp;}</div></ol></div></div></div></div><br>
<br>
<br>
где sw<br>
<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">&nbsp;&nbsp; &nbsp;public GameView(Context context, AttributeSet attrs) {</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;super(context, attrs);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//...</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sw = this.getWidth();</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//...</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;initApple();</div><div class="code_line">&nbsp;&nbsp; &nbsp;}</div></ol></div></div></div></div><br>
<br>
И второе, если делать invalidate() с целью сделать цикл падения яблока - приложение Not Responding.<br>
<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">&nbsp;&nbsp; &nbsp;private void startGame() {</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;setContentView(R.layout.game);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;GameView gv = (GameView)findViewById(R.id.gameView);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;while (true) {</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;gv.invalidate();</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try { Thread.sleep(100); } catch (InterruptedException ie) {}</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</div><div class="code_line">&nbsp;&nbsp; &nbsp;}</div></ol></div></div></div></div><br>
<br>
для перерисовки и инкрементации координаты.<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">&nbsp;&nbsp; &nbsp;@Override</div><div class="code_line">&nbsp;&nbsp; &nbsp;protected void onDraw(Canvas canvas) {</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;canvas.drawBitmap(apple.b, apple.x, apple.y, null);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;apple.y ++;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if (apple.y &#62; sh) initApple();</div><div class="code_line">&nbsp;&nbsp; &nbsp;}</div></ol></div></div></div></div>]]></description>
        <author>Doctor Drive</author>
        <category>Android</category>
      </item>
	
      </channel>
      </rss>
	