Question

Trying to build a little fotball game as an project in school but I'm having some issues. So when I run the code it says that ReferenceError: Screen is not defined, but accordign to me I have defined it.

This code is just a prototype, going to change the keys to buttons later on so that it can actually work on a phone.

import QtQuick 2.0

Item {
    id:root

    width:Screen.width
    height:Screen.height-10
    focus:true

    Keys.onPressed: {

        if(event.key===Qt.Key_Up)
        {
            event.accepted = true;
            player.y=(player.y) - 40

        }
        if(event.Key === Qt.Key_Down){
            event.accepted = true;
            player.y = (player.y)+ 40
           }
        if (event.key === Qt.Key_Right)
         { event.accepted=true;
            player.x=(player.x)-40

        }
   if (event.key === Qt.Key_Left)
{event.accepted = true;
       player.x=(player.x) +40
   }

    }

Flickable {
    width:Screen.width
    height:Screen.height
    contentHeight: Screen.height*4
    contentWidth:Screen.width
    interactive:true
    boundsBehavior: Flickable.StopAtBounds

    Image{
        id: feild
        anchors.fill:parent
        source:"Namnlös.png"
        sourceSize.height:Screen.height*4
        sourceSize.width:Screen.width
        }

    Image {
        id: player
        source:"asd.png"
        x:Screen.width/2
        y:Screen.height/2
    }
}

}

So if you run this code you'll only get the player showing up, and then disapear instantly, the field is not shown.

Was it helpful?

Solution

You lack the Screen import.

import QtQuick.Window 2.1

Screen docs

OTHER TIPS

Resizing items to screen is abnormal, you should simply use resizeMode property and anchor all child items inside root item.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top