Penguin.qml Example File

demos/RubeSidescroller/qml/entities/Penguin.qml
 import QtQuick 2.0
 import Felgo 3.0

 RubeBody {
   id: penguin
   entityType: "penguin"

   // force applied to the body, drives the penguin forward (always) and up (when touched)
   force: Qt.point(60, isMouseDown ? -300 : 0)

   // torque to achieve the target rotation = deltaRotation * factor
   // this is like a proportional controller (see PID controller)
   torque: (targetRotation - rotation) * 60

   // target rotation depends on the vertical velocity
   property real targetRotation: linearVelocity.y / 6

   // current touch state
   property bool isMouseDown: false

   onXChanged: {
     // move "camera" of scene so that the penguin is 1/3 from the left screen border
     scene.level.x = -x + scene.width / 3

     // test until endless scrolling is enabled
     if (x > 3200){
       reset()
     }
   }

   onInitialized: {
     reset()
   }

   onBeginContact: {
     var entityType = other.getBody().target.entityType;
     if (entityType === "saw"){
       reset()
     }
   }

   Connections {
     target: scene.mouseArea
     onPressed: penguin.isMouseDown = true
     onReleased: penguin.isMouseDown = false
   }

   // resets pengiun position to start-position
   function reset(){
     var resetPos = Qt.point(0, 0);

     // only one startPos can exist
     var startPosEntities = entityManager.getEntityArrayByType("startPos")
     if (startPosEntities.length > 0 && startPosEntities[0]) {
       resetPos = Qt.point(startPosEntities[0].x, startPosEntities[0].y)
     }

     x = resetPos.x
     y = resetPos.y
     linearVelocity = Qt.point(0,0)

     rotation = 0
     angularVelocity = 0
   }
 }

Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
FREE!
create apps
create games
cross platform
native performance
3rd party services
game network
multiplayer
level editor
easiest to learn
biggest time saving
best support