Simple problem

Hi can someone please tell me why this does not work:

var $number:Number = 123;
button_0_mc.onPress = function($number)
{
description_txt.text = $number;
}

the description box will show “undefined”

However this does work, the description will show “123”

button_0_mc.onPress = function($number)
{
var $number:Number = 123;
description_txt.text = $number;
}

I need to be able to pass variables to my onpress function.

Thanks,

Matt.

[quote=matt_nz;2343974]Hi can someone please tell me why this does not work:

var $number:Number = 123;
button_0_mc.onPress = function($number)
{
description_txt.text = $number;
}

the description box will show “undefined”

However this does work, the description will show “123”

button_0_mc.onPress = function($number)
{
var $number:Number = 123;
description_txt.text = $number;
}

I need to be able to pass variables to my onpress function.

Thanks,

Matt.[/quote]

whats wrong with just:


var num:Number = 123;
button_0_mc.onPress = function()
{
    trace(num);
};

Seems like your coding from a php perspective.

[COLOR=#000000]var[/COLOR] number:[COLOR=#0000FF]Number[/COLOR] = [COLOR=#000080]123[/COLOR];
button_[COLOR=#000080]0[/COLOR]_mc.[COLOR=#0000FF]onPress[/COLOR] = [COLOR=#000000]function[/COLOR]COLOR=#000000[/COLOR]
[COLOR=#000000]{[/COLOR]
description_txt.text =[COLOR=#000000][/COLOR] num[COLOR=#000000][/COLOR]ber;
[COLOR=#000000]}[/COLOR];

[quote=motionman95;2343980]Seems like your coding from a php perspective.

[COLOR=#000000]var[/COLOR] number:[COLOR=#0000ff]Number[/COLOR] = [COLOR=#000080]123[/COLOR];
button_[COLOR=#000080]0[/COLOR]_mc.[COLOR=#0000ff]onPress[/COLOR] = [COLOR=#000000]function[/COLOR]COLOR=#000000[/COLOR]
[COLOR=#000000]{[/COLOR]
description_txt.text = number;
[COLOR=#000000]}[/COLOR];[/quote]

Yes I am a php guy (I guess the "$"variable gave that away), I find that bizzare, if I do not pass the paramater then it works fine, but if I do pass the paramater it does not work.

Thanks, for the help.

That worked ok for my simple example with numbers, but what I really want to do is this:

description[0] = “description 1”;
description[1] = “description 2”;

button[0] = _root[‘button_0_mc’];
button[1] = _root[‘button_1_mc’];

for(i=0; i<2; i++)
{

$button*.onPress = function() 
{
    var fade_out:Tween = new Tween(big_image_mc, "_alpha", easeOut, 100, 0, 1, true);
    description_txt.text = $description*;
};

}

I still get undefined for the description, although the fade_out works ok, can someone help me please, Thanks.

Give this a whirl:


import mx.transitions.Tween;
import mx.transitions.easing.*;

var description:Array = ["description 1", "description 2"];


for (i = 0; i < description.length; i++)
{
    this["button" + i].id = i;
    this["button" + i].onRelease = function()
    {
        var fade_out:Tween = new Tween(big_image_mc, "_alpha", easeOut, 100, 0, 1, true);
        traceDescription(this.id);
    };
}
function traceDescription(itemID)
{
    trace(description[itemID]);
    description_txt.text = description[itemID];
}

note: I renamed your button to simply button0, button1 etc…

Thanks dude ,that worked awesome, I just cut my actionscript by about 40 lines, I ended up doing this which is pretty much what you suggested:

for (i = 0; i < description.length; i++)
{
//make the thumbnail buttons
root.slider_mc["button" + i + “_mc”].id = i;
root.slider_mc["button" + i + “_mc”].onRelease = function()
{
var fade_out:Tween = new Tween(big_image_mc, “_alpha”, easeOut, 100, 0, 1, true);
setTimeout(traceDescription, 1000, this.id);
};
}

function traceDescription(itemID)
{
description_txt.text = description[itemID];
big_image_mc.loadMovie(big_path[itemID]);
var fade_in:Tween = new Tween(big_image_mc, “_alpha”, easeOut, 0, 100, 1, true);
}

So my gallery is now only 4kb! (+ images and xml)

What exactly does this code do?

can you use Tween and ease properties?

an image gallery:

the code gets an xml file with a list of image nodes, each image has a thumbnail path, a big image path, and a description.

The swf gets the xml file and makes 3 arrays, an array of the descriptions, an array of the thumb paths, an array of the big paths. The arrays are such that thumb[0] and description[0] and big[0] all relate to the same image.

I loop through the thumbs path array and put all the thumbs in place.
I put a bunch of invisible buttons over the thumbs, and put the button names into an array.

when button[0] is pressed (which is over thumb[0]) big[0] is displayed in the main window and description[0] is displayed below it.

Full code:(let me know if u have any q’s)

import mx.transitions.Tween;
import mx.transitions.easing.*;

var theXML:XML = new XML();
theXML.ignoreWhite = true;

var big_path:Array = new Array();
var thumb_path:Array = new Array();
var description:Array = new Array();

theXML.onLoad = function()
{

var images:Array = this.firstChild.childNodes;
var i:Number = 0;

    //make the three arrays
    for(i = 0; i &lt; images.length; i++)
        {
            thumb_path.push(images*.attributes.thumb_path);
            big_path.push(images*.attributes.big_path);
            description.push(images*.attributes.description);
        }

//make the array thumbnail holders
var thumbs:Array = new Array();
for (i = 0; i < description.length; i++)
{
thumbs*=root.slider_mc["thumb"+ i + “_mc”];
}

for (i = 0; i < description.length; i++)
{
root.slider_mc["button" + i + “_mc”].id = i;
root.slider_mc["button" + i + “_mc”].onRelease = function()
{
var fade_out:Tween = new Tween(big_image_mc, “_alpha”, easeOut, 100, 0, 1, true);
setTimeout(traceDescription, 1000, this.id);
};
}

function traceDescription(itemID)
{
description_txt.text = description[itemID];
big_image_mc.loadMovie(big_path[itemID]);
var fade_in:Tween = new Tween(big_image_mc, “_alpha”, easeOut, 0, 100, 1, true);
}

var $time:Number = 0.2;

//put the images into the thumb holders
for(i = 0; i &lt; 10; i++)
{
    thumbs*.loadMovie(thumb_path*);
    var fade_in:Tween = new Tween(thumbs*, "_alpha", easeOut, 0, 100, $time, true);
    $time = $time + 0.2;
}

big_image_mc.loadMovie(big_path[0]);
var fade_in:Tween = new Tween(big_image_mc, "_alpha", easeOut, 0, 100, .5, true);
description_txt.text = description[0];


arrow1_mc.onPress = function()
{
    var slide:Tween = new Tween(slider_mc, "_y", easeOut, slider_mc._y, (slider_mc._y + 90), 1, true);
}

arrow2_mc.onPress = function()
{
    var slide:Tween = new Tween(slider_mc, "_y", easeOut, slider_mc._y, (slider_mc._y - 90), 1, true);
}

}
theXML.load(“xml/gallery.xml”);

glad you got it workin :slight_smile: