MyShip.qml 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * Copyright (c) 2011 Nokia Corporation.
  3. */
  4. import QtQuick 1.0
  5. import "Game.js" as GameScript
  6. Item {
  7. id: myShip
  8. objectName: "myShip"
  9. property variant myShipSize
  10. property int originalY
  11. /**
  12. * Fires missile if the ship exists.
  13. */
  14. function fire()
  15. {
  16. if (myShip.opacity == 1)
  17. {
  18. GameScript.fireMissile(myShip.x + myShip.width / 2,
  19. myShip.y, myShip.height * -1);
  20. if (myShip.y + myShip.height < gameArea.yDim + 5) {
  21. goDownAnim.restart();
  22. }
  23. }
  24. }
  25. /**
  26. * Sets the graphics and the position of the ship.
  27. */
  28. function createGraphicsForLevel()
  29. {
  30. if (LevelPlugin) {
  31. myShip.myShipSize =
  32. LevelPlugin.graphSize(LevelPlugin.pathToMyShipPic());
  33. myShip.height = myShipSize.height;
  34. myShip.width = myShipSize.width;
  35. image.source = "file:/" + LevelPlugin.pathToMyShipPic();
  36. myShip.y = gameArea.yDim - myShip.height - 10;
  37. originalY = myShip.y;
  38. myShip.x = (gameArea.xDim - myShip.width) / 2;
  39. }
  40. }
  41. // The visual representation of the ship. Initialized by
  42. // createGraphicsForLevel() function.
  43. Image {
  44. id: image
  45. smooth: true
  46. }
  47. // Handle keys
  48. Keys.onSpacePressed: fire();
  49. Keys.onSelectPressed: fire();
  50. Keys.onRightPressed: {
  51. if (myShip.x < (gameArea.xDim - myShip.width - 20)) {
  52. toRightAnim.restart();
  53. }
  54. }
  55. Keys.onLeftPressed: {
  56. if (myShip.x > 20) {
  57. toLeftAnim.restart();
  58. }
  59. }
  60. // To right animation
  61. PropertyAnimation {
  62. id: toRightAnim
  63. target: myShip
  64. easing.type: Easing.OutQuint
  65. properties: "x"
  66. from: myShip.x; to: myShip.x + 20
  67. duration: 500
  68. }
  69. // To left animation
  70. PropertyAnimation {
  71. id: toLeftAnim
  72. target: myShip
  73. easing.type: Easing.OutQuint
  74. properties: "x"
  75. from: myShip.x; to: myShip.x - 20
  76. duration: 500
  77. }
  78. // Go down on fire animation
  79. SequentialAnimation {
  80. id: goDownAnim
  81. NumberAnimation {
  82. target: myShip
  83. property: "y"
  84. from: myShip.y; to: myShip.y + 5
  85. easing.type: Easing.Linear
  86. duration: 200
  87. }
  88. NumberAnimation {
  89. target: myShip
  90. property: "y";
  91. from: myShip.y; to: originalY
  92. easing.type: Easing.Linear
  93. duration: 200
  94. }
  95. }
  96. }