| Development/Tutorials/Qt4 Ruby Tutorial/Chapter 11 | Current message text |
| ↓...Chapter 11/Page display title/fi | Development/Tutorials/Qt4 Ruby Tutorial/Chapter 11 |
| ↓...t4 Ruby Tutorial/Chapter 11/1/fi | TutorialBrowser |
| ↓...t4 Ruby Tutorial/Chapter 11/2/fi | Qt4 Ruby -oppikurssi |
| ↓...t4 Ruby Tutorial/Chapter 11/3/fi | Giving It a Shot |
| ↓...t4 Ruby Tutorial/Chapter 11/4/fi | Oppikurssi 10 - Sileää kuin silkki |
| ↓...t4 Ruby Tutorial/Chapter 11/5/fi | Tutorial 12 - Hanging in the Air the Way Bricks Don't |
| ↓...t4 Ruby Tutorial/Chapter 11/6/fi | == Giving It a Shot == |
| ↓...t4 Ruby Tutorial/Chapter 11/7/fi | [[Image:Qt4_Ruby_Tutorial_Screenshot_11.png|center]] |
| ↓...t4 Ruby Tutorial/Chapter 11/8/fi | Files: |
| ↓...t4 Ruby Tutorial/Chapter 11/9/fi | === Yleiskuva === |
| ↓...4 Ruby Tutorial/Chapter 11/10/fi | In this example we introduce a timer to implement animated shooting. |
| ↓...4 Ruby Tutorial/Chapter 11/11/fi | === Läpikäynti rivi riviltä === |
| ↓...4 Ruby Tutorial/Chapter 11/12/fi | The '''<tt>CannonField</tt>''' now has shooting capabilities. |
| ↓...4 Ruby Tutorial/Chapter 11/13/fi | We include '''<tt>Math</tt>''' because we need the '''<tt>sin()</tt>''' and '''<tt>cos()</tt>''' functions. |
| ↓...4 Ruby Tutorial/Chapter 11/14/fi | We initialize our new private variables and connect the [http://doc.qt.nokia.com/latest/qtimer.html#timeout Qt::Timer::timeout()] signal to our '''<tt>moveShot()</tt>''' slot. We'll move the shot every time the timer times out. |
| ↓...4 Ruby Tutorial/Chapter 11/15/fi | The '''<tt>timerCount</tt>''' keeps track of the time passed since the shot was fired. The '''<tt>shootAngle</tt>''' is the cannon angle and '''<tt>shootForce</tt>''' is the cannon force when the shot was fired. |
| ↓...4 Ruby Tutorial/Chapter 11/16/fi | This function shoots a shot unless a shot is in the air. The '''<tt>timerCount</tt>''' is reset to zero. The '''<tt>shootAngle</tt>''' and '''<tt>shootForce</tt>''' variables are set to the current cannon angle and force. Finally, we start the timer. |
| ↓...4 Ruby Tutorial/Chapter 11/17/fi | '''<tt>moveShot()</tt>''' is the slot that moves the shot, called every 5 milliseconds when the [http://doc.qt.nokia.com/latest/qtimer.html Qt::Timer] fires. |
| ↓...4 Ruby Tutorial/Chapter 11/18/fi | Its tasks are to compute the new position, update the screen with the shot in the new position, and if necessary, stop the timer. |
| ↓...4 Ruby Tutorial/Chapter 11/19/fi | First we make a [http://doc.qt.nokia.com/latest/qregion.html Qt::Region] that holds the old '''<tt>shotRect()</tt>'''. A [http://doc.qt.nokia.com/latest/qregion.html Qt::Region] is capable of holding any sort of region, and we'll use it here to simplify the painting. '''<tt>shotRect()</tt>''' returns the rectangle where the shot is now. It is explained in detail later. |
| ↓...4 Ruby Tutorial/Chapter 11/20/fi | Then we increment the '''<tt>timerCount</tt>''', which has the effect of moving the shot one step along its trajectory. |
| ↓...4 Ruby Tutorial/Chapter 11/21/fi | Next we fetch the new shot rectangle. |
| ↓...4 Ruby Tutorial/Chapter 11/22/fi | If the shot has moved beyond the right or bottom edge of the widget we stop the timer, or we add the new '''<tt>shotRect()</tt>''' to the [http://doc.qt.nokia.com/latest/qregion.html Qt::Region]. |
| ↓...4 Ruby Tutorial/Chapter 11/23/fi | Finally, we repaint the [http://doc.qt.nokia.com/latest/qregion.html Qt::Region]. This will send a single paint event for just the one or two rectangles that need updating. |
| ↓...4 Ruby Tutorial/Chapter 11/24/fi | The paint event function has been simplified since the previous chapter. Most of the logic has been moved to the new '''<tt>paintShot()</tt>''' and '''<tt>paintCannon()</tt>''' functions. |
| ↓...4 Ruby Tutorial/Chapter 11/25/fi | This private function paints the shot by drawing a black filled rectangle. |
| ↓...4 Ruby Tutorial/Chapter 11/26/fi | We leave out the implementation of '''<tt>paintCannon()</tt>'''; it is the same as the [http://doc.qt.nokia.com/latest/qwidget.html#paintEvent Qt::Widget::paintEvent()] reimplementation from the previous chapter. |
| ↓...4 Ruby Tutorial/Chapter 11/27/fi | This private function calculates the center point of the shot and returns the enclosing rectangle of the shot. It uses the initial cannon force and angle in addition to '''<tt>timerCount</tt>''', which increases as time passes. |
| ↓...4 Ruby Tutorial/Chapter 11/28/fi | The formula used is the standard Newtonian formula for frictionless movement in a gravity field. For simplicity, we've chosen to disregard any Einsteinian effects. |
| ↓...4 Ruby Tutorial/Chapter 11/29/fi | We calculate the center point in a coordinate system where y coordinates increase upward. After we have calculated the center point, we construct a [http://doc.qt.nokia.com/latest/qrect.html Qt::Rect] with size 6 x 6 and move its center point to the point calculated above. In the same operation we convert the point into the widget's coordinate system (see [http://doc.qt.nokia.com/latest/coordsys.html The Coordinate System]). |
| ↓...4 Ruby Tutorial/Chapter 11/30/fi | The only addition is the <strong>Shoot</strong> button. |
| ↓...4 Ruby Tutorial/Chapter 11/31/fi | In the constructor we create and set up the <strong>Shoot</strong> button exactly like we did with the <strong>Quit</strong> button. |
| ↓...4 Ruby Tutorial/Chapter 11/32/fi | Connects the '''<tt>clicked()</tt>''' signal of the <strong>Shoot</strong> button to the '''<tt>shoot()</tt>''' slot of the '''<tt>CannonField</tt>'''. |
| ↓...4 Ruby Tutorial/Chapter 11/33/fi | === Sovelluksen suorittaminen === |
| ↓...4 Ruby Tutorial/Chapter 11/34/fi | Kanuuna voi laukaista, mutta ei ole mitään mitä ampua. |
| ↓...4 Ruby Tutorial/Chapter 11/35/fi | === Harjoitukset === |
| ↓...4 Ruby Tutorial/Chapter 11/36/fi | Tee laukauksesta täytetty ympyrä. [Vihje: [http://doc.qt.nokia.com/latest/qpainter.html#drawEllipse Qt::Painter::drawEllipse()] saattaa auttaa.] |
| ↓...4 Ruby Tutorial/Chapter 11/37/fi | Vaihda kanuunan väriä kun laukaus on ilmassa. |
| ↓...4 Ruby Tutorial/Chapter 11/38/fi | [[Category:Ruby]] |