Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
New Developer
MedNez
Posts: 67
Registered: ‎11-24-2008
My Device: Not Specified

Basic cascades question - functions and referencing elements

Hello!

 

Just a basic question in Cascades -- hoping someone can help!

 

Consider the following code:

 

Label

{

    id: firstlabel

    text: "Hi I am the first label"

}

Label

{

    id: secondlabel

    text: "Hi I am the second label"

}

 

Label

{

    id: thirdlabel

    text: "Hi I am the third label"

}

 

Now let's say I generate a random number between 1 and 3, and pass it into a function which should modify the text of the label that is being referenced:

 

function changeMyText( num )

{

    if(num == 1)

    {

      // update text 1..

    }

    else if(num == 2... etc

}

 

Is there an easier way to do that?

 

Can I pass a label into a function, or something similiar, so the function could just set theLabelThatWasPassedIn.text? 

 

Alternatively, is it possible to iterate through all elements of a particular match (kind of like selectors in jquery) and cycle through them, within a function? 

 

 

Thanks!

 

 

Med

Please use plain text.
Developer
peter9477
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Basic cascades question - functions and referencing elements

There are various ways to do something like what you want, but which is best depends on what you really want to do. The description leaves unanswered various questions, such as do you want to do this once, or repeatedly? When you change the text of the label, what will you be changing it to? Will it change back to the original content when the next label is changed? Things like that.

With QML, there are quite simple declarative ways to do many things which one's first inclination may be to do procedurally. Whether those are available here depend on more info than you've given.

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Please use plain text.
New Developer
MedNez
Posts: 67
Registered: ‎11-24-2008
My Device: Not Specified

Re: Basic cascades question - functions and referencing elements

Sure!

 

So I'm making (or have made, but looking to refactor and increase my cascades knowledge), a game. 

 

I have a whole bunch of objects, similiar to the following:

 

ImageView
{
    id: item1
    property int changed: 0
    opacity: 0.0
    imageSource: "asset:///images/a.png" 
    layoutProperties: AbsoluteLayoutProperties {
        positionX: 10
        positionY: 10  
    }  
    animations: [       		            
        AnimationPlay {
            id: item1play
        },
        AnimatioDie {
            id: item1die
        }
    ]
    gestureHandlers: [
        TapHandler {
            onTapped: {
                increaseScore();
                item1.imageSource = "asset:///images/b.png"
                item1.changed = someVariable
                item1play.stop();
                item1die.play();
            }
        }
    ]
}

I guess I have three questions about how to specifically deal with referencing.

 

Question 1: Custom Objects

 

Is there a better way, in QML, rather than having that in my code 5/10/20 times for each item, but changing item1 to item2, item3, etc... to create a custom object that would significantly reduce the code? I can see how to make a custom object that would genericise the property, opacity, imagesource etc, but is there a way to also add the animation and gesturehandlers into there as well? 

 

The problem I see is that in the tap handler, the code references it's own animations/image sources etc, but I don't know how that would work if I made a custom object for an item.

 

Question 2: Iterating through the objects

 

Every second I call a function to reset items:

 

function everySecond()
{
    if(item1.changed > 0 && (item1.changed - someVariable >= someNumber))
    {
        item1.imageSource = 'asset:///images/a.png'
        item1.changed = 0
    } 
	
    // etc

}

 

The problem here is that if there are 10 items, there are 10 if statements. Works fine, but if I was to do this again with 100 objects, this would be massive pain. Is there a better way to reference all the item objects? (as in code sample 1)

 

Question 3: Picking an item at random

 

At the moment I also do (a lot more) if statements for particular parts of my code.

 

tl;dr If there are 10 items, I generate a number between 1 and 10, then pass that number to a function. If the number is say, 4, then it plays the animation for item 4:

 

function playItem(itemNum)
{
	if(itemNum == 1)
	{
		if(item1.changed > 0)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	else if(itemNum == 2)
	{
		// etc
	}
}

 

Again, if I were to have 100 items I would have to write 100 statements. Is there a better way to "pick" an item at random, then pass it to a function which might look something like this?

 

function playItem(item)
{
	if(item.changed > 0)
	{
		return false;
	}
	else
	{
		return true;
	}
	
}

 

So you could just pass in the item, rather than passing in a number and then if/elsing to change a particular item...

 

 

I have a lot to learn in cascades, I've hit a code-efficiency roadblock!

 

 

Thank you!

 

Please use plain text.