# Simple problem

**URL:** <https://forum.kirupa.com/t/simple-problem/263537>\
**Category:** Uncategorized\
**Created:** [June 16, 2008, 11:54pm UTC](https://forum.kirupa.com/t/simple-problem/263537 "2008-06-16T23:54:51Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt\_nz](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@matt\_nz](https://forum.kirupa.com/u/matt_nz)\
**Post date:** [June 16, 2008, 11:54pm UTC](https://forum.kirupa.com/t/simple-problem/263537/1 "2008-06-16T23:54:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rondog](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/rondog/32/2478_2.png) [@rondog](https://forum.kirupa.com/u/rondog)\
**Post date:** [June 17, 2008, 12:02am UTC](https://forum.kirupa.com/t/simple-problem/263537/2 "2008-06-17T00:02:28Z")

</div>

[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:

```auto

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

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 17, 2008, 12:03am UTC](https://forum.kirupa.com/t/simple-problem/263537/3 "2008-06-17T00:03:47Z")

</div>

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];

---

<div class="post-metadata">

**Author:** ![matt\_nz](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@matt\_nz](https://forum.kirupa.com/u/matt_nz)\
**Post date:** [June 17, 2008, 12:15am UTC](https://forum.kirupa.com/t/simple-problem/263537/4 "2008-06-17T00:15:25Z")

</div>

[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.

---

<div class="post-metadata">

**Author:** ![matt\_nz](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@matt\_nz](https://forum.kirupa.com/u/matt_nz)\
**Post date:** [June 17, 2008, 12:33am UTC](https://forum.kirupa.com/t/simple-problem/263537/5 "2008-06-17T00:33:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rondog](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/rondog/32/2478_2.png) [@rondog](https://forum.kirupa.com/u/rondog)\
**Post date:** [June 17, 2008, 6:30am UTC](https://forum.kirupa.com/t/simple-problem/263537/6 "2008-06-17T06:30:19Z")

</div>

Give this a whirl:

```auto

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…

---

<div class="post-metadata">

**Author:** ![matt\_nz](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@matt\_nz](https://forum.kirupa.com/u/matt_nz)\
**Post date:** [June 17, 2008, 9:47am UTC](https://forum.kirupa.com/t/simple-problem/263537/7 "2008-06-17T09:47:17Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![iMike91](https://avatars.discourse-cdn.com/v4/letter/i/2bfe46/32.png) [@iMike91](https://forum.kirupa.com/u/iMike91)\
**Post date:** [June 17, 2008, 10:39am UTC](https://forum.kirupa.com/t/simple-problem/263537/8 "2008-06-17T10:39:15Z")

</div>

What exactly does this code do?

can you use Tween and ease properties?

---

<div class="post-metadata">

**Author:** ![matt\_nz](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@matt\_nz](https://forum.kirupa.com/u/matt_nz)\
**Post date:** [June 17, 2008, 11:06am UTC](https://forum.kirupa.com/t/simple-problem/263537/9 "2008-06-17T11:06:35Z")

</div>

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”);

---

<div class="post-metadata">

**Author:** ![rondog](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/rondog/32/2478_2.png) [@rondog](https://forum.kirupa.com/u/rondog)\
**Post date:** [June 17, 2008, 4:28pm UTC](https://forum.kirupa.com/t/simple-problem/263537/10 "2008-06-17T16:28:44Z")

</div>

glad you got it workin 🙂
