PDA

View Full Version : converting a movieclip toString



davelk
September 30th, 2008, 03:35 PM
Hi, I am making a map with tooltips that are triggered by invisible movieclips sitting on top of each continent.

I know I could do this by referencing each instance and giving it it's own event listener but I'm trying to learn how to do things more dynamically.

heres the code so far...I need a variable that holds a string for the country I currently have the mouse over. Then I can obviously use that variable to send the right text to the textfield within my tooltip movieclip.

Thanks in advance guys, please talk to me like an I'm stupid and I'll stand a better chance of understanding you



//hide tooltip
toolTip.visible = false;
//make tooltip follow cursor
addEventListener(Event.ENTER_FRAME, followCursor);
function followCursor(e:Event):void
{
toolTip.x = mouseX;
toolTip.y = mouseY;
}
//create array to hold transparent movieclips/hit areas
var countries:Array = new Array();
countries.push(africa);
countries.push(asia);
countries.push(europe);
countries.push(oceania);
countries.push(northAmerica);
countries.push(southAmerica);

//----This was a test to see if I could convert the instance name of an mc to string
//----but the textfield just displays '[object movieclip]'

var currentCountry:String = countries[0].toString();

function doRollOver(e:MouseEvent):void{
toolTip.visible = true;
toolTip.toolText.text = currentCountry;
}
function doRollOut(e:MouseEvent):void{
toolTip.visible = false;
}
//assign listeners to each movieclip in the countries array
function init():void
{
var i=0;
var hitArea;
for (i=0; i<countries.length; i++){
area = countries[i];
area.addEventListener(MouseEvent.MOUSE_OVER, doRollOver);
area.addEventListener(MouseEvent.MOUSE_OUT, doRollOut);
//area.addEventListener(MouseEvent.MOUSE_OVER, doClick);
}
}
init();

davelk
September 30th, 2008, 04:16 PM
ok worked it out. Bit embarressed now it wasn't that difficult


var currentCountry;

function doRollOver(e:MouseEvent):void{
toolTip.visible = true;
currentCountry = e.target.name;
toolTip.toolText.text = currentCountry;
}

r3dkrpto
September 30th, 2008, 04:17 PM
If you have the name property set for the country movieclips, just use africa.name for instance to get the name of the country. I doubt toString() is defined for the movieclips which is why you're just getting [object movieclip]. A movieclip has a name property which is the instance name so if you gave each country an instance name, you can grab that by using [instance].name, like africa.name.

In your doRollover event handler, you should be able to just write:
toolTip.toolText.text = Movieclip(e.target).name to get the instance name of the movieclip that the mouse is over.

Hope that helps.


Hi, I am making a map with tooltips that are triggered by invisible movieclips sitting on top of each continent.

I know I could do this by referencing each instance and giving it it's own event listener but I'm trying to learn how to do things more dynamically.

heres the code so far...I need a variable that holds a string for the country I currently have the mouse over. Then I can obviously use that variable to send the right text to the textfield within my tooltip movieclip.

Thanks in advance guys, please talk to me like an I'm stupid and I'll stand a better chance of understanding you



//hide tooltip
toolTip.visible = false;
//make tooltip follow cursor
addEventListener(Event.ENTER_FRAME, followCursor);
function followCursor(e:Event):void
{
toolTip.x = mouseX;
toolTip.y = mouseY;
}
//create array to hold transparent movieclips/hit areas
var countries:Array = new Array();
countries.push(africa);
countries.push(asia);
countries.push(europe);
countries.push(oceania);
countries.push(northAmerica);
countries.push(southAmerica);

//----This was a test to see if I could convert the instance name of an mc to string
//----but the textfield just displays '[object movieclip]'

var currentCountry:String = countries[0].toString();

function doRollOver(e:MouseEvent):void{
toolTip.visible = true;
toolTip.toolText.text = currentCountry;
}
function doRollOut(e:MouseEvent):void{
toolTip.visible = false;
}
//assign listeners to each movieclip in the countries array
function init():void
{
var i=0;
var hitArea;
for (i=0; i<countries.length; i++){
area = countries[i];
area.addEventListener(MouseEvent.MOUSE_OVER, doRollOver);
area.addEventListener(MouseEvent.MOUSE_OUT, doRollOut);
//area.addEventListener(MouseEvent.MOUSE_OVER, doClick);
}
}
init();

r3dkrpto
September 30th, 2008, 04:18 PM
Ahhh!

Never mind. You got it!


If you have the name property set for the country movieclips, just use africa.name for instance to get the name of the country. I doubt toString() is defined for the movieclips which is why you're just getting [object movieclip]. A movieclip has a name property which is the instance name so if you gave each country an instance name, you can grab that by using [instance].name, like africa.name.

In your doRollover event handler, you should be able to just write:
toolTip.toolText.text = Movieclip(e.target).name to get the instance name of the movieclip that the mouse is over.

Hope that helps.

davelk
September 30th, 2008, 04:26 PM
one small problem...

an instance name can't have spaces in and I dont want my tooltip to display northamerica as one word.

anyone help?