PDA

View Full Version : accessing function from different function


luka66_6
07-23-2008, 01:38 PM
here is my code

// gumb start

start_btn.addEventListener(MouseEvent.MOUSE_UP, startGame)

function startGame(event:MouseEvent):void
{
// gumb start izgubi listener in izgine iz odra

start_btn.removeEventListener(MouseEvent.MOUSE_DOW N, startGame)
removeChild(start_btn);


// napadalec začne napad

napadalec.play();


// tarča postaneš ti

tarca.addEventListener(Event.ENTER_FRAME, followMouse)
function followMouse(event:Event):void
{
tarca.x = mouseX;
tarca.y = mouseY;
Mouse.hide();
}


// life se začne prikazovati

lifeCounter.lifeCounter_txt.text = lifeCount + " Lives Left ";
}

// nisi več tarča

tarca.addEventListener(MouseEvent.MOUSE_UP, tarcaOff)
function tarcaOff(event:MouseEvent):void
{
tarca.removeEventListener(MouseEvent.MOUSE_UP, tarcaOff)
tarca.removeEventListener(Event.ENTER_FRAME, followMouse)
tarca.x = 10;
tarca.y = 10;
Mouse.show();
}


i am trying to acess
followMouse

from

tarcaOff

how do i do that?

as at the moment i get an error undefinf property followMouse. I know it is so becouse i have closed startGame function but how do i access a function inside function from some outside function.

hope i did not complicate too much.

regards

Chris220
07-28-2008, 01:11 PM
Just add two parenthesis "()" after "followMouse".

CW.Allen-Poole
07-28-2008, 07:03 PM
Just add two parenthesis "()" after "followMouse".

Because of the way that Flash works with functions, including parenthesis after followMouse will not call followMouse, but rather Flash will try to assign the listener to return value of followMouse. Ex:

broadcaster.addEventListener(Event.ENTER_FRAME, foo());
function foo(evt:Event){
trace('I want this to run on enterFrame');
return true;
}

This will be the equivalent of:

broadcaster.addEventListener(Event.ENTER_FRAME, true);


The problem here is actually the scope of the function. The function is nested within another function, therefore it does not exist outside of the external function. What you want is to move the function followMouse outside of the function startGame. You also may want to change the event from "Event.ENTER_FRAME" to "MouseEvent.MOUSE_MOVE," this will save you memory in the long run and therefore give you better performance. The updated code is below:
// gumb start

start_btn.addEventListener(MouseEvent.MOUSE_UP, startGame)

function startGame(event:MouseEvent):void
{
// gumb start izgubi listener in izgine iz odra

start_btn.removeEventListener(MouseEvent.MOUSE_DOW N, startGame)
removeChild(start_btn);


// napadalec začne napad

napadalec.play();


// tarča postaneš ti

tarca.addEventListener(MouseEvent.MOUSE_MOVE, followMouse)


// life se začne prikazovati

lifeCounter.lifeCounter_txt.text = lifeCount + " Lives Left ";
}

// nisi več tarča

function followMouse(MouseEvent:Event = null):void
// by putting '= null' you can fire this function without the parameter!
{
tarca.x = mouseX;
tarca.y = mouseY;
Mouse.hide();
}

tarca.addEventListener(MouseEvent.MOUSE_UP, tarcaOff)
function tarcaOff(event:MouseEvent):void
{
tarca.removeEventListener(MouseEvent.MOUSE_UP, tarcaOff)
tarca.removeEventListener(MouseEvent.MOUSE_MOVE, followMouse)
tarca.x = 10;
tarca.y = 10;
Mouse.show();
}

Chris220
08-27-2008, 11:58 AM
Oops.