Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am trying to make an app with QML for Ubuntu Touch that is similar to the game Avalanche, and am unsure how to go about inserting a varying number of falling blocks. All of the blocks will be the same picture, but will have different sizes and IDs. The blocks will also be deleted when they go off screen (so they don't fill up any more RAM to store their sizes, IDs, etc. when they are no longer needed). Here is the template for the blocks that I would like to use:

Image {
    id: block0 //The first block will be 0, the second 1, the third 2, and so on
    source: "block.png"
    height: units.gu(Math.floor(Math.random) * 5 + 1)
    width: height
    x: units.gu(Math.floor(Math.random() * (75 - width)))
}

So, how can I have this code snippet be written to and deleted from the QML file when the game is played? I would like to do this with JavaScript so it will be easier to make the IDs and store them in an array to easily access information. Sorry if this is difficult to understand, and thanks for the help!

share|improve this question
    
Look at the QML samegame code. You should be able to use most of that. The only difference is size randomization, which it looks like you have figured out. –  hbdgaf Jul 3 '13 at 2:25

2 Answers 2

up vote 2 down vote accepted

You can dynamically create elements from the code. Check this out:

http://qt-project.org/wiki/QML-Dynamic-Objects

share|improve this answer

I have done something similar in one of my game. Simplified code is explained in following post

http://kunalmaemo.blogspot.com/2011/04/creating-qml-element-dynamically-on.html

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.