PDA

View Full Version : passing variable values from one method to another?


lyndonwong
10-06-2003, 09:05 PM
Hi Antun,

In my 'simple slideshow', I have successfully invoked a method 'getImageCount' to determine the number of images in the show sequence, but I can not figure out how to pass that variable to the 'changePhoto' method, where I hope to use the 'imageCount' variable to halt the show or restart from the beginning.

Can you tell what I'm doing wrong from this code?

---------------------
<canvas width="584" height="584" debug='true'>
<debug width='300' height='200' x='200' fontsize='12'/>

<!-- RESOURCES -->
<font name="serif" src="timmonsr.ttf" />
<font name="san" src="helmetr.ttf" />

<!-- App parameter values in an external XML file -->
<dataset name="slideshow_list" src="slideshow_list.xml" />

<!-- APP INTERFACE -->

<!-- Widget border created via two nested views -->
<view x="3" y="3" width="180" height="180" bgcolor="gray" >

<view width="178" height="178" x="1" y="1" bgcolor="white" datapath="slideshow_list:/slideshow/" oninit='this.setDelay()'>

<attribute name="slide_number" type="number" value="1" />

<!-- Set the datapointer of this view to images node for getImageCount method -->
<datapointer id="imagesNode" xpath="slideshow_list:/slideshow/images[1]"/>

<method name="changePhoto">
this.getImageCount(count);
this.slide_number++;
var dp = 'slideshow_list:/slideshow/images/image['
+ this.slide_number + ']';
this.slidewindow.setDatapath( dp );
this.setDelay();

Debug.Write (this.count + 10);
</method>

<method name="setDelay">
this.photoDelay = new LzDelegate( this, 'changePhoto' );
LzTimer.addTimer( this.photoDelay, 2000 );
</method>

<method name="getImageCount" args='count'>
var imageCount=imagesNode.getNodeCount();
output.setText( imageCount );
return count=imageCount;
</method>

<simplelayout axis="y" />
<text x="29" y="2" height="22" font="san" fontsize="16" width="170" fgcolor='green' datapath='title/text()' />

<!-- Display slideshow image data -->
<view name='slidewindow' x="29" width="170" clip="true" font="serif" fontsize="12" datapath="images/image[1]/">
<simplelayout axis="y" />
<view name="photo" width="120" height="90" stretches="both" datapath="path/text()">
<method event="ondata">
this.setSource( data );
</method>
</view>

<text name='imgTitle' fontsize="14" fgcolor='green' width="166" datapath="caption/text()" />
<text name='imgPathValue' fontsize="12" fgcolor='gray' width="166" datapath="path/text()" />
</view>
<text name='output' width='160'>Debugging data field.</text>
</view>
</view>
</canvas>

--------------------------------------

HERE'S THE XML FILE WHERE THE DATAPOINTER IS SET TO THE <images> node:

<slideshow>
<title>Southeast Asia</title>
<author>Lyndon W. Wong</author>
<images>
<image>
<caption>An infant</caption>
<path>img01.jpg</path>
</image>
<image>
<caption>Infant's mother</caption>
<path>img02.jpg</path>
</image>
<image>
<caption>Toraja Grandma</caption>
<path>img03.jpg</path>
</image>
<image>
<caption>Hmong Kids</caption>
<path>img04.jpg</path>
</image>
<image>
<caption>Toraja Farmer</caption>
<path>img05.jpg</path>
</image>

</images>
</slideshow>

antun
10-06-2003, 09:26 PM
I'm confused as to why you're passing an argument into the getImageCount method - it's not really necessary as far as I can see. If a method returns a value, you can assign it as follows:


<method name="changePhoto">
<![CDATA[
var imageCount = this.getImageCount();
this.slide_number++;
if ( this.slide_number > imageCount ) {
this.slide_number = 0;
this.setDelay();
return;
}

var dp = 'slideshow_list:/slideshow/images/image['
+ this.slide_number + ']';
this.slidewindow.setDatapath( dp );
this.setDelay();
]]>
</method>


You could also shorten the if statement to:


if ( this.slide_number > this.getImageCount() )
....


-Antun

PS Check out this on formatting in these forums - they let you preserve white space:

http://www.laszlosystems.com/developers/community/forums/misc.php?action=bbcode#buttons