PDA

View Full Version : Problem in calling function



kamikaze5141
May 14th, 2009, 02:50 AM
package com.games.flashgames.common {
import flash.display.*;
import flash.events.*;


public class Title extends MovieClip {

startButton.addEventListener(MouseEvent.CLICK, startButtonClicked);
}

private function startButtonClicked(mouseEvent:MouseEvent):void {
Object(parent).showHighScores();
}
}



the code works fine , but actually i want the "Object(parent).showHighScores();" to be load automatically without clicking the button (auto call the function once the page loaded), i tried use Event.Enter_frame but it wont stop running, can u help me ?

sebrofm
May 14th, 2009, 03:15 AM
addEventListener(Event.ADDED, showScores);

private function showScores(e:Event) : void{
Object(parent).showHighScores();
}

Shaedo
May 14th, 2009, 03:19 AM
EDIT sebrofm has a better solution/way of doing it but just in case here is mine, which I think is closer to the way you were approaching it.

Not quite sure how you want this to work but if you want to call somthing once you could either call it in the constructor (basically a function with the same name as your class) or you could use the enterframe and then remove the listener when it gets called



package com.games.flashgames.common
{
import flash.display.*;
import flash.events.*;
public class Title extends MovieClip
{
function Title():void // constructor function that runs DURING the construction
{
// EITHER PUT your code here eg Object(parent).showHighScores();
addEventListener(Event.ENTER_FRAME, enterFrame);
}

private function enterFrame(e:Event):void
{
// OR PUT your code here eg Object(parent).showHighScores();
removeEventListener(Event.ENTER_FRAME, enterFrame);
}
}
}


To break down what this code is doing for you:
first it has a constructor function 'function Title():void' that has the same name as the class. It almost always runs automatically, once, when you construct/initialise the object from the class (there are exceptions but not that you need to worry about) in your document class it generally runs when the swf is opened or loaded.

we are then adding a Event.ENTER_FRAME which you did but could not get it to stop repeating over and over. We achieve this by simply removing it once it gets called with the removeEventListener() note the arguments (bits in the brackets) must be identical between the remove... and add.... event listeners.

some cautionary info. The constructor runs during the construction. so for example it will run before the object is actually added to the stage. This means that it can't do everything straight off and might cause some hassles. But you can cross that bridge when you come to it.

Best of luck!

birdwing
May 14th, 2009, 03:24 AM
your best bet is to go with what sebrofm posted.

Which does the function when the event is loaded (which should be when the page loads)

kamikaze5141
May 14th, 2009, 04:41 AM
thx alot guys.... but i still facing some problem here.

for sebrofm solution i still facing the same problem, it wont stop running !!!!

and for Shaedo solution, actually the showHighScores() (to show high scores in a table) that i wanna call is in .swc means it is a component so if i remove it the scores table will also gone as well...........

can anyone help me ???

kadaj
May 14th, 2009, 10:14 AM
after adding the event listener try removing the event listener inside the called function.

kamikaze5141
May 14th, 2009, 10:47 PM
guys ..... thx for your help !!! its actually works.... BUT there is still this error:

TypeError: Error #1010: A term is undefined and has no properties.

i wonder y ?? the flash is still able to run without problem when i convert it into .swf.

sebrofm
May 15th, 2009, 01:09 AM
debug it and figure out what line is causing it