[SailfishDevel] Refrence dynamically created QML object?
Peter Pykäläinen
peter.pykalainen at gmail.com
Mon Oct 6 18:00:32 UTC 2014
Thank you Adam!
Almost there, I think I nailed it with storing the objects in a javascript
array and then destroying them once not needed anymore.
Still needs a lot of polishing and testing.
Thanks to all of you for your great help!
// Peter Pykäläinen
On 6 October 2014 20:57, Adam Pigg <adam at piggz.co.uk> wrote:
> I have this kind of code in a .js file, referenced from the .qml file:
>
> var aliens = new Array(10*5);
>
>
> //Create all aliens and position them on the board in rows
>
> function createAliens() {
>
> //Row 1
>
> component = Qt.createComponent("alien1.qml");
>
> var y = topline.y + topline.height + Sizer.alienYGap();
>
> for (var i=0; i<10; i++) {
>
> aliens[i] = component.createObject(board);
>
> aliens[i].x = ((aliens[i].width + Sizer.alienXGap()) * i) + Sizer.alienXGap();
>
> aliens[i].y = y;
>
> aliens[i].objectName = "alien_" + i;
>
> }
>
>
> etc etc etc
>
>
> }
>
>
> and then i might want to check a collision against all items in the array:
>
>
> function checkBulletAlienCollision()
>
> {
>
> //Loop over each alien, checking if the bullet intersects
>
>
> for (var i=0; i<50; i++) {
>
> if (bullet && aliens[i]) { //Check if objects exist first
>
> if (bullet.x > aliens[i].x && bullet.x < aliens[i].x + aliens[i].width) {
>
> if ((bullet.y > aliens[i].y || bullet.y + bullet.height > aliens[i].y) && bullet.y < aliens[i].y + aliens[i].height) {
>
> playSound("killed");
>
>
> score += aliens[i].pointsAwarded;
>
> scoretext.text = "Score: " + score;
>
> bullet.y = -bullet.height; //Move the bullet offscreen
>
>
> explosion.x = aliens[i].x - (explosion.width - aliens[i].width) /2;
>
> explosion.y = aliens[i].y - (explosion.height - aliens[i].height) /2;
>
>
> aliens[i].destroy();
>
> aliens[i] = null;
>
>
> explosion.play();
>
> if (alienanimation.interval > 100) { //Get faster when a alien is killed
>
> alienanimation.interval -= 10;
>
> }
>
> break;
>
> }
>
> }
>
> }
>
> }
>
> }
>
>
>
> On Mon, Oct 6, 2014 at 9:04 AM, Peter Pykäläinen <
> peter.pykalainen at gmail.com> wrote:
>
>> Thanks Andrey!
>> Will test that too tonight.
>>
>>
>> // Peter Pykäläinen
>>
>> On 6 October 2014 10:39, Andrey Kozhevnikov <coderusinbox at gmail.com>
>> wrote:
>>
>>> use
>>>
>>> property Item object1
>>> property Item object2
>>>
>>> ...
>>>
>>> function createObjects() {
>>> object1 = component1.createObject(parent, paraneters)
>>> object2 = component2.createObject(parent, paraneters)
>>> checkCollisionsFunction(object1, object2)
>>> }
>>>
>>> 06.10.2014 12:26, Peter Pykäläinen пишет:
>>>
>>> Hi,
>>>
>>> I have a problem with a dynamically created QML object and referencing
>>> it.
>>> The problem is as follows:
>>>
>>> - Create a dynamic QML object with createObject, assign signal to it.
>>> This works fine.
>>> - Create another dynamic QML object.
>>> - I would need to check collision of these two new objects, I am using
>>> a timer to check that.
>>> - Now since I can not reference both objects with their IDs I can't get
>>> the collision check to work.
>>>
>>> Any ideas how to achive this?
>>>
>>> I have tried to use a LlistModel to store all my objects, but looping
>>> through them with JavaScript slows down my application significantly, so
>>> this is not an option.
>>>
>>> // Peter Pykäläinen
>>>
>>>
>>> _______________________________________________
>>> SailfishOS.org Devel mailing list
>>> To unsubscribe, please send a mail to devel-unsubscribe at lists.sailfishos.org
>>>
>>>
>>>
>>> _______________________________________________
>>> SailfishOS.org Devel mailing list
>>> To unsubscribe, please send a mail to
>>> devel-unsubscribe at lists.sailfishos.org
>>>
>>
>>
>> _______________________________________________
>> SailfishOS.org Devel mailing list
>> To unsubscribe, please send a mail to
>> devel-unsubscribe at lists.sailfishos.org
>>
>
>
> _______________________________________________
> SailfishOS.org Devel mailing list
> To unsubscribe, please send a mail to
> devel-unsubscribe at lists.sailfishos.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.sailfishos.org/pipermail/devel/attachments/20141006/de2abfb6/attachment-0001.html>
More information about the Devel
mailing list